CacheTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Cache;
  17. use BadMethodCallException;
  18. use Cake\Cache\Cache;
  19. use Cake\Cache\CacheRegistry;
  20. use Cake\Cache\Engine\FileEngine;
  21. use Cake\Cache\Engine\NullEngine;
  22. use Cake\Cache\InvalidArgumentException;
  23. use Cake\TestSuite\TestCase;
  24. use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
  25. use stdClass;
  26. /**
  27. * CacheTest class
  28. */
  29. class CacheTest extends TestCase
  30. {
  31. /**
  32. * setUp method
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. Cache::enable();
  38. }
  39. /**
  40. * tearDown method
  41. */
  42. public function tearDown(): void
  43. {
  44. parent::tearDown();
  45. Cache::drop('tests');
  46. Cache::drop('test_trigger');
  47. }
  48. /**
  49. * Configure cache settings for test
  50. */
  51. protected function _configCache(): void
  52. {
  53. Cache::setConfig('tests', [
  54. 'engine' => 'File',
  55. 'path' => CACHE,
  56. 'prefix' => 'test_',
  57. ]);
  58. }
  59. /**
  60. * tests Cache::pool() fallback
  61. */
  62. public function testCachePoolFallback(): void
  63. {
  64. $filename = tempnam(CACHE, 'tmp_');
  65. Cache::setConfig('tests', [
  66. 'engine' => 'File',
  67. 'path' => $filename,
  68. 'prefix' => 'test_',
  69. 'fallback' => 'tests_fallback',
  70. ]);
  71. Cache::setConfig('tests_fallback', [
  72. 'engine' => 'File',
  73. 'path' => CACHE,
  74. 'prefix' => 'test_',
  75. ]);
  76. $engine = Cache::pool('tests');
  77. $path = $engine->getConfig('path');
  78. $this->assertSame(CACHE, $path);
  79. Cache::drop('tests');
  80. Cache::drop('tests_fallback');
  81. unlink($filename);
  82. }
  83. /**
  84. * tests you can disable Cache::pool() fallback
  85. */
  86. public function testCachePoolFallbackDisabled(): void
  87. {
  88. $filename = tempnam(CACHE, 'tmp_');
  89. Cache::setConfig('tests', [
  90. 'engine' => 'File',
  91. 'path' => $filename,
  92. 'prefix' => 'test_',
  93. 'fallback' => false,
  94. ]);
  95. $this->expectError();
  96. Cache::pool('tests');
  97. }
  98. /**
  99. * tests handling misconfiguration of fallback
  100. */
  101. public function testCacheEngineFallbackToSelf(): void
  102. {
  103. $filename = tempnam(CACHE, 'tmp_');
  104. Cache::setConfig('tests', [
  105. 'engine' => 'File',
  106. 'path' => $filename,
  107. 'prefix' => 'test_',
  108. 'fallback' => 'tests',
  109. ]);
  110. $e = null;
  111. try {
  112. Cache::pool('tests');
  113. } catch (InvalidArgumentException $e) {
  114. }
  115. Cache::drop('tests');
  116. unlink($filename);
  117. $this->assertNotNull($e);
  118. $this->assertStringEndsWith('cannot fallback to itself.', $e->getMessage());
  119. $this->assertInstanceOf('RunTimeException', $e->getPrevious());
  120. }
  121. /**
  122. * tests Cache::pool() fallback when using groups
  123. */
  124. public function testCacheFallbackWithGroups(): void
  125. {
  126. $filename = tempnam(CACHE, 'tmp_');
  127. Cache::setConfig('tests', [
  128. 'engine' => 'File',
  129. 'path' => $filename,
  130. 'prefix' => 'test_',
  131. 'fallback' => 'tests_fallback',
  132. 'groups' => ['group1', 'group2'],
  133. ]);
  134. Cache::setConfig('tests_fallback', [
  135. 'engine' => 'File',
  136. 'path' => CACHE,
  137. 'prefix' => 'test_',
  138. 'groups' => ['group3', 'group1'],
  139. ]);
  140. $result = Cache::groupConfigs('group1');
  141. $this->assertSame(['group1' => ['tests', 'tests_fallback']], $result);
  142. $result = Cache::groupConfigs('group2');
  143. $this->assertSame(['group2' => ['tests']], $result);
  144. Cache::drop('tests');
  145. Cache::drop('tests_fallback');
  146. unlink($filename);
  147. }
  148. /**
  149. * tests cache fallback
  150. */
  151. public function testCacheFallbackIntegration(): void
  152. {
  153. $filename = tempnam(CACHE, 'tmp_');
  154. Cache::setConfig('tests', [
  155. 'engine' => 'File',
  156. 'path' => $filename,
  157. 'fallback' => 'tests_fallback',
  158. 'groups' => ['integration_group', 'integration_group_2'],
  159. ]);
  160. Cache::setConfig('tests_fallback', [
  161. 'engine' => 'File',
  162. 'path' => $filename,
  163. 'fallback' => 'tests_fallback_final',
  164. 'groups' => ['integration_group'],
  165. ]);
  166. Cache::setConfig('tests_fallback_final', [
  167. 'engine' => 'File',
  168. 'path' => CACHE . 'cake_test' . DS,
  169. 'groups' => ['integration_group_3'],
  170. ]);
  171. $this->assertTrue(Cache::write('grouped', 'worked', 'tests'));
  172. $this->assertTrue(Cache::write('grouped_2', 'worked', 'tests_fallback'));
  173. $this->assertTrue(Cache::write('grouped_3', 'worked', 'tests_fallback_final'));
  174. $this->assertTrue(Cache::clearGroup('integration_group', 'tests'));
  175. $this->assertNull(Cache::read('grouped', 'tests'));
  176. $this->assertNull(Cache::read('grouped_2', 'tests_fallback'));
  177. $this->assertSame('worked', Cache::read('grouped_3', 'tests_fallback_final'));
  178. Cache::drop('tests');
  179. Cache::drop('tests_fallback');
  180. Cache::drop('tests_fallback_final');
  181. unlink($filename);
  182. }
  183. /**
  184. * Check that no fatal errors are issued doing normal things when Cache.disable is true.
  185. */
  186. public function testNonFatalErrorsWithCacheDisable(): void
  187. {
  188. Cache::disable();
  189. $this->_configCache();
  190. $this->assertTrue(Cache::write('no_save', 'Noooo!', 'tests'));
  191. $this->assertNull(Cache::read('no_save', 'tests'));
  192. $this->assertTrue(Cache::delete('no_save', 'tests'));
  193. }
  194. /**
  195. * Check that a null instance is returned from engine() when caching is disabled.
  196. */
  197. public function testNullEngineWhenCacheDisable(): void
  198. {
  199. $this->_configCache();
  200. Cache::disable();
  201. $result = Cache::pool('tests');
  202. $this->assertInstanceOf(NullEngine::class, $result);
  203. }
  204. /**
  205. * Test configuring an invalid class fails
  206. */
  207. public function testConfigInvalidClassType(): void
  208. {
  209. Cache::setConfig('tests', [
  210. 'className' => '\stdClass',
  211. ]);
  212. $this->expectError();
  213. $this->expectErrorMessage('Cache engines must use Cake\Cache\CacheEngine');
  214. Cache::pool('tests');
  215. }
  216. /**
  217. * Test engine init failing triggers an error but falls back to NullEngine
  218. */
  219. public function testConfigFailedInit(): void
  220. {
  221. $mock = $this->getMockForAbstractClass('Cake\Cache\CacheEngine', [], '', true, true, true, ['init']);
  222. $mock->method('init')->will($this->returnValue(false));
  223. Cache::setConfig('tests', [
  224. 'engine' => $mock,
  225. ]);
  226. $this->expectError();
  227. $this->expectErrorMessage('is not properly configured');
  228. Cache::pool('tests');
  229. }
  230. /**
  231. * test configuring CacheEngines in App/libs
  232. */
  233. public function testConfigWithLibAndPluginEngines(): void
  234. {
  235. static::setAppNamespace();
  236. $this->loadPlugins(['TestPlugin']);
  237. $config = ['engine' => 'TestAppCache', 'path' => CACHE, 'prefix' => 'cake_test_'];
  238. Cache::setConfig('libEngine', $config);
  239. $engine = Cache::pool('libEngine');
  240. $this->assertInstanceOf('TestApp\Cache\Engine\TestAppCacheEngine', $engine);
  241. $config = ['engine' => 'TestPlugin.TestPluginCache', 'path' => CACHE, 'prefix' => 'cake_test_'];
  242. Cache::setConfig('pluginLibEngine', $config);
  243. $engine = Cache::pool('pluginLibEngine');
  244. $this->assertInstanceOf('TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine);
  245. Cache::drop('libEngine');
  246. Cache::drop('pluginLibEngine');
  247. $this->clearPlugins();
  248. }
  249. /**
  250. * Test write from a config that is undefined.
  251. */
  252. public function testWriteNonExistentConfig(): void
  253. {
  254. $this->expectException(InvalidArgumentException::class);
  255. Cache::write('key', 'value', 'totally fake');
  256. }
  257. /**
  258. * Test write from a config that is undefined.
  259. */
  260. public function testIncrementNonExistentConfig(): void
  261. {
  262. $this->expectException(InvalidArgumentException::class);
  263. Cache::increment('key', 1, 'totally fake');
  264. }
  265. /**
  266. * Test increment with value < 0
  267. */
  268. public function testIncrementSubZero(): void
  269. {
  270. $this->expectException(InvalidArgumentException::class);
  271. Cache::increment('key', -1);
  272. }
  273. /**
  274. * Test write from a config that is undefined.
  275. */
  276. public function testDecrementNonExistentConfig(): void
  277. {
  278. $this->expectException(InvalidArgumentException::class);
  279. Cache::decrement('key', 1, 'totally fake');
  280. }
  281. /**
  282. * Test decrement value < 0
  283. */
  284. public function testDecrementSubZero(): void
  285. {
  286. $this->expectException(InvalidArgumentException::class);
  287. Cache::decrement('key', -1);
  288. }
  289. /**
  290. * Data provider for valid config data sets.
  291. *
  292. * @return array
  293. */
  294. public static function configProvider(): array
  295. {
  296. return [
  297. 'Array of data using engine key.' => [[
  298. 'engine' => 'File',
  299. 'path' => CACHE . 'tests',
  300. 'prefix' => 'cake_test_',
  301. ]],
  302. 'Array of data using classname key.' => [[
  303. 'className' => 'File',
  304. 'path' => CACHE . 'tests',
  305. 'prefix' => 'cake_test_',
  306. ]],
  307. 'Direct instance' => [new FileEngine()],
  308. ];
  309. }
  310. /**
  311. * testConfig method
  312. *
  313. * @dataProvider configProvider
  314. * @param \Cake\Cache\CacheEngine|array $config
  315. */
  316. public function testConfigVariants($config): void
  317. {
  318. $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
  319. Cache::setConfig('tests', $config);
  320. $engine = Cache::pool('tests');
  321. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  322. $this->assertContains('tests', Cache::configured());
  323. }
  324. /**
  325. * testConfigInvalidEngine method
  326. */
  327. public function testConfigInvalidEngine(): void
  328. {
  329. $config = ['engine' => 'Imaginary'];
  330. Cache::setConfig('test', $config);
  331. $this->expectException(BadMethodCallException::class);
  332. Cache::pool('test');
  333. }
  334. /**
  335. * test that trying to configure classes that don't extend CacheEngine fail.
  336. */
  337. public function testConfigInvalidObject(): void
  338. {
  339. $this->getMockBuilder(stdClass::class)
  340. ->setMockClassName('RubbishEngine')
  341. ->getMock();
  342. $this->expectException(BadMethodCallException::class);
  343. Cache::setConfig('test', [
  344. 'engine' => '\RubbishEngine',
  345. ]);
  346. }
  347. /**
  348. * Ensure you cannot reconfigure a cache adapter.
  349. */
  350. public function testConfigErrorOnReconfigure(): void
  351. {
  352. Cache::setConfig('tests', ['engine' => 'File', 'path' => CACHE]);
  353. $this->expectException(BadMethodCallException::class);
  354. Cache::setConfig('tests', ['engine' => 'Apc']);
  355. }
  356. /**
  357. * Test reading configuration.
  358. */
  359. public function testConfigRead(): void
  360. {
  361. $config = [
  362. 'engine' => 'File',
  363. 'path' => CACHE,
  364. 'prefix' => 'cake_',
  365. ];
  366. Cache::setConfig('tests', $config);
  367. $expected = $config;
  368. $expected['className'] = $config['engine'];
  369. unset($expected['engine']);
  370. $this->assertEquals($expected, Cache::getConfig('tests'));
  371. }
  372. /**
  373. * Test reading configuration with numeric string.
  374. */
  375. public function testConfigReadNumeric(): void
  376. {
  377. $config = [
  378. 'engine' => 'File',
  379. 'path' => CACHE,
  380. 'prefix' => 'cake_',
  381. ];
  382. Cache::setConfig('123', $config);
  383. $expected = $config;
  384. $expected['className'] = $config['engine'];
  385. unset($expected['engine']);
  386. $this->assertEquals($expected, Cache::getConfig('123'));
  387. }
  388. /**
  389. * test config() with dotted name
  390. */
  391. public function testConfigDottedAlias(): void
  392. {
  393. Cache::setConfig('cache.dotted', [
  394. 'className' => 'File',
  395. 'path' => CACHE,
  396. 'prefix' => 'cache_value_',
  397. ]);
  398. $engine = Cache::pool('cache.dotted');
  399. $this->assertContains('cache.dotted', Cache::configured());
  400. $this->assertNotContains('dotted', Cache::configured());
  401. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  402. Cache::drop('cache.dotted');
  403. }
  404. /**
  405. * testGroupConfigs method
  406. */
  407. public function testGroupConfigs(): void
  408. {
  409. Cache::drop('test');
  410. Cache::setConfig('latest', [
  411. 'duration' => 300,
  412. 'engine' => 'File',
  413. 'groups' => ['posts', 'comments'],
  414. ]);
  415. $result = Cache::groupConfigs();
  416. $this->assertArrayHasKey('posts', $result);
  417. $this->assertContains('latest', $result['posts']);
  418. $this->assertArrayHasKey('comments', $result);
  419. $this->assertContains('latest', $result['comments']);
  420. $result = Cache::groupConfigs('posts');
  421. $this->assertEquals(['posts' => ['latest']], $result);
  422. Cache::setConfig('page', [
  423. 'duration' => 86400,
  424. 'engine' => 'File',
  425. 'groups' => ['posts', 'archive'],
  426. ]);
  427. $result = Cache::groupConfigs();
  428. $this->assertArrayHasKey('posts', $result);
  429. $this->assertContains('latest', $result['posts']);
  430. $this->assertContains('page', $result['posts']);
  431. $this->assertArrayHasKey('comments', $result);
  432. $this->assertContains('latest', $result['comments']);
  433. $this->assertNotContains('page', $result['comments']);
  434. $this->assertArrayHasKey('archive', $result);
  435. $this->assertContains('page', $result['archive']);
  436. $this->assertNotContains('latest', $result['archive']);
  437. $result = Cache::groupConfigs('archive');
  438. $this->assertEquals(['archive' => ['page']], $result);
  439. Cache::setConfig('archive', [
  440. 'duration' => 86400 * 30,
  441. 'engine' => 'File',
  442. 'groups' => ['posts', 'archive', 'comments'],
  443. ]);
  444. $result = Cache::groupConfigs('archive');
  445. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  446. }
  447. /**
  448. * testGroupConfigsWithCacheInstance method
  449. */
  450. public function testGroupConfigsWithCacheInstance(): void
  451. {
  452. Cache::drop('test');
  453. $cache = new FileEngine();
  454. $cache->init([
  455. 'duration' => 300,
  456. 'engine' => 'File',
  457. 'groups' => ['users', 'comments'],
  458. ]);
  459. Cache::setConfig('cached', $cache);
  460. $result = Cache::groupConfigs('users');
  461. $this->assertEquals(['users' => ['cached']], $result);
  462. }
  463. /**
  464. * testGroupConfigsThrowsException method
  465. */
  466. public function testGroupConfigsThrowsException(): void
  467. {
  468. $this->expectException(InvalidArgumentException::class);
  469. Cache::groupConfigs('bogus');
  470. }
  471. /**
  472. * test that configured returns an array of the currently configured cache
  473. * config
  474. */
  475. public function testConfigured(): void
  476. {
  477. Cache::drop('default');
  478. $result = Cache::configured();
  479. $this->assertContains('_cake_core_', $result);
  480. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  481. }
  482. /**
  483. * test that drop removes cache configs, and that further attempts to use that config
  484. * do not work.
  485. */
  486. public function testDrop(): void
  487. {
  488. static::setAppNamespace();
  489. $result = Cache::drop('some_config_that_does_not_exist');
  490. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  491. Cache::setConfig('unconfigTest', [
  492. 'engine' => 'TestAppCache',
  493. ]);
  494. $this->assertInstanceOf(
  495. 'TestApp\Cache\Engine\TestAppCacheEngine',
  496. Cache::pool('unconfigTest')
  497. );
  498. $this->assertTrue(Cache::drop('unconfigTest'));
  499. }
  500. /**
  501. * testWriteEmptyValues method
  502. */
  503. public function testWriteEmptyValues(): void
  504. {
  505. $this->_configCache();
  506. Cache::write('App.falseTest', false, 'tests');
  507. $this->assertFalse(Cache::read('App.falseTest', 'tests'));
  508. Cache::write('App.trueTest', true, 'tests');
  509. $this->assertTrue(Cache::read('App.trueTest', 'tests'));
  510. Cache::write('App.nullTest', null, 'tests');
  511. $this->assertNull(Cache::read('App.nullTest', 'tests'));
  512. Cache::write('App.zeroTest', 0, 'tests');
  513. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  514. Cache::write('App.zeroTest2', '0', 'tests');
  515. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  516. }
  517. /**
  518. * testWriteEmptyValues method
  519. */
  520. public function testWriteEmptyKey(): void
  521. {
  522. $this->_configCache();
  523. $this->expectException(InvalidArgumentException::class);
  524. $this->expectExceptionMessage('A cache key must be a non-empty string');
  525. Cache::write('', 'not null', 'tests');
  526. }
  527. /**
  528. * testReadWriteMany method
  529. */
  530. public function testReadWriteMany(): void
  531. {
  532. $this->_configCache();
  533. $data = [
  534. 'App.falseTest' => false,
  535. 'App.trueTest' => true,
  536. 'App.nullTest' => null,
  537. 'App.zeroTest' => 0,
  538. 'App.zeroTest2' => '0',
  539. ];
  540. Cache::writeMany($data, 'tests');
  541. $read = Cache::readMany(array_keys($data), 'tests');
  542. $this->assertFalse($read['App.falseTest']);
  543. $this->assertTrue($read['App.trueTest']);
  544. $this->assertNull($read['App.nullTest']);
  545. $this->assertSame($read['App.zeroTest'], 0);
  546. $this->assertSame($read['App.zeroTest2'], '0');
  547. }
  548. /**
  549. * testDeleteMany method
  550. */
  551. public function testDeleteMany(): void
  552. {
  553. $this->_configCache();
  554. $data = [
  555. 'App.falseTest' => false,
  556. 'App.trueTest' => true,
  557. 'App.nullTest' => null,
  558. 'App.zeroTest' => 0,
  559. 'App.zeroTest2' => '0',
  560. ];
  561. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  562. Cache::deleteMany(array_keys($data), 'tests');
  563. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  564. $this->assertNull($read['App.falseTest']);
  565. $this->assertNull($read['App.trueTest']);
  566. $this->assertNull($read['App.nullTest']);
  567. $this->assertNull($read['App.zeroTest']);
  568. $this->assertNull($read['App.zeroTest2']);
  569. $this->assertSame($read['App.keepTest'], 'keepMe');
  570. }
  571. /**
  572. * testDeleteMany partial failure
  573. */
  574. public function testDeleteManyPartialFailure(): void
  575. {
  576. $this->_configCache();
  577. $data = [
  578. 'App.exists' => 'yes',
  579. 'App.exists2' => 'yes',
  580. ];
  581. Cache::writeMany($data, 'tests');
  582. $result = Cache::deleteMany(['App.exists', 'App.noExists', 'App.exists2'], 'tests');
  583. $this->assertFalse($result);
  584. $this->assertNull(Cache::read('App.exists', 'tests'));
  585. $this->assertNull(Cache::read('App.exists2', 'tests'));
  586. }
  587. /**
  588. * Test that failed writes cause errors to be triggered.
  589. */
  590. public function testWriteTriggerError(): void
  591. {
  592. static::setAppNamespace();
  593. Cache::setConfig('test_trigger', [
  594. 'engine' => 'TestAppCache',
  595. 'prefix' => '',
  596. ]);
  597. $this->expectError();
  598. Cache::write('fail', 'value', 'test_trigger');
  599. }
  600. /**
  601. * testCacheDisable method
  602. *
  603. * Check that the "Cache.disable" configuration and a change to it
  604. * (even after a cache config has been setup) is taken into account.
  605. */
  606. public function testCacheDisable(): void
  607. {
  608. Cache::enable();
  609. Cache::setConfig('test_cache_disable_1', [
  610. 'engine' => 'File',
  611. 'path' => CACHE . 'tests',
  612. ]);
  613. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  614. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  615. Cache::disable();
  616. $this->assertTrue(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  617. $this->assertNull(Cache::read('key_2', 'test_cache_disable_1'));
  618. Cache::enable();
  619. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  620. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  621. Cache::clear('test_cache_disable_1');
  622. Cache::disable();
  623. Cache::setConfig('test_cache_disable_2', [
  624. 'engine' => 'File',
  625. 'path' => CACHE . 'tests',
  626. ]);
  627. $this->assertTrue(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  628. $this->assertNull(Cache::read('key_4', 'test_cache_disable_2'));
  629. Cache::enable();
  630. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  631. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  632. Cache::disable();
  633. $this->assertTrue(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  634. $this->assertNull(Cache::read('key_6', 'test_cache_disable_2'));
  635. Cache::enable();
  636. Cache::clear('test_cache_disable_2');
  637. }
  638. /**
  639. * test clearAll() method
  640. */
  641. public function testClearAll(): void
  642. {
  643. Cache::setConfig('configTest', [
  644. 'engine' => 'File',
  645. 'path' => CACHE . 'tests',
  646. ]);
  647. Cache::setConfig('anotherConfigTest', [
  648. 'engine' => 'File',
  649. 'path' => CACHE . 'tests',
  650. ]);
  651. Cache::write('key_1', 'hello', 'configTest');
  652. Cache::write('key_2', 'hello again', 'anotherConfigTest');
  653. $this->assertSame(Cache::read('key_1', 'configTest'), 'hello');
  654. $this->assertSame(Cache::read('key_2', 'anotherConfigTest'), 'hello again');
  655. $result = Cache::clearAll();
  656. $this->assertTrue($result['configTest']);
  657. $this->assertTrue($result['anotherConfigTest']);
  658. $this->assertNull(Cache::read('key_1', 'configTest'));
  659. $this->assertNull(Cache::read('key_2', 'anotherConfigTest'));
  660. Cache::drop('configTest');
  661. Cache::drop('anotherConfigTest');
  662. }
  663. /**
  664. * Test toggling enabled state of cache.
  665. */
  666. public function testEnableDisableEnabled(): void
  667. {
  668. Cache::enable();
  669. $this->assertTrue(Cache::enabled(), 'Should be on');
  670. Cache::disable();
  671. $this->assertFalse(Cache::enabled(), 'Should be off');
  672. }
  673. /**
  674. * test remember method.
  675. */
  676. public function testRemember(): void
  677. {
  678. $this->_configCache();
  679. $counter = 0;
  680. $cacher = function () use ($counter) {
  681. return 'This is some data ' . $counter;
  682. };
  683. $expected = 'This is some data 0';
  684. $result = Cache::remember('test_key', $cacher, 'tests');
  685. $this->assertSame($expected, $result);
  686. $counter = 1;
  687. $result = Cache::remember('test_key', $cacher, 'tests');
  688. $this->assertSame($expected, $result);
  689. }
  690. /**
  691. * Test add method.
  692. */
  693. public function testAdd(): void
  694. {
  695. $this->_configCache();
  696. Cache::delete('test_add_key', 'tests');
  697. $result = Cache::add('test_add_key', 'test data', 'tests');
  698. $this->assertTrue($result);
  699. $expected = 'test data';
  700. $result = Cache::read('test_add_key', 'tests');
  701. $this->assertSame($expected, $result);
  702. $result = Cache::add('test_add_key', 'test data 2', 'tests');
  703. $this->assertFalse($result);
  704. }
  705. /**
  706. * Test getting the registry
  707. */
  708. public function testGetRegistry(): void
  709. {
  710. $this->assertInstanceOf(CacheRegistry::class, Cache::getRegistry());
  711. }
  712. /**
  713. * Test setting the registry
  714. */
  715. public function testSetAndGetRegistry(): void
  716. {
  717. $registry = new CacheRegistry();
  718. Cache::setRegistry($registry);
  719. $this->assertSame($registry, Cache::getRegistry());
  720. }
  721. /**
  722. * Test getting instances with pool
  723. */
  724. public function testPool(): void
  725. {
  726. $this->_configCache();
  727. $pool = Cache::pool('tests');
  728. $this->assertInstanceOf(SimpleCacheInterface::class, $pool);
  729. }
  730. /**
  731. * Test getting instances with pool
  732. */
  733. public function testPoolCacheDisabled(): void
  734. {
  735. Cache::disable();
  736. $pool = Cache::pool('tests');
  737. $this->assertInstanceOf(SimpleCacheInterface::class, $pool);
  738. }
  739. }