SimpleCacheEngineTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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->expectExceptionMessage('A cache key must be a non-empty string.');
  90. $this->cache->get('');
  91. }
  92. /**
  93. * Test set() inheriting the default TTL
  94. *
  95. * @return void
  96. * @covers ::set
  97. * @covers ::__construct
  98. */
  99. public function testSetNoTtl()
  100. {
  101. $this->assertTrue($this->cache->set('key', 'a value'));
  102. $this->assertSame('a value', $this->cache->get('key'));
  103. }
  104. /**
  105. * Test the TTL parameter of set()
  106. *
  107. * @return void
  108. * @covers ::set
  109. */
  110. public function testSetWithTtl()
  111. {
  112. $this->assertTrue($this->cache->set('key', 'a value'));
  113. $this->assertTrue($this->cache->set('expired', 'a value', 0));
  114. sleep(1);
  115. $this->assertSame('a value', $this->cache->get('key'));
  116. $this->assertNull($this->cache->get('expired'));
  117. $this->assertSame(5, $this->inner->getConfig('duration'));
  118. }
  119. /**
  120. * Test set() with an invalid key.
  121. *
  122. * @return void
  123. * @covers ::set
  124. * @covers ::ensureValidKey
  125. */
  126. public function testSetInvalidKey()
  127. {
  128. $this->expectException(InvalidArgumentException::class);
  129. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  130. $this->cache->set('', 'some data');
  131. }
  132. /**
  133. * Test delete on known and unknown keys
  134. *
  135. * @return void
  136. * @covers ::delete
  137. */
  138. public function testDelete()
  139. {
  140. $this->cache->set('key', 'a value');
  141. $this->assertTrue($this->cache->delete('key'));
  142. $this->assertFalse($this->cache->delete('undefined'));
  143. }
  144. /**
  145. * Test delete on an invalid key
  146. *
  147. * @return void
  148. * @covers ::delete
  149. * @covers ::ensureValidKey
  150. */
  151. public function testDeleteInvalidKey()
  152. {
  153. $this->expectException(InvalidArgumentException::class);
  154. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  155. $this->cache->delete('');
  156. }
  157. /**
  158. * Test clearing cache data
  159. *
  160. * @return void
  161. * @covers ::clear
  162. */
  163. public function testClear()
  164. {
  165. $this->cache->set('key', 'a value');
  166. $this->cache->set('key2', 'other value');
  167. $this->assertTrue($this->cache->clear());
  168. $this->assertNull($this->cache->get('key'));
  169. $this->assertNull($this->cache->get('key2'));
  170. }
  171. /**
  172. * Test getMultiple
  173. *
  174. * @return void
  175. * @covers ::getMultiple
  176. */
  177. public function testGetMultiple()
  178. {
  179. $this->cache->set('key', 'a value');
  180. $this->cache->set('key2', 'other value');
  181. $results = $this->cache->getMultiple(['key', 'key2', 'no']);
  182. $expected = [
  183. 'key' => 'a value',
  184. 'key2' => 'other value',
  185. 'no' => null,
  186. ];
  187. $this->assertSame($expected, $results);
  188. }
  189. /**
  190. * Test getting multiple keys with an invalid key
  191. *
  192. * @return void
  193. * @covers ::getMultiple
  194. * @covers ::ensureValidKeys
  195. * @covers ::ensureValidKey
  196. */
  197. public function testGetMultipleInvalidKey()
  198. {
  199. $this->expectException(InvalidArgumentException::class);
  200. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  201. $withInvalidKey = [''];
  202. $this->cache->getMultiple($withInvalidKey);
  203. }
  204. /**
  205. * Test getting multiple keys with an invalid keys parameter
  206. *
  207. * @return void
  208. * @covers ::getMultiple
  209. * @covers ::ensureValidKeys
  210. */
  211. public function testGetMultipleInvalidKeys()
  212. {
  213. $this->expectException(InvalidArgumentException::class);
  214. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  215. $notAnArray = 'neither an array nor a Traversable';
  216. $this->cache->getMultiple($notAnArray);
  217. }
  218. /**
  219. * Test getMultiple adding defaults in.
  220. *
  221. * @return void
  222. * @covers ::getMultiple
  223. */
  224. public function testGetMultipleDefault()
  225. {
  226. $this->cache->set('key', 'a value');
  227. $this->cache->set('key2', 'other value');
  228. $results = $this->cache->getMultiple(['key', 'key2', 'no'], 'default value');
  229. $expected = [
  230. 'key' => 'a value',
  231. 'key2' => 'other value',
  232. 'no' => 'default value',
  233. ];
  234. $this->assertSame($expected, $results);
  235. }
  236. /**
  237. * Test setMultiple
  238. *
  239. * @return void
  240. * @covers ::setMultiple
  241. */
  242. public function testSetMultiple()
  243. {
  244. $data = [
  245. 'key' => 'a value',
  246. 'key2' => 'other value',
  247. ];
  248. $this->cache->setMultiple($data);
  249. $results = $this->cache->getMultiple(array_keys($data));
  250. $this->assertSame($data, $results);
  251. }
  252. /**
  253. * Test setMultiple with an invalid key
  254. *
  255. * @return void
  256. * @covers ::setMultiple
  257. * @covers ::ensureValidKeys
  258. * @covers ::ensureValidKey
  259. */
  260. public function testSetMultipleInvalidKey()
  261. {
  262. $this->expectException(InvalidArgumentException::class);
  263. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  264. $data = [
  265. '' => 'a value wuth an invalid key',
  266. ];
  267. $this->cache->setMultiple($data);
  268. }
  269. /**
  270. * Test setMultiple with ttl parameter
  271. *
  272. * @return void
  273. * @covers ::setMultiple
  274. * @covers ::ensureValidKeys
  275. */
  276. public function testSetMultipleWithTtl()
  277. {
  278. $data = [
  279. 'key' => 'a value',
  280. 'key2' => 'other value',
  281. ];
  282. $this->cache->setMultiple($data, 0);
  283. sleep(1);
  284. $results = $this->cache->getMultiple(array_keys($data));
  285. $this->assertNull($results['key']);
  286. $this->assertNull($results['key2']);
  287. $this->assertSame(5, $this->inner->getConfig('duration'));
  288. }
  289. /**
  290. * Test deleting multiple keys
  291. *
  292. * @return void
  293. * @covers ::deleteMultiple
  294. */
  295. public function testDeleteMultiple()
  296. {
  297. $data = [
  298. 'key' => 'a value',
  299. 'key2' => 'other value',
  300. 'key3' => 'more data',
  301. ];
  302. $this->cache->setMultiple($data);
  303. $this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
  304. $this->assertNull($this->cache->get('key'));
  305. $this->assertNull($this->cache->get('key3'));
  306. $this->assertSame('other value', $this->cache->get('key2'));
  307. }
  308. /**
  309. * Test deleting multiple keys with an invalid key
  310. *
  311. * @return void
  312. * @covers ::deleteMultiple
  313. * @covers ::ensureValidKeys
  314. * @covers ::ensureValidKey
  315. */
  316. public function testDeleteMultipleInvalidKey()
  317. {
  318. $this->expectException(InvalidArgumentException::class);
  319. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  320. $withInvalidKey = [''];
  321. $this->cache->deleteMultiple($withInvalidKey);
  322. }
  323. /**
  324. * Test deleting multiple keys with an invalid keys parameter
  325. *
  326. * @return void
  327. * @covers ::deleteMultiple
  328. * @covers ::ensureValidKeys
  329. */
  330. public function testDeleteMultipleInvalidKeys()
  331. {
  332. $this->expectException(InvalidArgumentException::class);
  333. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  334. $notAnArray = 'neither an array nor a Traversable';
  335. $this->cache->deleteMultiple($notAnArray);
  336. }
  337. /**
  338. * Test partial success with deleteMultiple
  339. *
  340. * @return void
  341. * @covers ::deleteMultiple
  342. */
  343. public function testDeleteMultipleSomeMisses()
  344. {
  345. $data = [
  346. 'key' => 'a value',
  347. ];
  348. $this->cache->setMultiple($data);
  349. $this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
  350. }
  351. /**
  352. * Test has
  353. *
  354. * @return void
  355. * @covers ::has
  356. */
  357. public function testHas()
  358. {
  359. $this->assertFalse($this->cache->has('key'));
  360. $this->cache->set('key', 'value');
  361. $this->assertTrue($this->cache->has('key'));
  362. }
  363. /**
  364. * Test has with invalid key
  365. *
  366. * @return void
  367. * @covers ::has
  368. * @covers ::ensureValidKey
  369. */
  370. public function testHasInvalidKey()
  371. {
  372. $this->expectException(InvalidArgumentException::class);
  373. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  374. $this->cache->has('');
  375. }
  376. }