/tests/TestCase/Cache/Engine/ApcuEngineTest.php

https://github.com/LubosRemplik/cakephp · PHP · 336 lines · 184 code · 46 blank · 106 comment · 2 complexity · 926729f921479442594a77386c3b7192 MD5 · raw file

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