ApcuEngineTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 bool
  30. */
  31. static protected $useRequestTime = null;
  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()
  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. */
  48. public static function teardownAfterClass()
  49. {
  50. ini_set('apc.use_request_time', (string)static::$useRequestTime);
  51. }
  52. /**
  53. * setUp method
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. parent::setUp();
  60. $this->skipIf(!function_exists('apcu_store'), 'APCu is not installed or configured properly.');
  61. if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
  62. $this->skipIf(!ini_get('apc.enable_cli'), 'APCu is not enabled for the CLI.');
  63. }
  64. Cache::enable();
  65. $this->_configCache();
  66. Cache::clearAll();
  67. }
  68. /**
  69. * tearDown method
  70. *
  71. * @return void
  72. */
  73. public function tearDown()
  74. {
  75. parent::tearDown();
  76. Cache::drop('apcu');
  77. Cache::drop('apcu_groups');
  78. }
  79. /**
  80. * Helper method for testing.
  81. *
  82. * @param array $config
  83. * @return void
  84. */
  85. protected function _configCache($config = [])
  86. {
  87. $defaults = [
  88. 'className' => 'Apcu',
  89. 'prefix' => 'cake_',
  90. 'warnOnWriteFailures' => true,
  91. ];
  92. Cache::drop('apcu');
  93. Cache::setConfig('apcu', array_merge($defaults, $config));
  94. }
  95. /**
  96. * testReadAndWriteCache method
  97. *
  98. * @return void
  99. */
  100. public function testReadAndWriteCache()
  101. {
  102. $this->_configCache(['duration' => 1]);
  103. $result = Cache::read('test', 'apcu');
  104. $expecting = '';
  105. $this->assertEquals($expecting, $result);
  106. $data = 'this is a test of the emergency broadcasting system';
  107. $result = Cache::write('test', $data, 'apcu');
  108. $this->assertTrue($result);
  109. $result = Cache::read('test', 'apcu');
  110. $expecting = $data;
  111. $this->assertEquals($expecting, $result);
  112. Cache::delete('test', 'apcu');
  113. }
  114. /**
  115. * Writing cache entries with duration = 0 (forever) should work.
  116. *
  117. * @return void
  118. */
  119. public function testReadWriteDurationZero()
  120. {
  121. Cache::drop('apcu');
  122. Cache::setConfig('apcu', ['engine' => 'Apcu', 'duration' => 0, 'prefix' => 'cake_']);
  123. Cache::write('zero', 'Should save', 'apcu');
  124. sleep(1);
  125. $result = Cache::read('zero', 'apcu');
  126. $this->assertEquals('Should save', $result);
  127. }
  128. /**
  129. * Test get with default value
  130. *
  131. * @return void
  132. */
  133. public function testGetDefaultValue()
  134. {
  135. $apcu = Cache::pool('apcu');
  136. $this->assertFalse($apcu->get('nope', false));
  137. $this->assertNull($apcu->get('nope', null));
  138. $this->assertTrue($apcu->get('nope', true));
  139. $this->assertSame(0, $apcu->get('nope', 0));
  140. $apcu->set('yep', 0);
  141. $this->assertSame(0, $apcu->get('yep', false));
  142. }
  143. /**
  144. * testExpiry method
  145. *
  146. * @return void
  147. */
  148. public function testExpiry()
  149. {
  150. $this->_configCache(['duration' => 1]);
  151. $result = Cache::read('test', 'apcu');
  152. $this->assertNull($result);
  153. $data = 'this is a test of the emergency broadcasting system';
  154. $result = Cache::write('other_test', $data, 'apcu');
  155. $this->assertTrue($result);
  156. sleep(2);
  157. $result = Cache::read('other_test', 'apcu');
  158. $this->assertNull($result);
  159. $this->assertSame(0, Cache::pool('apcu')->get('other_test', 0), 'expired values get default.');
  160. }
  161. /**
  162. * test set ttl parameter
  163. *
  164. * @return void
  165. */
  166. public function testSetWithTtl()
  167. {
  168. $this->_configCache(['duration' => 99]);
  169. $engine = Cache::pool('apcu');
  170. $this->assertNull($engine->get('test'));
  171. $data = 'this is a test of the emergency broadcasting system';
  172. $this->assertTrue($engine->set('default_ttl', $data));
  173. $this->assertTrue($engine->set('int_ttl', $data, 1));
  174. $this->assertTrue($engine->set('interval_ttl', $data, new DateInterval('PT1S')));
  175. sleep(2);
  176. $this->assertNull($engine->get('int_ttl'));
  177. $this->assertNull($engine->get('interval_ttl'));
  178. $this->assertSame($data, $engine->get('default_ttl'));
  179. }
  180. /**
  181. * testDeleteCache method
  182. *
  183. * @return void
  184. */
  185. public function testDeleteCache()
  186. {
  187. $data = 'this is a test of the emergency broadcasting system';
  188. $result = Cache::write('delete_test', $data, 'apcu');
  189. $this->assertTrue($result);
  190. $result = Cache::delete('delete_test', 'apcu');
  191. $this->assertTrue($result);
  192. }
  193. /**
  194. * testDecrement method
  195. *
  196. * @return void
  197. */
  198. public function testDecrement()
  199. {
  200. $result = Cache::write('test_decrement', 5, 'apcu');
  201. $this->assertTrue($result);
  202. $result = Cache::decrement('test_decrement', 1, 'apcu');
  203. $this->assertEquals(4, $result);
  204. $result = Cache::read('test_decrement', 'apcu');
  205. $this->assertEquals(4, $result);
  206. $result = Cache::decrement('test_decrement', 2, 'apcu');
  207. $this->assertEquals(2, $result);
  208. $result = Cache::read('test_decrement', 'apcu');
  209. $this->assertEquals(2, $result);
  210. }
  211. /**
  212. * testIncrement method
  213. *
  214. * @return void
  215. */
  216. public function testIncrement()
  217. {
  218. $result = Cache::write('test_increment', 5, 'apcu');
  219. $this->assertTrue($result);
  220. $result = Cache::increment('test_increment', 1, 'apcu');
  221. $this->assertEquals(6, $result);
  222. $result = Cache::read('test_increment', 'apcu');
  223. $this->assertEquals(6, $result);
  224. $result = Cache::increment('test_increment', 2, 'apcu');
  225. $this->assertEquals(8, $result);
  226. $result = Cache::read('test_increment', 'apcu');
  227. $this->assertEquals(8, $result);
  228. }
  229. /**
  230. * test the clearing of cache keys
  231. *
  232. * @return void
  233. */
  234. public function testClear()
  235. {
  236. apcu_store('not_cake', 'survive');
  237. Cache::write('some_value', 'value', 'apcu');
  238. $result = Cache::clear('apcu');
  239. $this->assertTrue($result);
  240. $this->assertNull(Cache::read('some_value', 'apcu'));
  241. $this->assertEquals('survive', apcu_fetch('not_cake'));
  242. apcu_delete('not_cake');
  243. }
  244. /**
  245. * Tests that configuring groups for stored keys return the correct values when read/written
  246. * Shows that altering the group value is equivalent to deleting all keys under the same
  247. * group
  248. *
  249. * @return void
  250. */
  251. public function testGroupsReadWrite()
  252. {
  253. Cache::setConfig('apcu_groups', [
  254. 'engine' => 'Apcu',
  255. 'duration' => 0,
  256. 'groups' => ['group_a', 'group_b'],
  257. 'prefix' => 'test_',
  258. 'warnOnWriteFailures' => true,
  259. ]);
  260. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  261. $this->assertEquals('value', Cache::read('test_groups', 'apcu_groups'));
  262. apcu_inc('test_group_a');
  263. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  264. $this->assertTrue(Cache::write('test_groups', 'value2', 'apcu_groups'));
  265. $this->assertEquals('value2', Cache::read('test_groups', 'apcu_groups'));
  266. apcu_inc('test_group_b');
  267. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  268. $this->assertTrue(Cache::write('test_groups', 'value3', 'apcu_groups'));
  269. $this->assertEquals('value3', Cache::read('test_groups', 'apcu_groups'));
  270. }
  271. /**
  272. * Tests that deleting from a groups-enabled config is possible
  273. *
  274. * @return void
  275. */
  276. public function testGroupDelete()
  277. {
  278. Cache::setConfig('apcu_groups', [
  279. 'engine' => 'Apcu',
  280. 'duration' => 0,
  281. 'groups' => ['group_a', 'group_b'],
  282. 'prefix' => 'test_',
  283. 'warnOnWriteFailures' => true,
  284. ]);
  285. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  286. $this->assertEquals('value', Cache::read('test_groups', 'apcu_groups'));
  287. $this->assertTrue(Cache::delete('test_groups', 'apcu_groups'));
  288. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  289. }
  290. /**
  291. * Test clearing a cache group
  292. *
  293. * @return void
  294. */
  295. public function testGroupClear()
  296. {
  297. Cache::setConfig('apcu_groups', [
  298. 'engine' => 'Apcu',
  299. 'duration' => 0,
  300. 'groups' => ['group_a', 'group_b'],
  301. 'prefix' => 'test_',
  302. 'warnOnWriteFailures' => true,
  303. ]);
  304. $this->assertTrue(Cache::write('test_groups', 'value', 'apcu_groups'));
  305. $this->assertTrue(Cache::clearGroup('group_a', 'apcu_groups'));
  306. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  307. $this->assertTrue(Cache::write('test_groups', 'value2', 'apcu_groups'));
  308. $this->assertTrue(Cache::clearGroup('group_b', 'apcu_groups'));
  309. $this->assertNull(Cache::read('test_groups', 'apcu_groups'));
  310. }
  311. /**
  312. * Test add
  313. *
  314. * @return void
  315. */
  316. public function testAdd()
  317. {
  318. Cache::delete('test_add_key', 'apcu');
  319. $result = Cache::add('test_add_key', 'test data', 'apcu');
  320. $this->assertTrue($result);
  321. $expected = 'test data';
  322. $result = Cache::read('test_add_key', 'apcu');
  323. $this->assertEquals($expected, $result);
  324. $result = Cache::add('test_add_key', 'test data 2', 'apcu');
  325. $this->assertFalse($result);
  326. }
  327. }