SimpleCacheEngineTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. 'groups' => ['blog', 'category']
  54. ]);
  55. $this->cache = new SimpleCacheEngine($this->innerEngine);
  56. }
  57. /**
  58. * Tear down
  59. *
  60. * @return void
  61. */
  62. public function tearDown()
  63. {
  64. parent::tearDown();
  65. $this->innerEngine->clear(false);
  66. }
  67. /**
  68. * Test getting keys
  69. *
  70. * @return void
  71. * @covers ::get
  72. * @covers ::__construct
  73. * @covers ::ensureValidKey
  74. */
  75. public function testGetSuccess()
  76. {
  77. $this->innerEngine->write('key_one', 'Some Value');
  78. $this->assertSame('Some Value', $this->cache->get('key_one'));
  79. $this->assertSame('Some Value', $this->cache->get('key_one', 'default'));
  80. }
  81. /**
  82. * Test get on missing keys
  83. *
  84. * @return void
  85. * @covers ::get
  86. */
  87. public function testGetNoKey()
  88. {
  89. $this->assertSame('default', $this->cache->get('no', 'default'));
  90. $this->assertNull($this->cache->get('no'));
  91. }
  92. /**
  93. * Test get on invalid keys. The PSR spec outlines that an exception
  94. * must be raised.
  95. *
  96. * @return void
  97. * @covers ::get
  98. * @covers ::ensureValidKey
  99. */
  100. public function testGetInvalidKey()
  101. {
  102. $this->expectException(InvalidArgumentException::class);
  103. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  104. $this->cache->get('');
  105. }
  106. /**
  107. * Test set() inheriting the default TTL
  108. *
  109. * @return void
  110. * @covers ::set
  111. * @covers ::__construct
  112. */
  113. public function testSetNoTtl()
  114. {
  115. $this->assertTrue($this->cache->set('key', 'a value'));
  116. $this->assertSame('a value', $this->cache->get('key'));
  117. }
  118. /**
  119. * Test the TTL parameter of set()
  120. *
  121. * @return void
  122. * @covers ::set
  123. */
  124. public function testSetWithTtl()
  125. {
  126. $this->assertTrue($this->cache->set('key', 'a value'));
  127. $ttl = 0;
  128. $this->assertTrue($this->cache->set('expired', 'a value', $ttl));
  129. sleep(1);
  130. $this->assertSame('a value', $this->cache->get('key'));
  131. $this->assertNull($this->cache->get('expired'));
  132. $this->assertSame(5, $this->innerEngine->getConfig('duration'));
  133. }
  134. /**
  135. * Test set() with an invalid key.
  136. *
  137. * @return void
  138. * @covers ::set
  139. * @covers ::ensureValidKey
  140. */
  141. public function testSetInvalidKey()
  142. {
  143. $this->expectException(InvalidArgumentException::class);
  144. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  145. $this->cache->set('', 'some data');
  146. }
  147. /**
  148. * Test delete on known and unknown keys
  149. *
  150. * @return void
  151. * @covers ::delete
  152. */
  153. public function testDelete()
  154. {
  155. $this->cache->set('key', 'a value');
  156. $this->assertTrue($this->cache->delete('key'));
  157. $this->assertFalse($this->cache->delete('undefined'));
  158. }
  159. /**
  160. * Test delete on an invalid key
  161. *
  162. * @return void
  163. * @covers ::delete
  164. * @covers ::ensureValidKey
  165. */
  166. public function testDeleteInvalidKey()
  167. {
  168. $this->expectException(InvalidArgumentException::class);
  169. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  170. $this->cache->delete('');
  171. }
  172. /**
  173. * Test clearing cache data
  174. *
  175. * @return void
  176. * @covers ::clear
  177. */
  178. public function testClear()
  179. {
  180. $this->cache->set('key', 'a value');
  181. $this->cache->set('key2', 'other value');
  182. $this->assertTrue($this->cache->clear());
  183. $this->assertNull($this->cache->get('key'));
  184. $this->assertNull($this->cache->get('key2'));
  185. }
  186. /**
  187. * Test getMultiple
  188. *
  189. * @return void
  190. * @covers ::getMultiple
  191. */
  192. public function testGetMultiple()
  193. {
  194. $this->cache->set('key', 'a value');
  195. $this->cache->set('key2', 'other value');
  196. $results = $this->cache->getMultiple(['key', 'key2', 'no']);
  197. $expected = [
  198. 'key' => 'a value',
  199. 'key2' => 'other value',
  200. 'no' => null,
  201. ];
  202. $this->assertSame($expected, $results);
  203. }
  204. /**
  205. * Test getting multiple keys with an invalid key
  206. *
  207. * @return void
  208. * @covers ::getMultiple
  209. * @covers ::ensureValidKeys
  210. * @covers ::ensureValidKey
  211. */
  212. public function testGetMultipleInvalidKey()
  213. {
  214. $this->expectException(InvalidArgumentException::class);
  215. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  216. $withInvalidKey = [''];
  217. $this->cache->getMultiple($withInvalidKey);
  218. }
  219. /**
  220. * Test getting multiple keys with an invalid keys parameter
  221. *
  222. * @return void
  223. * @covers ::getMultiple
  224. * @covers ::ensureValidKeys
  225. */
  226. public function testGetMultipleInvalidKeys()
  227. {
  228. $this->expectException(InvalidArgumentException::class);
  229. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  230. $notAnArray = 'neither an array nor a Traversable';
  231. $this->cache->getMultiple($notAnArray);
  232. }
  233. /**
  234. * Test getMultiple adding defaults in.
  235. *
  236. * @return void
  237. * @covers ::getMultiple
  238. */
  239. public function testGetMultipleDefault()
  240. {
  241. $this->cache->set('key', 'a value');
  242. $this->cache->set('key2', 'other value');
  243. $results = $this->cache->getMultiple(['key', 'key2', 'no'], 'default value');
  244. $expected = [
  245. 'key' => 'a value',
  246. 'key2' => 'other value',
  247. 'no' => 'default value',
  248. ];
  249. $this->assertSame($expected, $results);
  250. }
  251. /**
  252. * Test setMultiple
  253. *
  254. * We should not assert for array equality, as the PSR-16 specs
  255. * do not make any guarantees on key order.
  256. *
  257. * @return void
  258. * @covers ::setMultiple
  259. */
  260. public function testSetMultiple()
  261. {
  262. $data = [
  263. 'key' => 'a value',
  264. 'key2' => 'other value',
  265. ];
  266. $expected = [
  267. 'key2' => 'other value',
  268. 'key' => 'a value',
  269. ];
  270. $this->cache->setMultiple($data);
  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. /**
  400. * Test pass through on clearGroup()
  401. *
  402. * @return void
  403. */
  404. public function testClearGroup()
  405. {
  406. $this->cache->set('one', 'val');
  407. $this->cache->set('two', 'val 2');
  408. $this->cache->clearGroup('blog');
  409. $this->assertFalse($this->cache->has('one'));
  410. $this->assertFalse($this->cache->has('two'));
  411. }
  412. /**
  413. * Test pass through on increment()
  414. *
  415. * @return void
  416. */
  417. public function testIncrement()
  418. {
  419. $mock = $this->createMock(CacheEngine::class);
  420. $mock->expects($this->once())
  421. ->method('increment')
  422. ->with('key', 2)
  423. ->will($this->returnValue(true));
  424. $cache = new SimpleCacheEngine($mock);
  425. $this->assertTrue($cache->increment('key', 2));
  426. }
  427. /**
  428. * Test pass through on decrement()
  429. *
  430. * @return void
  431. */
  432. public function testDecrement()
  433. {
  434. $mock = $this->createMock(CacheEngine::class);
  435. $mock->expects($this->once())
  436. ->method('decrement')
  437. ->with('key', 2)
  438. ->will($this->returnValue(true));
  439. $cache = new SimpleCacheEngine($mock);
  440. $this->assertTrue($cache->decrement('key', 2));
  441. }
  442. }