SimpleCacheEngineTest.php 11 KB

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