ApcuEngineTest.php 9.3 KB

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