ArrayEngineTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.7.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Cache\Engine;
  15. use Cake\Cache\Cache;
  16. use Cake\TestSuite\TestCase;
  17. /**
  18. * ArrayEngineTest class
  19. */
  20. class ArrayEngineTest extends TestCase
  21. {
  22. /**
  23. * setUp method
  24. *
  25. * @return void
  26. */
  27. public function setUp()
  28. {
  29. parent::setUp();
  30. Cache::enable();
  31. $this->_configCache();
  32. Cache::clearAll();
  33. }
  34. /**
  35. * tearDown method
  36. *
  37. * @return void
  38. */
  39. public function tearDown()
  40. {
  41. parent::tearDown();
  42. Cache::drop('array');
  43. Cache::drop('array_groups');
  44. }
  45. /**
  46. * Helper method for testing.
  47. *
  48. * @param array $config
  49. * @return void
  50. */
  51. protected function _configCache($config = [])
  52. {
  53. $defaults = [
  54. 'className' => 'Array',
  55. 'prefix' => 'cake_',
  56. 'warnOnWriteFailures' => true,
  57. ];
  58. Cache::drop('array');
  59. Cache::setConfig('array', array_merge($defaults, $config));
  60. }
  61. /**
  62. * testReadAndWriteCache method
  63. *
  64. * @return void
  65. */
  66. public function testReadAndWriteCache()
  67. {
  68. $this->_configCache(['duration' => 1]);
  69. $result = Cache::read('test', 'array');
  70. $expecting = '';
  71. $this->assertEquals($expecting, $result);
  72. $data = 'this is a test of the emergency broadcasting system';
  73. $result = Cache::write('test', $data, 'array');
  74. $this->assertTrue($result);
  75. $result = Cache::read('test', 'array');
  76. $expecting = $data;
  77. $this->assertEquals($expecting, $result);
  78. Cache::delete('test', 'array');
  79. }
  80. /**
  81. * testExpiry method
  82. *
  83. * @return void
  84. */
  85. public function testExpiry()
  86. {
  87. $this->_configCache(['duration' => 1]);
  88. $result = Cache::read('test', 'array');
  89. $this->assertFalse($result);
  90. $data = 'this is a test of the emergency broadcasting system';
  91. $result = Cache::write('other_test', $data, 'array');
  92. $this->assertTrue($result);
  93. sleep(2);
  94. $result = Cache::read('other_test', 'array');
  95. $this->assertFalse($result);
  96. }
  97. /**
  98. * testDeleteCache method
  99. *
  100. * @return void
  101. */
  102. public function testDeleteCache()
  103. {
  104. $data = 'this is a test of the emergency broadcasting system';
  105. $result = Cache::write('delete_test', $data, 'array');
  106. $this->assertTrue($result);
  107. $result = Cache::delete('delete_test', 'array');
  108. $this->assertTrue($result);
  109. }
  110. /**
  111. * testDecrement method
  112. *
  113. * @return void
  114. */
  115. public function testDecrement()
  116. {
  117. $result = Cache::write('test_decrement', 5, 'array');
  118. $this->assertTrue($result);
  119. $result = Cache::decrement('test_decrement', 1, 'array');
  120. $this->assertEquals(4, $result);
  121. $result = Cache::read('test_decrement', 'array');
  122. $this->assertEquals(4, $result);
  123. $result = Cache::decrement('test_decrement', 2, 'array');
  124. $this->assertEquals(2, $result);
  125. $result = Cache::read('test_decrement', 'array');
  126. $this->assertEquals(2, $result);
  127. }
  128. /**
  129. * testIncrement method
  130. *
  131. * @return void
  132. */
  133. public function testIncrement()
  134. {
  135. $result = Cache::write('test_increment', 5, 'array');
  136. $this->assertTrue($result);
  137. $result = Cache::increment('test_increment', 1, 'array');
  138. $this->assertSame(6, $result);
  139. $result = Cache::read('test_increment', 'array');
  140. $this->assertSame(6, $result);
  141. $result = Cache::increment('test_increment', 2, 'array');
  142. $this->assertSame(8, $result);
  143. $result = Cache::read('test_increment', 'array');
  144. $this->assertSame(8, $result);
  145. }
  146. /**
  147. * test the clearing of cache keys
  148. *
  149. * @return void
  150. */
  151. public function testClear()
  152. {
  153. Cache::write('some_value', 'value', 'array');
  154. $result = Cache::clear(false, 'array');
  155. $this->assertTrue($result);
  156. $this->assertFalse(Cache::read('some_value', 'array'));
  157. }
  158. /**
  159. * Tests that configuring groups for stored keys return the correct values when read/written
  160. * Shows that altering the group value is equivalent to deleting all keys under the same
  161. * group
  162. *
  163. * @return void
  164. */
  165. public function testGroupsReadWrite()
  166. {
  167. Cache::setConfig('array_groups', [
  168. 'engine' => 'array',
  169. 'duration' => 30,
  170. 'groups' => ['group_a', 'group_b'],
  171. 'prefix' => 'test_',
  172. 'warnOnWriteFailures' => true,
  173. ]);
  174. $this->assertTrue(Cache::write('test_groups', 'value', 'array_groups'));
  175. $this->assertEquals('value', Cache::read('test_groups', 'array_groups'));
  176. Cache::clearGroup('group_a', 'array_groups');
  177. $this->assertFalse(Cache::read('test_groups', 'array_groups'));
  178. $this->assertTrue(Cache::write('test_groups', 'value2', 'array_groups'));
  179. $this->assertEquals('value2', Cache::read('test_groups', 'array_groups'));
  180. Cache::clearGroup('group_b', 'array_groups');
  181. $this->assertFalse(Cache::read('test_groups', 'array_groups'));
  182. $this->assertTrue(Cache::write('test_groups', 'value3', 'array_groups'));
  183. $this->assertEquals('value3', Cache::read('test_groups', 'array_groups'));
  184. }
  185. /**
  186. * Tests that deleting from a groups-enabled config is possible
  187. *
  188. * @return void
  189. */
  190. public function testGroupDelete()
  191. {
  192. Cache::setConfig('array_groups', [
  193. 'engine' => 'array',
  194. 'duration' => 10,
  195. 'groups' => ['group_a', 'group_b'],
  196. 'prefix' => 'test_',
  197. 'warnOnWriteFailures' => true,
  198. ]);
  199. $this->assertTrue(Cache::write('test_groups', 'value', 'array_groups'));
  200. $this->assertEquals('value', Cache::read('test_groups', 'array_groups'));
  201. $this->assertTrue(Cache::delete('test_groups', 'array_groups'));
  202. $this->assertFalse(Cache::read('test_groups', 'array_groups'));
  203. }
  204. /**
  205. * Test clearing a cache group
  206. *
  207. * @return void
  208. */
  209. public function testGroupClear()
  210. {
  211. Cache::setConfig('array_groups', [
  212. 'engine' => 'array',
  213. 'duration' => 10,
  214. 'groups' => ['group_a', 'group_b'],
  215. 'prefix' => 'test_',
  216. 'warnOnWriteFailures' => true,
  217. ]);
  218. $this->assertTrue(Cache::write('test_groups', 'value', 'array_groups'));
  219. $this->assertTrue(Cache::clearGroup('group_a', 'array_groups'));
  220. $this->assertFalse(Cache::read('test_groups', 'array_groups'));
  221. $this->assertTrue(Cache::write('test_groups', 'value2', 'array_groups'));
  222. $this->assertTrue(Cache::clearGroup('group_b', 'array_groups'));
  223. $this->assertFalse(Cache::read('test_groups', 'array_groups'));
  224. }
  225. /**
  226. * Test add
  227. *
  228. * @return void
  229. */
  230. public function testAdd()
  231. {
  232. Cache::delete('test_add_key', 'array');
  233. $result = Cache::add('test_add_key', 'test data', 'array');
  234. $this->assertTrue($result);
  235. $expected = 'test data';
  236. $result = Cache::read('test_add_key', 'array');
  237. $this->assertEquals($expected, $result);
  238. $result = Cache::add('test_add_key', 'test data 2', 'array');
  239. $this->assertFalse($result);
  240. }
  241. }