ArrayEngineTest.php 7.9 KB

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