SimpleCacheEngineTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. * @coversDefaultClass \Cake\Cache\SimpleCacheEngine
  24. */
  25. class SimpleCacheEngineTest extends TestCase
  26. {
  27. /**
  28. * Setup
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. parent::setUp();
  35. $this->inner = new FileEngine();
  36. $this->inner->init([
  37. 'prefix' => '',
  38. 'path' => TMP . 'tests',
  39. 'duration' => 5,
  40. ]);
  41. $this->cache = new SimpleCacheEngine($this->inner);
  42. }
  43. /**
  44. * Tear down
  45. *
  46. * @return void
  47. */
  48. public function tearDown()
  49. {
  50. parent::tearDown();
  51. $this->inner->clear(false);
  52. }
  53. /**
  54. * Test getting keys
  55. *
  56. * @return void
  57. * @covers ::get
  58. * @covers ::__construct
  59. * @covers ::ensureValidKey
  60. */
  61. public function testGetSuccess()
  62. {
  63. $this->inner->write('key_one', 'Some Value');
  64. $this->assertSame('Some Value', $this->cache->get('key_one'));
  65. $this->assertSame('Some Value', $this->cache->get('key_one', 'default'));
  66. }
  67. /**
  68. * Test get on missing keys
  69. *
  70. * @return void
  71. * @covers ::get
  72. */
  73. public function testGetNoKey()
  74. {
  75. $this->assertSame('default', $this->cache->get('no', 'default'));
  76. $this->assertNull($this->cache->get('no'));
  77. }
  78. /**
  79. * Test get on invalid keys. The PSR spec outlines that an exception
  80. * must be raised.
  81. *
  82. * @return void
  83. * @covers ::get
  84. * @covers ::ensureValidKey
  85. */
  86. public function testGetInvalidKey()
  87. {
  88. $this->expectException(InvalidArgumentException::class);
  89. $this->cache->get('');
  90. }
  91. /**
  92. * Test set() inheriting the default TTL
  93. *
  94. * @return void
  95. * @covers ::set
  96. * @covers ::__construct
  97. */
  98. public function testSetNoTtl()
  99. {
  100. $this->assertTrue($this->cache->set('key', 'a value'));
  101. $this->assertSame('a value', $this->cache->get('key'));
  102. }
  103. /**
  104. * Test the TTL parameter of set()
  105. *
  106. * @return void
  107. * @covers ::set
  108. */
  109. public function testSetWithTtl()
  110. {
  111. $this->assertTrue($this->cache->set('key', 'a value'));
  112. $this->assertTrue($this->cache->set('expired', 'a value', 0));
  113. sleep(1);
  114. $this->assertSame('a value', $this->cache->get('key'));
  115. $this->assertNull($this->cache->get('expired'));
  116. $this->assertSame(5, $this->inner->getConfig('duration'));
  117. }
  118. /**
  119. * Test set() with an invalid key.
  120. *
  121. * @return void
  122. * @covers ::set
  123. * @covers ::ensureValidKey
  124. */
  125. public function testSetInvalidKey()
  126. {
  127. $this->expectException(InvalidArgumentException::class);
  128. $this->cache->set('', 'some data');
  129. }
  130. /**
  131. * Test delete on known and unknown keys
  132. *
  133. * @return void
  134. * @covers ::delete
  135. */
  136. public function testDelete()
  137. {
  138. $this->cache->set('key', 'a value');
  139. $this->assertTrue($this->cache->delete('key'));
  140. $this->assertFalse($this->cache->delete('undefined'));
  141. }
  142. /**
  143. * Test delete on an invalid key
  144. *
  145. * @return void
  146. * @covers ::delete
  147. * @covers ::ensureValidKey
  148. */
  149. public function testDeleteInvalidKey()
  150. {
  151. $this->expectException(InvalidArgumentException::class);
  152. $this->cache->delete('');
  153. }
  154. /**
  155. * Test clearing cache data
  156. *
  157. * @return void
  158. * @covers ::clear
  159. */
  160. public function testClear()
  161. {
  162. $this->cache->set('key', 'a value');
  163. $this->cache->set('key2', 'other value');
  164. $this->assertTrue($this->cache->clear());
  165. $this->assertNull($this->cache->get('key'));
  166. $this->assertNull($this->cache->get('key2'));
  167. }
  168. /**
  169. * Test getMultiple
  170. *
  171. * @return void
  172. * @covers ::getMultiple
  173. */
  174. public function testGetMultiple()
  175. {
  176. $this->cache->set('key', 'a value');
  177. $this->cache->set('key2', 'other value');
  178. $results = $this->cache->getMultiple(['key', 'key2', 'no']);
  179. $expected = [
  180. 'key' => 'a value',
  181. 'key2' => 'other value',
  182. 'no' => null,
  183. ];
  184. $this->assertSame($expected, $results);
  185. }
  186. /**
  187. * Test getting multiple keys with an invalid key
  188. *
  189. * @return void
  190. * @covers ::getMultiple
  191. * @covers ::ensureValidKeys
  192. * @covers ::ensureValidKey
  193. */
  194. public function testGetMultipleInvalidKey()
  195. {
  196. $this->expectException(InvalidArgumentException::class);
  197. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  198. $withInvalidKey = [''];
  199. $this->cache->getMultiple($withInvalidKey);
  200. }
  201. /**
  202. * Test getting multiple keys with an invalid keys parameter
  203. *
  204. * @return void
  205. * @covers ::getMultiple
  206. * @covers ::ensureValidKeys
  207. */
  208. public function testGetMultipleInvalidKeys()
  209. {
  210. $this->expectException(InvalidArgumentException::class);
  211. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  212. $notAnArray = 'neither an array nor a Traversable';
  213. $this->cache->getMultiple($notAnArray);
  214. }
  215. /**
  216. * Test getMultiple adding defaults in.
  217. *
  218. * @return void
  219. * @covers ::getMultiple
  220. */
  221. public function testGetMultipleDefault()
  222. {
  223. $this->cache->set('key', 'a value');
  224. $this->cache->set('key2', 'other value');
  225. $results = $this->cache->getMultiple(['key', 'key2', 'no'], 'default value');
  226. $expected = [
  227. 'key' => 'a value',
  228. 'key2' => 'other value',
  229. 'no' => 'default value',
  230. ];
  231. $this->assertSame($expected, $results);
  232. }
  233. /**
  234. * Test setMultiple
  235. *
  236. * @return void
  237. * @covers ::setMultiple
  238. */
  239. public function testSetMultiple()
  240. {
  241. $data = [
  242. 'key' => 'a value',
  243. 'key2' => 'other value',
  244. ];
  245. $this->cache->setMultiple($data);
  246. $results = $this->cache->getMultiple(array_keys($data));
  247. $this->assertSame($data, $results);
  248. }
  249. /**
  250. * Test setMultiple with an invalid key
  251. *
  252. * @return void
  253. * @covers ::setMultiple
  254. * @covers ::ensureValidKeys
  255. * @covers ::ensureValidKey
  256. */
  257. public function testSetMultipleInvalidKey()
  258. {
  259. $this->expectException(InvalidArgumentException::class);
  260. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  261. $data = [
  262. '' => 'a value wuth an invalid key',
  263. ];
  264. $this->cache->setMultiple($data);
  265. }
  266. /**
  267. * Test setMultiple with ttl parameter
  268. *
  269. * @return void
  270. * @covers ::setMultiple
  271. * @covers ::ensureValidKeys
  272. */
  273. public function testSetMultipleWithTtl()
  274. {
  275. $data = [
  276. 'key' => 'a value',
  277. 'key2' => 'other value',
  278. ];
  279. $this->cache->setMultiple($data, 0);
  280. sleep(1);
  281. $results = $this->cache->getMultiple(array_keys($data));
  282. $this->assertNull($results['key']);
  283. $this->assertNull($results['key2']);
  284. $this->assertSame(5, $this->inner->getConfig('duration'));
  285. }
  286. /**
  287. * Test deleting multiple keys
  288. *
  289. * @return void
  290. * @covers ::deleteMultiple
  291. */
  292. public function testDeleteMultiple()
  293. {
  294. $data = [
  295. 'key' => 'a value',
  296. 'key2' => 'other value',
  297. 'key3' => 'more data',
  298. ];
  299. $this->cache->setMultiple($data);
  300. $this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
  301. $this->assertNull($this->cache->get('key'));
  302. $this->assertNull($this->cache->get('key3'));
  303. $this->assertSame('other value', $this->cache->get('key2'));
  304. }
  305. /**
  306. * Test deleting multiple keys with an invalid key
  307. *
  308. * @return void
  309. * @covers ::deleteMultiple
  310. * @covers ::ensureValidKeys
  311. * @covers ::ensureValidKey
  312. */
  313. public function testDeleteMultipleInvalidKey()
  314. {
  315. $this->expectException(InvalidArgumentException::class);
  316. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  317. $withInvalidKey = [''];
  318. $this->cache->deleteMultiple($withInvalidKey);
  319. }
  320. /**
  321. * Test deleting multiple keys with an invalid keys parameter
  322. *
  323. * @return void
  324. * @covers ::deleteMultiple
  325. * @covers ::ensureValidKeys
  326. */
  327. public function testDeleteMultipleInvalidKeys()
  328. {
  329. $this->expectException(InvalidArgumentException::class);
  330. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  331. $notAnArray = 'neither an array nor a Traversable';
  332. $this->cache->deleteMultiple($notAnArray);
  333. }
  334. /**
  335. * Test partial success with deleteMultiple
  336. *
  337. * @return void
  338. * @covers ::deleteMultiple
  339. */
  340. public function testDeleteMultipleSomeMisses()
  341. {
  342. $data = [
  343. 'key' => 'a value',
  344. ];
  345. $this->cache->setMultiple($data);
  346. $this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
  347. }
  348. /**
  349. * Test has
  350. *
  351. * @return void
  352. * @covers ::has
  353. */
  354. public function testHas()
  355. {
  356. $this->assertFalse($this->cache->has('key'));
  357. $this->cache->set('key', 'value');
  358. $this->assertTrue($this->cache->has('key'));
  359. }
  360. /**
  361. * Test has with invalid key
  362. *
  363. * @return void
  364. * @covers ::has
  365. * @covers ::ensureValidKey
  366. */
  367. public function testHasInvalidKey()
  368. {
  369. $this->expectException(InvalidArgumentException::class);
  370. $this->cache->has('');
  371. }
  372. }