CacheTest.php 26 KB

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