SimpleCacheEngineTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.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;
  16. use Cake\Cache\Engine\FileEngine;
  17. use Cake\Cache\SimpleCacheEngine;
  18. use Cake\TestSuite\TestCase;
  19. use Psr\SimpleCache\InvalidArgumentException;
  20. /**
  21. * SimpleCacheEngine class
  22. */
  23. class SimpleCacheEngineTest extends TestCase
  24. {
  25. /**
  26. * setup
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. $this->inner = new FileEngine();
  34. $this->inner->init([
  35. 'prefix' => '',
  36. 'path' => TMP . 'tests',
  37. 'duration' => 5,
  38. ]);
  39. $this->cache = new SimpleCacheEngine($this->inner);
  40. }
  41. /**
  42. * tear down
  43. *
  44. * @return void
  45. */
  46. public function tearDown()
  47. {
  48. parent::tearDown();
  49. $this->inner->clear(false);
  50. }
  51. /**
  52. * test getting keys
  53. *
  54. * @return void
  55. */
  56. public function testGetSuccess()
  57. {
  58. $this->inner->write('key_one', 'Some Value');
  59. $this->assertSame('Some Value', $this->cache->get('key_one'));
  60. $this->assertSame('Some Value', $this->cache->get('key_one', 'default'));
  61. }
  62. /**
  63. * test get on missing keys
  64. *
  65. * @return void
  66. */
  67. public function testGetNoKey()
  68. {
  69. $this->assertSame('default', $this->cache->get('no', 'default'));
  70. $this->assertNull($this->cache->get('no'));
  71. }
  72. /**
  73. * test get on invalid keys. The PSR spec outlines that an exception
  74. * must be raised.
  75. *
  76. * @return void
  77. */
  78. public function testGetInvalidKey()
  79. {
  80. $this->expectException(InvalidArgumentException::class);
  81. $this->cache->get('');
  82. }
  83. /**
  84. * test set() inheriting the default TTL
  85. *
  86. * @return void
  87. */
  88. public function testSetNoTtl()
  89. {
  90. $this->assertTrue($this->cache->set('key', 'a value'));
  91. $this->assertSame('a value', $this->cache->get('key'));
  92. }
  93. /**
  94. * test the TTL parameter of set()
  95. *
  96. * @return void
  97. */
  98. public function testSetWithTtl()
  99. {
  100. $this->assertTrue($this->cache->set('key', 'a value'));
  101. $this->assertTrue($this->cache->set('expired', 'a value', 0));
  102. sleep(1);
  103. $this->assertSame('a value', $this->cache->get('key'));
  104. $this->assertNull($this->cache->get('expired'));
  105. $this->assertSame(5, $this->inner->getConfig('duration'));
  106. }
  107. /**
  108. * test set() with an invalid key.
  109. *
  110. * @return void
  111. */
  112. public function testSetInvalidKey()
  113. {
  114. $this->expectException(InvalidArgumentException::class);
  115. $this->cache->set('', 'some data');
  116. }
  117. /**
  118. * test delete on known and unknown keys
  119. *
  120. * @return void
  121. */
  122. public function testDelete()
  123. {
  124. $this->cache->set('key', 'a value');
  125. $this->assertTrue($this->cache->delete('key'));
  126. $this->assertFalse($this->cache->delete('undefined'));
  127. }
  128. /**
  129. * test delete on an invalid key
  130. *
  131. * @return void
  132. */
  133. public function testDeleteInvalidKey()
  134. {
  135. $this->expectException(InvalidArgumentException::class);
  136. $this->cache->delete('');
  137. }
  138. /**
  139. * test clearing cache data
  140. *
  141. * @return void
  142. */
  143. public function testClear()
  144. {
  145. $this->cache->set('key', 'a value');
  146. $this->cache->set('key2', 'other value');
  147. $this->assertTrue($this->cache->clear());
  148. $this->assertNull($this->cache->get('key'));
  149. $this->assertNull($this->cache->get('key2'));
  150. }
  151. /**
  152. * test getMultiple
  153. *
  154. * @return void
  155. */
  156. public function testGetMultiple()
  157. {
  158. $this->cache->set('key', 'a value');
  159. $this->cache->set('key2', 'other value');
  160. $results = $this->cache->getMultiple(['key', 'key2', 'no']);
  161. $expected = [
  162. 'key' => 'a value',
  163. 'key2' => 'other value',
  164. 'no' => null,
  165. ];
  166. $this->assertSame($expected, $results);
  167. }
  168. /**
  169. * test getMultiple adding defaults in.
  170. *
  171. * @return void
  172. */
  173. public function testGetMultipleDefault()
  174. {
  175. $this->cache->set('key', 'a value');
  176. $this->cache->set('key2', 'other value');
  177. $results = $this->cache->getMultiple(['key', 'key2', 'no'], 'default value');
  178. $expected = [
  179. 'key' => 'a value',
  180. 'key2' => 'other value',
  181. 'no' => 'default value',
  182. ];
  183. $this->assertSame($expected, $results);
  184. }
  185. /**
  186. * test setMultiple
  187. *
  188. * @return void
  189. */
  190. public function testSetMultiple()
  191. {
  192. $data = [
  193. 'key' => 'a value',
  194. 'key2' => 'other value',
  195. ];
  196. $this->cache->setMultiple($data);
  197. $results = $this->cache->getMultiple(array_keys($data));
  198. $this->assertSame($data, $results);
  199. }
  200. /**
  201. * test setMultiple with ttl parameter
  202. *
  203. * @return void
  204. */
  205. public function testSetMultipleWithTtl()
  206. {
  207. $data = [
  208. 'key' => 'a value',
  209. 'key2' => 'other value',
  210. ];
  211. $this->cache->setMultiple($data, 0);
  212. sleep(1);
  213. $results = $this->cache->getMultiple(array_keys($data));
  214. $this->assertNull($results['key']);
  215. $this->assertNull($results['key2']);
  216. $this->assertSame(5, $this->inner->getConfig('duration'));
  217. }
  218. /**
  219. * test deleting multiple keys
  220. *
  221. * @return void
  222. */
  223. public function testDeleteMultiple()
  224. {
  225. $data = [
  226. 'key' => 'a value',
  227. 'key2' => 'other value',
  228. 'key3' => 'more data',
  229. ];
  230. $this->cache->setMultiple($data);
  231. $this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
  232. $this->assertNull($this->cache->get('key'));
  233. $this->assertNull($this->cache->get('key3'));
  234. $this->assertSame('other value', $this->cache->get('key2'));
  235. }
  236. /**
  237. * test partial success with deleteMultiple
  238. *
  239. * @return void
  240. */
  241. public function testDeleteMultipleSomeMisses()
  242. {
  243. $data = [
  244. 'key' => 'a value',
  245. ];
  246. $this->cache->setMultiple($data);
  247. $this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
  248. }
  249. /**
  250. * test has
  251. *
  252. * @return void
  253. */
  254. public function testHas()
  255. {
  256. $this->assertFalse($this->cache->has('key'));
  257. $this->cache->set('key', 'value');
  258. $this->assertTrue($this->cache->has('key'));
  259. }
  260. /**
  261. * test has with invalid key
  262. *
  263. * @return void
  264. */
  265. public function testHasInvalidKey()
  266. {
  267. $this->expectException(InvalidArgumentException::class);
  268. $this->cache->has('');
  269. }
  270. }