SimpleCacheEngineTest.php 11 KB

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