CacheTest.php 25 KB

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