ApcuEngineTest.php 8.6 KB

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