ArrayEngineTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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\Cache\Engine\ArrayEngine;
  19. use Cake\Cache\InvalidArgumentException;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ArrayEngineTest class
  23. */
  24. class ArrayEngineTest extends TestCase
  25. {
  26. /**
  27. * setUp method
  28. */
  29. public function setUp(): void
  30. {
  31. parent::setUp();
  32. Cache::enable();
  33. $this->_configCache();
  34. Cache::clearAll();
  35. }
  36. /**
  37. * tearDown method
  38. */
  39. public function tearDown(): void
  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. */
  50. protected function _configCache($config = []): void
  51. {
  52. $defaults = [
  53. 'className' => 'Array',
  54. 'prefix' => 'cake_',
  55. 'warnOnWriteFailures' => true,
  56. ];
  57. Cache::drop('array');
  58. Cache::setConfig('array', array_merge($defaults, $config));
  59. }
  60. /**
  61. * testReadAndWriteCache method
  62. */
  63. public function testReadAndWriteCache(): void
  64. {
  65. $this->_configCache(['duration' => 1]);
  66. $result = Cache::read('test', 'array');
  67. $this->assertNull($result);
  68. $data = 'this is a test of the emergency broadcasting system';
  69. $result = Cache::write('test', $data, 'array');
  70. $this->assertTrue($result);
  71. $result = Cache::read('test', 'array');
  72. $expecting = $data;
  73. $this->assertSame($expecting, $result);
  74. Cache::delete('test', 'array');
  75. }
  76. /**
  77. * testExpiry method
  78. */
  79. public function testExpiry(): void
  80. {
  81. $this->_configCache(['duration' => 1]);
  82. $result = Cache::read('test', 'array');
  83. $this->assertNull($result);
  84. $data = 'this is a test of the emergency broadcasting system';
  85. $result = Cache::write('other_test', $data, 'array');
  86. $this->assertTrue($result);
  87. sleep(2);
  88. $result = Cache::read('other_test', 'array');
  89. $this->assertNull($result);
  90. }
  91. /**
  92. * testDeleteCache method
  93. */
  94. public function testDeleteCache(): void
  95. {
  96. $data = 'this is a test of the emergency broadcasting system';
  97. $result = Cache::write('delete_test', $data, 'array');
  98. $this->assertTrue($result);
  99. $result = Cache::delete('delete_test', 'array');
  100. $this->assertTrue($result);
  101. }
  102. /**
  103. * testDecrement method
  104. */
  105. public function testDecrement(): void
  106. {
  107. $result = Cache::write('test_decrement', 5, 'array');
  108. $this->assertTrue($result);
  109. $result = Cache::decrement('test_decrement', 1, 'array');
  110. $this->assertSame(4, $result);
  111. $result = Cache::read('test_decrement', 'array');
  112. $this->assertSame(4, $result);
  113. $result = Cache::decrement('test_decrement', 2, 'array');
  114. $this->assertSame(2, $result);
  115. $result = Cache::read('test_decrement', 'array');
  116. $this->assertSame(2, $result);
  117. }
  118. /**
  119. * testIncrement method
  120. */
  121. public function testIncrement(): void
  122. {
  123. $result = Cache::write('test_increment', 5, 'array');
  124. $this->assertTrue($result);
  125. $result = Cache::increment('test_increment', 1, 'array');
  126. $this->assertSame(6, $result);
  127. $result = Cache::read('test_increment', 'array');
  128. $this->assertSame(6, $result);
  129. $result = Cache::increment('test_increment', 2, 'array');
  130. $this->assertSame(8, $result);
  131. $result = Cache::read('test_increment', 'array');
  132. $this->assertSame(8, $result);
  133. }
  134. /**
  135. * test the clearing of cache keys
  136. */
  137. public function testClear(): void
  138. {
  139. Cache::write('some_value', 'value', 'array');
  140. $result = Cache::clear('array');
  141. $this->assertTrue($result);
  142. $this->assertNull(Cache::read('some_value', 'array'));
  143. }
  144. /**
  145. * Tests that configuring groups for stored keys return the correct values when read/written
  146. * Shows that altering the group value is equivalent to deleting all keys under the same
  147. * group
  148. */
  149. public function testGroupsReadWrite(): void
  150. {
  151. Cache::setConfig('array_groups', [
  152. 'engine' => 'array',
  153. 'duration' => 30,
  154. 'groups' => ['group_a', 'group_b'],
  155. 'prefix' => 'test_',
  156. 'warnOnWriteFailures' => true,
  157. ]);
  158. $this->assertTrue(Cache::write('test_groups', 'value', 'array_groups'));
  159. $this->assertSame('value', Cache::read('test_groups', 'array_groups'));
  160. Cache::clearGroup('group_a', 'array_groups');
  161. $this->assertNull(Cache::read('test_groups', 'array_groups'));
  162. $this->assertTrue(Cache::write('test_groups', 'value2', 'array_groups'));
  163. $this->assertSame('value2', Cache::read('test_groups', 'array_groups'));
  164. Cache::clearGroup('group_b', 'array_groups');
  165. $this->assertNull(Cache::read('test_groups', 'array_groups'));
  166. $this->assertTrue(Cache::write('test_groups', 'value3', 'array_groups'));
  167. $this->assertSame('value3', Cache::read('test_groups', 'array_groups'));
  168. }
  169. /**
  170. * Tests that deleting from a groups-enabled config is possible
  171. */
  172. public function testGroupDelete(): void
  173. {
  174. Cache::setConfig('array_groups', [
  175. 'engine' => 'array',
  176. 'duration' => 10,
  177. 'groups' => ['group_a', 'group_b'],
  178. 'prefix' => 'test_',
  179. 'warnOnWriteFailures' => true,
  180. ]);
  181. $this->assertTrue(Cache::write('test_groups', 'value', 'array_groups'));
  182. $this->assertSame('value', Cache::read('test_groups', 'array_groups'));
  183. $this->assertTrue(Cache::delete('test_groups', 'array_groups'));
  184. $this->assertNull(Cache::read('test_groups', 'array_groups'));
  185. }
  186. /**
  187. * Test clearing a cache group
  188. */
  189. public function testGroupClear(): void
  190. {
  191. Cache::setConfig('array_groups', [
  192. 'engine' => 'array',
  193. 'duration' => 10,
  194. 'groups' => ['group_a', 'group_b'],
  195. 'prefix' => 'test_',
  196. 'warnOnWriteFailures' => true,
  197. ]);
  198. $this->assertTrue(Cache::write('test_groups', 'value', 'array_groups'));
  199. $this->assertTrue(Cache::clearGroup('group_a', 'array_groups'));
  200. $this->assertNull(Cache::read('test_groups', 'array_groups'));
  201. $this->assertTrue(Cache::write('test_groups', 'value2', 'array_groups'));
  202. $this->assertTrue(Cache::clearGroup('group_b', 'array_groups'));
  203. $this->assertNull(Cache::read('test_groups', 'array_groups'));
  204. }
  205. /**
  206. * Test add
  207. */
  208. public function testAdd(): void
  209. {
  210. Cache::delete('test_add_key', 'array');
  211. $result = Cache::add('test_add_key', 'test data', 'array');
  212. $this->assertTrue($result);
  213. $expected = 'test data';
  214. $result = Cache::read('test_add_key', 'array');
  215. $this->assertSame($expected, $result);
  216. $result = Cache::add('test_add_key', 'test data 2', 'array');
  217. $this->assertFalse($result);
  218. }
  219. /**
  220. * Test writeMany() with Traversable
  221. */
  222. public function testWriteManyTraversable(): void
  223. {
  224. $data = new ArrayObject([
  225. 'a' => 1,
  226. 'b' => 'foo',
  227. ]);
  228. $result = Cache::writeMany($data, 'array');
  229. $this->assertTrue($result);
  230. $this->assertSame(1, Cache::read('a', 'array'));
  231. $this->assertSame('foo', Cache::read('b', 'array'));
  232. }
  233. /**
  234. * Test that passing a non iterable argument to setMultiple() throws exception.
  235. */
  236. public function testSetMultipleException(): void
  237. {
  238. $this->expectException(InvalidArgumentException::class);
  239. $this->expectExceptionMessage('A cache set must be either an array or a Traversable.');
  240. $engine = new ArrayEngine();
  241. $engine->setMultiple('foo');
  242. }
  243. /**
  244. * Test that passing a non iterable argument to getMultiple() throws exception.
  245. */
  246. public function testGetMultipleException(): void
  247. {
  248. $this->expectException(InvalidArgumentException::class);
  249. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  250. $engine = new ArrayEngine();
  251. $engine->getMultiple('foo');
  252. }
  253. /**
  254. * Test that passing a non iterable argument to deleteMultiple() throws exception.
  255. */
  256. public function testDeleteMultipleException(): void
  257. {
  258. $this->expectException(InvalidArgumentException::class);
  259. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  260. $engine = new ArrayEngine();
  261. $engine->deleteMultiple('foo');
  262. }
  263. }