SimpleCacheEngineTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. $result = $this->cache->setMultiple($data);
  271. $this->assertTrue($result);
  272. $results = $this->cache->getMultiple(array_keys($data));
  273. $this->assertEquals($expected, $results);
  274. }
  275. /**
  276. * Test setMultiple with an invalid key
  277. *
  278. * @return void
  279. * @covers ::setMultiple
  280. * @covers ::ensureValidKeys
  281. * @covers ::ensureValidKey
  282. */
  283. public function testSetMultipleInvalidKey()
  284. {
  285. $this->expectException(InvalidArgumentException::class);
  286. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  287. $data = [
  288. '' => 'a value wuth an invalid key',
  289. ];
  290. $this->cache->setMultiple($data);
  291. }
  292. /**
  293. * Test setMultiple with TTL parameter
  294. *
  295. * @return void
  296. * @covers ::setMultiple
  297. * @covers ::ensureValidKeys
  298. */
  299. public function testSetMultipleWithTtl()
  300. {
  301. $data = [
  302. 'key' => 'a value',
  303. 'key2' => 'other value',
  304. ];
  305. $ttl = 0;
  306. $this->cache->setMultiple($data, $ttl);
  307. sleep(1);
  308. $results = $this->cache->getMultiple(array_keys($data));
  309. $this->assertNull($results['key']);
  310. $this->assertNull($results['key2']);
  311. $this->assertSame(5, $this->innerEngine->getConfig('duration'));
  312. }
  313. /**
  314. * Test deleting multiple keys
  315. *
  316. * @return void
  317. * @covers ::deleteMultiple
  318. */
  319. public function testDeleteMultiple()
  320. {
  321. $data = [
  322. 'key' => 'a value',
  323. 'key2' => 'other value',
  324. 'key3' => 'more data',
  325. ];
  326. $this->cache->setMultiple($data);
  327. $this->assertTrue($this->cache->deleteMultiple(['key', 'key3']));
  328. $this->assertNull($this->cache->get('key'));
  329. $this->assertNull($this->cache->get('key3'));
  330. $this->assertSame('other value', $this->cache->get('key2'));
  331. }
  332. /**
  333. * Test deleting multiple keys with an invalid key
  334. *
  335. * @return void
  336. * @covers ::deleteMultiple
  337. * @covers ::ensureValidKeys
  338. * @covers ::ensureValidKey
  339. */
  340. public function testDeleteMultipleInvalidKey()
  341. {
  342. $this->expectException(InvalidArgumentException::class);
  343. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  344. $withInvalidKey = [''];
  345. $this->cache->deleteMultiple($withInvalidKey);
  346. }
  347. /**
  348. * Test deleting multiple keys with an invalid keys parameter
  349. *
  350. * @return void
  351. * @covers ::deleteMultiple
  352. * @covers ::ensureValidKeys
  353. */
  354. public function testDeleteMultipleInvalidKeys()
  355. {
  356. $this->expectException(InvalidArgumentException::class);
  357. $this->expectExceptionMessage('A cache key set must be either an array or a Traversable.');
  358. $notAnArray = 'neither an array nor a Traversable';
  359. $this->cache->deleteMultiple($notAnArray);
  360. }
  361. /**
  362. * Test partial success with deleteMultiple
  363. *
  364. * @return void
  365. * @covers ::deleteMultiple
  366. */
  367. public function testDeleteMultipleSomeMisses()
  368. {
  369. $data = [
  370. 'key' => 'a value',
  371. ];
  372. $this->cache->setMultiple($data);
  373. $this->assertFalse($this->cache->deleteMultiple(['key', 'key3']));
  374. }
  375. /**
  376. * Test has
  377. *
  378. * @return void
  379. * @covers ::has
  380. */
  381. public function testHas()
  382. {
  383. $this->assertFalse($this->cache->has('key'));
  384. $this->cache->set('key', 'value');
  385. $this->assertTrue($this->cache->has('key'));
  386. }
  387. /**
  388. * Test has with invalid key
  389. *
  390. * @return void
  391. * @covers ::has
  392. * @covers ::ensureValidKey
  393. */
  394. public function testHasInvalidKey()
  395. {
  396. $this->expectException(InvalidArgumentException::class);
  397. $this->expectExceptionMessage('A cache key must be a non-empty string.');
  398. $this->cache->has('');
  399. }
  400. /**
  401. * Test pass through on clearGroup()
  402. *
  403. * @return void
  404. */
  405. public function testClearGroup()
  406. {
  407. $this->cache->set('one', 'val');
  408. $this->cache->set('two', 'val 2');
  409. $this->cache->clearGroup('blog');
  410. $this->assertFalse($this->cache->has('one'));
  411. $this->assertFalse($this->cache->has('two'));
  412. }
  413. /**
  414. * Test pass through on increment()
  415. *
  416. * @return void
  417. */
  418. public function testIncrement()
  419. {
  420. $mock = $this->createMock(CacheEngine::class);
  421. $mock->expects($this->once())
  422. ->method('increment')
  423. ->with('key', 2)
  424. ->will($this->returnValue(true));
  425. $cache = new SimpleCacheEngine($mock);
  426. $this->assertTrue($cache->increment('key', 2));
  427. }
  428. /**
  429. * Test pass through on decrement()
  430. *
  431. * @return void
  432. */
  433. public function testDecrement()
  434. {
  435. $mock = $this->createMock(CacheEngine::class);
  436. $mock->expects($this->once())
  437. ->method('decrement')
  438. ->with('key', 2)
  439. ->will($this->returnValue(true));
  440. $cache = new SimpleCacheEngine($mock);
  441. $this->assertTrue($cache->decrement('key', 2));
  442. }
  443. /**
  444. * Test pass through on add()
  445. *
  446. * @return void
  447. */
  448. public function testAdd()
  449. {
  450. $mock = $this->createMock(CacheEngine::class);
  451. $mock->expects($this->once())
  452. ->method('add')
  453. ->with('key', 2)
  454. ->will($this->returnValue(true));
  455. $cache = new SimpleCacheEngine($mock);
  456. $this->assertTrue($cache->add('key', 2));
  457. }
  458. }