CacheTest.php 26 KB

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