CacheTest.php 26 KB

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