ApcuEngineTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * ApcuEngineTest file
  5. *
  6. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 3.5.4
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Cache\Engine;
  18. use Cake\Cache\Cache;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * ApcuEngineTest class
  22. */
  23. class ApcuEngineTest extends TestCase
  24. {
  25. /**
  26. * useRequestTime original value
  27. *
  28. * @var bool
  29. */
  30. static protected $useRequestTime = null;
  31. /**
  32. * Ensure use_request_time is turned off
  33. *
  34. * If use_request_time is on, all cache entries are inserted with the same
  35. * timestamp and ttl comparisons within the same request are effectively
  36. * meaningless
  37. */
  38. public static function setUpBeforeClass()
  39. {
  40. static::$useRequestTime = ini_get('apc.use_request_time');
  41. ini_set('apc.use_request_time', '0');
  42. }
  43. /**
  44. * Reset apc.user_request_time to original value
  45. *
  46. */
  47. public static function teardownAfterClass()
  48. {
  49. ini_set('apc.use_request_time', (string)static::$useRequestTime);
  50. }
  51. /**
  52. * setUp method
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. parent::setUp();
  59. $this->skipIf(!function_exists('apcu_store'), 'APCu is not installed or configured properly.');
  60. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  61. $this->skipIf(!ini_get('apc.enable_cli'), 'APCu is not enabled for the CLI.');
  62. }
  63. Cache::enable();
  64. $this->_configCache();
  65. Cache::clearAll();
  66. }
  67. /**
  68. * tearDown method
  69. *
  70. * @return void
  71. */
  72. public function tearDown()
  73. {
  74. parent::tearDown();
  75. Cache::drop('apcu');
  76. Cache::drop('apcu_groups');
  77. }
  78. /**
  79. * Helper method for testing.
  80. *
  81. * @param array $config
  82. * @return void
  83. */
  84. protected function _configCache($config = [])
  85. {
  86. $defaults = [
  87. 'className' => 'Apcu',
  88. 'prefix' => 'cake_',
  89. 'warnOnWriteFailures' => true,
  90. ];
  91. Cache::drop('apcu');
  92. Cache::setConfig('apcu', array_merge($defaults, $config));
  93. }
  94. /**
  95. * testReadAndWriteCache method
  96. *
  97. * @return void
  98. */
  99. public function testReadAndWriteCache()
  100. {
  101. $this->_configCache(['duration' => 1]);
  102. $result = Cache::read('test', 'apcu');
  103. $expecting = '';
  104. $this->assertEquals($expecting, $result);
  105. $data = 'this is a test of the emergency broadcasting system';
  106. $result = Cache::write('test', $data, 'apcu');
  107. $this->assertTrue($result);
  108. $result = Cache::read('test', 'apcu');
  109. $expecting = $data;
  110. $this->assertEquals($expecting, $result);
  111. Cache::delete('test', 'apcu');
  112. }
  113. /**
  114. * Writing cache entries with duration = 0 (forever) should work.
  115. *
  116. * @return void
  117. */
  118. public function testReadWriteDurationZero()
  119. {
  120. Cache::drop('apcu');
  121. Cache::setConfig('apcu', ['engine' => 'Apcu', 'duration' => 0, 'prefix' => 'cake_']);
  122. Cache::write('zero', 'Should save', 'apcu');
  123. sleep(1);
  124. $result = Cache::read('zero', 'apcu');
  125. $this->assertEquals('Should save', $result);
  126. }
  127. /**
  128. * testExpiry method
  129. *
  130. * @return void
  131. */
  132. public function testExpiry()
  133. {
  134. $this->_configCache(['duration' => 1]);
  135. $result = Cache::read('test', 'apcu');
  136. $this->assertFalse($result);
  137. $data = 'this is a test of the emergency broadcasting system';
  138. $result = Cache::write('other_test', $data, 'apcu');
  139. $this->assertTrue($result);
  140. sleep(2);
  141. $result = Cache::read('other_test', 'apcu');
  142. $this->assertFalse($result);
  143. }
  144. /**
  145. * testDeleteCache method
  146. *
  147. * @return void
  148. */
  149. public function testDeleteCache()
  150. {
  151. $data = 'this is a test of the emergency broadcasting system';
  152. $result = Cache::write('delete_test', $data, 'apcu');
  153. $this->assertTrue($result);
  154. $result = Cache::delete('delete_test', 'apcu');
  155. $this->assertTrue($result);
  156. }
  157. /**
  158. * testDecrement method
  159. *
  160. * @return void
  161. */
  162. public function testDecrement()
  163. {
  164. $result = Cache::write('test_decrement', 5, 'apcu');
  165. $this->assertTrue($result);
  166. $result = Cache::decrement('test_decrement', 1, 'apcu');
  167. $this->assertEquals(4, $result);
  168. $result = Cache::read('test_decrement', 'apcu');
  169. $this->assertEquals(4, $result);
  170. $result = Cache::decrement('test_decrement', 2, 'apcu');
  171. $this->assertEquals(2, $result);
  172. $result = Cache::read('test_decrement', 'apcu');
  173. $this->assertEquals(2, $result);
  174. }
  175. /**
  176. * testIncrement method
  177. *
  178. * @return void
  179. */
  180. public function testIncrement()
  181. {
  182. $result = Cache::write('test_increment', 5, 'apcu');
  183. $this->assertTrue($result);
  184. $result = Cache::increment('test_increment', 1, 'apcu');
  185. $this->assertEquals(6, $result);
  186. $result = Cache::read('test_increment', 'apcu');
  187. $this->assertEquals(6, $result);
  188. $result = Cache::increment('test_increment', 2, 'apcu');
  189. $this->assertEquals(8, $result);
  190. $result = Cache::read('test_increment', 'apcu');
  191. $this->assertEquals(8, $result);
  192. }
  193. /**
  194. * test the clearing of cache keys
  195. *
  196. * @return void
  197. */
  198. public function testClear()
  199. {
  200. apcu_store('not_cake', 'survive');
  201. Cache::write('some_value', 'value', 'apcu');
  202. $result = Cache::clear('apcu');
  203. $this->assertTrue($result);
  204. $this->assertFalse(Cache::read('some_value', 'apcu'));
  205. $this->assertEquals('survive', apcu_fetch('not_cake'));
  206. apcu_delete('not_cake');
  207. }
  208. /**
  209. * Tests that configuring groups for stored keys return the correct values when read/written
  210. * Shows that altering the group value is equivalent to deleting all keys under the same
  211. * group
  212. *
  213. * @return void
  214. */
  215. public function testGroupsReadWrite()
  216. {
  217. Cache::setConfig('apcu_groups', [
  218. 'engine' => 'Apcu',
  219. 'duration' => 0,
  220. 'groups' => ['group_a', 'group_b'],
  221. 'prefix' => 'test_',
  222. 'warnOnWriteFailures' => true,
  223. ]);
  224. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  225. $this->assertEquals('value', Cache::read('test_groups', 'apcu_groups'));
  226. apcu_inc('test_group_a');
  227. $this->assertFalse(Cache::read('test_groups', 'apcu_groups'));
  228. $this->assertTrue(Cache::write('test_groups', 'value2', 'apcu_groups'));
  229. $this->assertEquals('value2', Cache::read('test_groups', 'apcu_groups'));
  230. apcu_inc('test_group_b');
  231. $this->assertFalse(Cache::read('test_groups', 'apcu_groups'));
  232. $this->assertTrue(Cache::write('test_groups', 'value3', 'apcu_groups'));
  233. $this->assertEquals('value3', Cache::read('test_groups', 'apcu_groups'));
  234. }
  235. /**
  236. * Tests that deleting from a groups-enabled config is possible
  237. *
  238. * @return void
  239. */
  240. public function testGroupDelete()
  241. {
  242. Cache::setConfig('apcu_groups', [
  243. 'engine' => 'Apcu',
  244. 'duration' => 0,
  245. 'groups' => ['group_a', 'group_b'],
  246. 'prefix' => 'test_',
  247. 'warnOnWriteFailures' => true,
  248. ]);
  249. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  250. $this->assertEquals('value', Cache::read('test_groups', 'apcu_groups'));
  251. $this->assertTrue(Cache::delete('test_groups', 'apcu_groups'));
  252. $this->assertFalse(Cache::read('test_groups', 'apcu_groups'));
  253. }
  254. /**
  255. * Test clearing a cache group
  256. *
  257. * @return void
  258. */
  259. public function testGroupClear()
  260. {
  261. Cache::setConfig('apcu_groups', [
  262. 'engine' => 'Apcu',
  263. 'duration' => 0,
  264. 'groups' => ['group_a', 'group_b'],
  265. 'prefix' => 'test_',
  266. 'warnOnWriteFailures' => true,
  267. ]);
  268. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  269. $this->assertTrue(Cache::clearGroup('group_a', 'apcu_groups'));
  270. $this->assertFalse(Cache::read('test_groups', 'apcu_groups'));
  271. $this->assertTrue(Cache::write('test_groups', 'value2', 'apcu_groups'));
  272. $this->assertTrue(Cache::clearGroup('group_b', 'apcu_groups'));
  273. $this->assertFalse(Cache::read('test_groups', 'apcu_groups'));
  274. }
  275. /**
  276. * Test add
  277. *
  278. * @return void
  279. */
  280. public function testAdd()
  281. {
  282. Cache::delete('test_add_key', 'apcu');
  283. $result = Cache::add('test_add_key', 'test data', 'apcu');
  284. $this->assertTrue($result);
  285. $expected = 'test data';
  286. $result = Cache::read('test_add_key', 'apcu');
  287. $this->assertEquals($expected, $result);
  288. $result = Cache::add('test_add_key', 'test data 2', 'apcu');
  289. $this->assertFalse($result);
  290. }
  291. }