ApcuEngineTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. use DateInterval;
  21. /**
  22. * ApcuEngineTest class
  23. */
  24. class ApcuEngineTest extends TestCase
  25. {
  26. /**
  27. * useRequestTime original value
  28. *
  29. * @var string
  30. */
  31. protected static $useRequestTime;
  32. /**
  33. * Ensure use_request_time is turned off
  34. *
  35. * If use_request_time is on, all cache entries are inserted with the same
  36. * timestamp and ttl comparisons within the same request are effectively
  37. * meaningless
  38. */
  39. public static function setUpBeforeClass(): void
  40. {
  41. static::$useRequestTime = ini_get('apc.use_request_time');
  42. ini_set('apc.use_request_time', '0');
  43. }
  44. /**
  45. * Reset apc.user_request_time to original value
  46. */
  47. public static function teardownAfterClass(): void
  48. {
  49. ini_set('apc.use_request_time', static::$useRequestTime ? '1' : '0');
  50. }
  51. /**
  52. * setUp method
  53. */
  54. public function setUp(): void
  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. public function tearDown(): void
  69. {
  70. parent::tearDown();
  71. Cache::drop('apcu');
  72. Cache::drop('apcu_groups');
  73. }
  74. /**
  75. * Helper method for testing.
  76. *
  77. * @param array $config
  78. */
  79. protected function _configCache(array $config = []): void
  80. {
  81. $defaults = [
  82. 'className' => 'Apcu',
  83. 'prefix' => 'cake_',
  84. 'warnOnWriteFailures' => true,
  85. ];
  86. Cache::drop('apcu');
  87. Cache::setConfig('apcu', array_merge($defaults, $config));
  88. }
  89. /**
  90. * testReadAndWriteCache method
  91. */
  92. public function testReadAndWriteCache(): void
  93. {
  94. $this->_configCache(['duration' => 1]);
  95. $result = Cache::read('test', 'apcu');
  96. $this->assertNull($result);
  97. $data = 'this is a test of the emergency broadcasting system';
  98. $result = Cache::write('test', $data, 'apcu');
  99. $this->assertTrue($result);
  100. $result = Cache::read('test', 'apcu');
  101. $expecting = $data;
  102. $this->assertSame($expecting, $result);
  103. Cache::delete('test', 'apcu');
  104. }
  105. /**
  106. * Writing cache entries with duration = 0 (forever) should work.
  107. */
  108. public function testReadWriteDurationZero(): void
  109. {
  110. Cache::drop('apcu');
  111. Cache::setConfig('apcu', ['engine' => 'Apcu', 'duration' => 0, 'prefix' => 'cake_']);
  112. Cache::write('zero', 'Should save', 'apcu');
  113. sleep(1);
  114. $result = Cache::read('zero', 'apcu');
  115. $this->assertSame('Should save', $result);
  116. }
  117. /**
  118. * Test get with default value
  119. */
  120. public function testGetDefaultValue(): void
  121. {
  122. $apcu = Cache::pool('apcu');
  123. $this->assertFalse($apcu->get('nope', false));
  124. $this->assertNull($apcu->get('nope', null));
  125. $this->assertTrue($apcu->get('nope', true));
  126. $this->assertSame(0, $apcu->get('nope', 0));
  127. $apcu->set('yep', 0);
  128. $this->assertSame(0, $apcu->get('yep', false));
  129. }
  130. /**
  131. * testExpiry method
  132. */
  133. public function testExpiry(): void
  134. {
  135. $this->_configCache(['duration' => 1]);
  136. $result = Cache::read('test', 'apcu');
  137. $this->assertNull($result);
  138. $data = 'this is a test of the emergency broadcasting system';
  139. $result = Cache::write('other_test', $data, 'apcu');
  140. $this->assertTrue($result);
  141. sleep(2);
  142. $result = Cache::read('other_test', 'apcu');
  143. $this->assertNull($result);
  144. $this->assertSame(0, Cache::pool('apcu')->get('other_test', 0), 'expired values get default.');
  145. }
  146. /**
  147. * test set ttl parameter
  148. */
  149. public function testSetWithTtl(): void
  150. {
  151. $this->_configCache(['duration' => 99]);
  152. $engine = Cache::pool('apcu');
  153. $this->assertNull($engine->get('test'));
  154. $data = 'this is a test of the emergency broadcasting system';
  155. $this->assertTrue($engine->set('default_ttl', $data));
  156. $this->assertTrue($engine->set('int_ttl', $data, 1));
  157. $this->assertTrue($engine->set('interval_ttl', $data, new DateInterval('PT1S')));
  158. sleep(2);
  159. $this->assertNull($engine->get('int_ttl'));
  160. $this->assertNull($engine->get('interval_ttl'));
  161. $this->assertSame($data, $engine->get('default_ttl'));
  162. }
  163. /**
  164. * testDeleteCache method
  165. */
  166. public function testDeleteCache(): void
  167. {
  168. $data = 'this is a test of the emergency broadcasting system';
  169. $result = Cache::write('delete_test', $data, 'apcu');
  170. $this->assertTrue($result);
  171. $result = Cache::delete('delete_test', 'apcu');
  172. $this->assertTrue($result);
  173. }
  174. /**
  175. * testDecrement method
  176. */
  177. public function testDecrement(): void
  178. {
  179. $result = Cache::write('test_decrement', 5, 'apcu');
  180. $this->assertTrue($result);
  181. $result = Cache::decrement('test_decrement', 1, 'apcu');
  182. $this->assertSame(4, $result);
  183. $result = Cache::read('test_decrement', 'apcu');
  184. $this->assertSame(4, $result);
  185. $result = Cache::decrement('test_decrement', 2, 'apcu');
  186. $this->assertSame(2, $result);
  187. $result = Cache::read('test_decrement', 'apcu');
  188. $this->assertSame(2, $result);
  189. }
  190. /**
  191. * testIncrement method
  192. */
  193. public function testIncrement(): void
  194. {
  195. $result = Cache::write('test_increment', 5, 'apcu');
  196. $this->assertTrue($result);
  197. $result = Cache::increment('test_increment', 1, 'apcu');
  198. $this->assertSame(6, $result);
  199. $result = Cache::read('test_increment', 'apcu');
  200. $this->assertSame(6, $result);
  201. $result = Cache::increment('test_increment', 2, 'apcu');
  202. $this->assertSame(8, $result);
  203. $result = Cache::read('test_increment', 'apcu');
  204. $this->assertSame(8, $result);
  205. }
  206. /**
  207. * test the clearing of cache keys
  208. */
  209. public function testClear(): void
  210. {
  211. apcu_store('not_cake', 'survive');
  212. Cache::write('some_value', 'value', 'apcu');
  213. $result = Cache::clear('apcu');
  214. $this->assertTrue($result);
  215. $this->assertNull(Cache::read('some_value', 'apcu'));
  216. $this->assertSame('survive', apcu_fetch('not_cake'));
  217. apcu_delete('not_cake');
  218. }
  219. /**
  220. * Tests that configuring groups for stored keys return the correct values when read/written
  221. * Shows that altering the group value is equivalent to deleting all keys under the same
  222. * group
  223. */
  224. public function testGroupsReadWrite(): void
  225. {
  226. Cache::setConfig('apcu_groups', [
  227. 'engine' => 'Apcu',
  228. 'duration' => 0,
  229. 'groups' => ['group_a', 'group_b'],
  230. 'prefix' => 'test_',
  231. 'warnOnWriteFailures' => true,
  232. ]);
  233. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  234. $this->assertSame('value', Cache::read('test_groups', 'apcu_groups'));
  235. apcu_inc('test_group_a');
  236. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  237. $this->assertTrue(Cache::write('test_groups', 'value2', 'apcu_groups'));
  238. $this->assertSame('value2', Cache::read('test_groups', 'apcu_groups'));
  239. apcu_inc('test_group_b');
  240. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  241. $this->assertTrue(Cache::write('test_groups', 'value3', 'apcu_groups'));
  242. $this->assertSame('value3', Cache::read('test_groups', 'apcu_groups'));
  243. }
  244. /**
  245. * Tests that deleting from a groups-enabled config is possible
  246. */
  247. public function testGroupDelete(): void
  248. {
  249. Cache::setConfig('apcu_groups', [
  250. 'engine' => 'Apcu',
  251. 'duration' => 0,
  252. 'groups' => ['group_a', 'group_b'],
  253. 'prefix' => 'test_',
  254. 'warnOnWriteFailures' => true,
  255. ]);
  256. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  257. $this->assertSame('value', Cache::read('test_groups', 'apcu_groups'));
  258. $this->assertTrue(Cache::delete('test_groups', 'apcu_groups'));
  259. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  260. }
  261. /**
  262. * Test clearing a cache group
  263. */
  264. public function testGroupClear(): void
  265. {
  266. Cache::setConfig('apcu_groups', [
  267. 'engine' => 'Apcu',
  268. 'duration' => 0,
  269. 'groups' => ['group_a', 'group_b'],
  270. 'prefix' => 'test_',
  271. 'warnOnWriteFailures' => true,
  272. ]);
  273. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  274. $this->assertTrue(Cache::clearGroup('group_a', 'apcu_groups'));
  275. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  276. $this->assertTrue(Cache::write('test_groups', 'value2', 'apcu_groups'));
  277. $this->assertTrue(Cache::clearGroup('group_b', 'apcu_groups'));
  278. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  279. }
  280. /**
  281. * Test add
  282. */
  283. public function testAdd(): void
  284. {
  285. Cache::delete('test_add_key', 'apcu');
  286. $result = Cache::add('test_add_key', 'test data', 'apcu');
  287. $this->assertTrue($result);
  288. $expected = 'test data';
  289. $result = Cache::read('test_add_key', 'apcu');
  290. $this->assertSame($expected, $result);
  291. $result = Cache::add('test_add_key', 'test data 2', 'apcu');
  292. $this->assertFalse($result);
  293. }
  294. }