SimpleCacheEngineTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. $this->cache->setMultiple($data);
  270. $results = $this->cache->getMultiple(array_keys($data));
  271. $this->assertEquals($expected, $results);
  272. }
  273. /**
  274. * Test setMultiple with an invalid key
  275. *
  276. * @return void
  277. * @covers ::setMultiple
  278. * @covers ::ensureValidKeys
  279. * @covers ::ensureValidKey
  280. */
  281. public function testSetMultipleInvalidKey()
  282. {
  283. $this->expectException(InvalidArgumentException::class);
  284. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  285. $data = [
  286. '' => 'a value wuth an invalid key',
  287. ];
  288. $this->cache->setMultiple($data);
  289. }
  290. /**
  291. * Test setMultiple with ttl parameter
  292. *
  293. * @return void
  294. * @covers ::setMultiple
  295. * @covers ::ensureValidKeys
  296. */
  297. public function testSetMultipleWithTtl()
  298. {
  299. $data = [
  300. 'key' => 'a value',
  301. 'key2' => 'other value',
  302. ];
  303. $ttl = 0;
  304. $this->cache->setMultiple($data, $ttl);
  305. sleep(1);
  306. $results = $this->cache->getMultiple(array_keys($data));
  307. $this->assertNull($results['key']);
  308. $this->assertNull($results['key2']);
  309. $this->assertSame(5, $this->innerEngine->getConfig('duration'));
  310. }
  311. /**
  312. * Test deleting multiple keys
  313. *
  314. * @return void
  315. * @covers ::deleteMultiple
  316. */
  317. public function testDeleteMultiple()
  318. {
  319. $data = [
  320. 'key' => 'a value',
  321. 'key2' => 'other value',
  322. 'key3' => 'more data',
  323. ];
  324. $this->cache->setMultiple($data);
  325. $this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
  326. $this->assertNull($this->cache->get('key'));
  327. $this->assertNull($this->cache->get('key3'));
  328. $this->assertSame('other value', $this->cache->get('key2'));
  329. }
  330. /**
  331. * Test deleting multiple keys with an invalid key
  332. *
  333. * @return void
  334. * @covers ::deleteMultiple
  335. * @covers ::ensureValidKeys
  336. * @covers ::ensureValidKey
  337. */
  338. public function testDeleteMultipleInvalidKey()
  339. {
  340. $this->expectException(InvalidArgumentException::class);
  341. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  342. $withInvalidKey = [''];
  343. $this->cache->deleteMultiple($withInvalidKey);
  344. }
  345. /**
  346. * Test deleting multiple keys with an invalid keys parameter
  347. *
  348. * @return void
  349. * @covers ::deleteMultiple
  350. * @covers ::ensureValidKeys
  351. */
  352. public function testDeleteMultipleInvalidKeys()
  353. {
  354. $this->expectException(InvalidArgumentException::class);
  355. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  356. $notAnArray = 'neither an array nor a Traversable';
  357. $this->cache->deleteMultiple($notAnArray);
  358. }
  359. /**
  360. * Test partial success with deleteMultiple
  361. *
  362. * @return void
  363. * @covers ::deleteMultiple
  364. */
  365. public function testDeleteMultipleSomeMisses()
  366. {
  367. $data = [
  368. 'key' => 'a value',
  369. ];
  370. $this->cache->setMultiple($data);
  371. $this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
  372. }
  373. /**
  374. * Test has
  375. *
  376. * @return void
  377. * @covers ::has
  378. */
  379. public function testHas()
  380. {
  381. $this->assertFalse($this->cache->has('key'));
  382. $this->cache->set('key', 'value');
  383. $this->assertTrue($this->cache->has('key'));
  384. }
  385. /**
  386. * Test has with invalid key
  387. *
  388. * @return void
  389. * @covers ::has
  390. * @covers ::ensureValidKey
  391. */
  392. public function testHasInvalidKey()
  393. {
  394. $this->expectException(InvalidArgumentException::class);
  395. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  396. $this->cache->has('');
  397. }
  398. }