CacheTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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 testWriteNonExistingConfig()
  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 testIncrementNonExistingConfig()
  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 testDecrementNonExistingConfig()
  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. * @return void
  349. */
  350. public function testConfigVariants($config)
  351. {
  352. $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
  353. Cache::setConfig('tests', $config);
  354. $engine = Cache::pool('tests');
  355. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  356. $this->assertContains('tests', Cache::configured());
  357. }
  358. /**
  359. * testConfigInvalidEngine method
  360. *
  361. * @return void
  362. */
  363. public function testConfigInvalidEngine()
  364. {
  365. $config = ['engine' => 'Imaginary'];
  366. Cache::setConfig('test', $config);
  367. $this->expectException(\BadMethodCallException::class);
  368. Cache::pool('test');
  369. }
  370. /**
  371. * test that trying to configure classes that don't extend CacheEngine fail.
  372. *
  373. * @return void
  374. */
  375. public function testConfigInvalidObject()
  376. {
  377. $this->getMockBuilder(\stdClass::class)
  378. ->setMockClassName('RubbishEngine')
  379. ->getMock();
  380. $this->expectException(\BadMethodCallException::class);
  381. Cache::setConfig('test', [
  382. 'engine' => '\RubbishEngine',
  383. ]);
  384. }
  385. /**
  386. * Ensure you cannot reconfigure a cache adapter.
  387. *
  388. * @return void
  389. */
  390. public function testConfigErrorOnReconfigure()
  391. {
  392. Cache::setConfig('tests', ['engine' => 'File', 'path' => CACHE]);
  393. $this->expectException(\BadMethodCallException::class);
  394. Cache::setConfig('tests', ['engine' => 'Apc']);
  395. }
  396. /**
  397. * Test reading configuration.
  398. *
  399. * @return void
  400. */
  401. public function testConfigRead()
  402. {
  403. $config = [
  404. 'engine' => 'File',
  405. 'path' => CACHE,
  406. 'prefix' => 'cake_',
  407. ];
  408. Cache::setConfig('tests', $config);
  409. $expected = $config;
  410. $expected['className'] = $config['engine'];
  411. unset($expected['engine']);
  412. $this->assertEquals($expected, Cache::getConfig('tests'));
  413. }
  414. /**
  415. * Test reading configuration with numeric string.
  416. *
  417. * @return void
  418. */
  419. public function testConfigReadNumeric()
  420. {
  421. $config = [
  422. 'engine' => 'File',
  423. 'path' => CACHE,
  424. 'prefix' => 'cake_',
  425. ];
  426. Cache::setConfig('123', $config);
  427. $expected = $config;
  428. $expected['className'] = $config['engine'];
  429. unset($expected['engine']);
  430. $this->assertEquals($expected, Cache::getConfig('123'));
  431. }
  432. /**
  433. * test config() with dotted name
  434. *
  435. * @return void
  436. */
  437. public function testConfigDottedAlias()
  438. {
  439. Cache::setConfig('cache.dotted', [
  440. 'className' => 'File',
  441. 'path' => CACHE,
  442. 'prefix' => 'cache_value_',
  443. ]);
  444. $engine = Cache::pool('cache.dotted');
  445. $this->assertContains('cache.dotted', Cache::configured());
  446. $this->assertNotContains('dotted', Cache::configured());
  447. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  448. Cache::drop('cache.dotted');
  449. }
  450. /**
  451. * testGroupConfigs method
  452. */
  453. public function testGroupConfigs()
  454. {
  455. Cache::drop('test');
  456. Cache::setConfig('latest', [
  457. 'duration' => 300,
  458. 'engine' => 'File',
  459. 'groups' => ['posts', 'comments'],
  460. ]);
  461. $result = Cache::groupConfigs();
  462. $this->assertArrayHasKey('posts', $result);
  463. $this->assertContains('latest', $result['posts']);
  464. $this->assertArrayHasKey('comments', $result);
  465. $this->assertContains('latest', $result['comments']);
  466. $result = Cache::groupConfigs('posts');
  467. $this->assertEquals(['posts' => ['latest']], $result);
  468. Cache::setConfig('page', [
  469. 'duration' => 86400,
  470. 'engine' => 'File',
  471. 'groups' => ['posts', 'archive'],
  472. ]);
  473. $result = Cache::groupConfigs();
  474. $this->assertArrayHasKey('posts', $result);
  475. $this->assertContains('latest', $result['posts']);
  476. $this->assertContains('page', $result['posts']);
  477. $this->assertArrayHasKey('comments', $result);
  478. $this->assertContains('latest', $result['comments']);
  479. $this->assertNotContains('page', $result['comments']);
  480. $this->assertArrayHasKey('archive', $result);
  481. $this->assertContains('page', $result['archive']);
  482. $this->assertNotContains('latest', $result['archive']);
  483. $result = Cache::groupConfigs('archive');
  484. $this->assertEquals(['archive' => ['page']], $result);
  485. Cache::setConfig('archive', [
  486. 'duration' => 86400 * 30,
  487. 'engine' => 'File',
  488. 'groups' => ['posts', 'archive', 'comments'],
  489. ]);
  490. $result = Cache::groupConfigs('archive');
  491. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  492. }
  493. /**
  494. * testGroupConfigsWithCacheInstance method
  495. */
  496. public function testGroupConfigsWithCacheInstance()
  497. {
  498. Cache::drop('test');
  499. $cache = new FileEngine();
  500. $cache->init([
  501. 'duration' => 300,
  502. 'engine' => 'File',
  503. 'groups' => ['users', 'comments'],
  504. ]);
  505. Cache::setConfig('cached', $cache);
  506. $result = Cache::groupConfigs('users');
  507. $this->assertEquals(['users' => ['cached']], $result);
  508. }
  509. /**
  510. * testGroupConfigsThrowsException method
  511. */
  512. public function testGroupConfigsThrowsException()
  513. {
  514. $this->expectException(InvalidArgumentException::class);
  515. Cache::groupConfigs('bogus');
  516. }
  517. /**
  518. * test that configured returns an array of the currently configured cache
  519. * config
  520. *
  521. * @return void
  522. */
  523. public function testConfigured()
  524. {
  525. Cache::drop('default');
  526. $result = Cache::configured();
  527. $this->assertContains('_cake_core_', $result);
  528. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  529. }
  530. /**
  531. * test that drop removes cache configs, and that further attempts to use that config
  532. * do not work.
  533. *
  534. * @return void
  535. */
  536. public function testDrop()
  537. {
  538. static::setAppNamespace();
  539. $result = Cache::drop('some_config_that_does_not_exist');
  540. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  541. Cache::setConfig('unconfigTest', [
  542. 'engine' => 'TestAppCache',
  543. ]);
  544. $this->assertInstanceOf(
  545. 'TestApp\Cache\Engine\TestAppCacheEngine',
  546. Cache::pool('unconfigTest')
  547. );
  548. $this->assertTrue(Cache::drop('unconfigTest'));
  549. }
  550. /**
  551. * testWriteEmptyValues method
  552. *
  553. * @return void
  554. */
  555. public function testWriteEmptyValues()
  556. {
  557. $this->_configCache();
  558. Cache::write('App.falseTest', false, 'tests');
  559. $this->assertFalse(Cache::read('App.falseTest', 'tests'));
  560. Cache::write('App.trueTest', true, 'tests');
  561. $this->assertTrue(Cache::read('App.trueTest', 'tests'));
  562. Cache::write('App.nullTest', null, 'tests');
  563. $this->assertNull(Cache::read('App.nullTest', 'tests'));
  564. Cache::write('App.zeroTest', 0, 'tests');
  565. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  566. Cache::write('App.zeroTest2', '0', 'tests');
  567. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  568. }
  569. /**
  570. * testWriteEmptyValues method
  571. *
  572. * @return void
  573. */
  574. public function testWriteEmptyKey()
  575. {
  576. $this->_configCache();
  577. $this->expectException(InvalidArgumentException::class);
  578. $this->expectExceptionMessage('A cache key must be a non-empty string');
  579. Cache::write('', 'not null', 'tests');
  580. }
  581. /**
  582. * testReadWriteMany method
  583. *
  584. * @return void
  585. */
  586. public function testReadWriteMany()
  587. {
  588. $this->_configCache();
  589. $data = [
  590. 'App.falseTest' => false,
  591. 'App.trueTest' => true,
  592. 'App.nullTest' => null,
  593. 'App.zeroTest' => 0,
  594. 'App.zeroTest2' => '0',
  595. ];
  596. Cache::writeMany($data, 'tests');
  597. $read = Cache::readMany(array_keys($data), 'tests');
  598. $this->assertFalse($read['App.falseTest']);
  599. $this->assertTrue($read['App.trueTest']);
  600. $this->assertNull($read['App.nullTest']);
  601. $this->assertSame($read['App.zeroTest'], 0);
  602. $this->assertSame($read['App.zeroTest2'], '0');
  603. }
  604. /**
  605. * testDeleteMany method
  606. *
  607. * @return void
  608. */
  609. public function testDeleteMany()
  610. {
  611. $this->_configCache();
  612. $data = [
  613. 'App.falseTest' => false,
  614. 'App.trueTest' => true,
  615. 'App.nullTest' => null,
  616. 'App.zeroTest' => 0,
  617. 'App.zeroTest2' => '0',
  618. ];
  619. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  620. Cache::deleteMany(array_keys($data), 'tests');
  621. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  622. $this->assertNull($read['App.falseTest']);
  623. $this->assertNull($read['App.trueTest']);
  624. $this->assertNull($read['App.nullTest']);
  625. $this->assertNull($read['App.zeroTest']);
  626. $this->assertNull($read['App.zeroTest2']);
  627. $this->assertSame($read['App.keepTest'], 'keepMe');
  628. }
  629. /**
  630. * Test that failed writes cause errors to be triggered.
  631. *
  632. * @return void
  633. */
  634. public function testWriteTriggerError()
  635. {
  636. static::setAppNamespace();
  637. Cache::setConfig('test_trigger', [
  638. 'engine' => 'TestAppCache',
  639. 'prefix' => '',
  640. ]);
  641. $this->expectError();
  642. Cache::write('fail', 'value', 'test_trigger');
  643. }
  644. /**
  645. * testCacheDisable method
  646. *
  647. * Check that the "Cache.disable" configuration and a change to it
  648. * (even after a cache config has been setup) is taken into account.
  649. *
  650. * @return void
  651. */
  652. public function testCacheDisable()
  653. {
  654. Cache::enable();
  655. Cache::setConfig('test_cache_disable_1', [
  656. 'engine' => 'File',
  657. 'path' => CACHE . 'tests',
  658. ]);
  659. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  660. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  661. Cache::disable();
  662. $this->assertTrue(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  663. $this->assertNull(Cache::read('key_2', 'test_cache_disable_1'));
  664. Cache::enable();
  665. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  666. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  667. Cache::clear('test_cache_disable_1');
  668. Cache::disable();
  669. Cache::setConfig('test_cache_disable_2', [
  670. 'engine' => 'File',
  671. 'path' => CACHE . 'tests',
  672. ]);
  673. $this->assertTrue(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  674. $this->assertNull(Cache::read('key_4', 'test_cache_disable_2'));
  675. Cache::enable();
  676. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  677. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  678. Cache::disable();
  679. $this->assertTrue(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  680. $this->assertNull(Cache::read('key_6', 'test_cache_disable_2'));
  681. Cache::enable();
  682. Cache::clear('test_cache_disable_2');
  683. }
  684. /**
  685. * test clearAll() method
  686. *
  687. * @return void
  688. */
  689. public function testClearAll()
  690. {
  691. Cache::setConfig('configTest', [
  692. 'engine' => 'File',
  693. 'path' => CACHE . 'tests',
  694. ]);
  695. Cache::setConfig('anotherConfigTest', [
  696. 'engine' => 'File',
  697. 'path' => CACHE . 'tests',
  698. ]);
  699. Cache::write('key_1', 'hello', 'configTest');
  700. Cache::write('key_2', 'hello again', 'anotherConfigTest');
  701. $this->assertSame(Cache::read('key_1', 'configTest'), 'hello');
  702. $this->assertSame(Cache::read('key_2', 'anotherConfigTest'), 'hello again');
  703. $result = Cache::clearAll();
  704. $this->assertTrue($result['configTest']);
  705. $this->assertTrue($result['anotherConfigTest']);
  706. $this->assertNull(Cache::read('key_1', 'configTest'));
  707. $this->assertNull(Cache::read('key_2', 'anotherConfigTest'));
  708. Cache::drop('configTest');
  709. Cache::drop('anotherConfigTest');
  710. }
  711. /**
  712. * Test toggling enabled state of cache.
  713. *
  714. * @return void
  715. */
  716. public function testEnableDisableEnabled()
  717. {
  718. Cache::enable();
  719. $this->assertTrue(Cache::enabled(), 'Should be on');
  720. Cache::disable();
  721. $this->assertFalse(Cache::enabled(), 'Should be off');
  722. }
  723. /**
  724. * test remember method.
  725. *
  726. * @return void
  727. */
  728. public function testRemember()
  729. {
  730. $this->_configCache();
  731. $counter = 0;
  732. $cacher = function () use ($counter) {
  733. return 'This is some data ' . $counter;
  734. };
  735. $expected = 'This is some data 0';
  736. $result = Cache::remember('test_key', $cacher, 'tests');
  737. $this->assertEquals($expected, $result);
  738. $counter = 1;
  739. $result = Cache::remember('test_key', $cacher, 'tests');
  740. $this->assertEquals($expected, $result);
  741. }
  742. /**
  743. * Test add method.
  744. *
  745. * @return void
  746. */
  747. public function testAdd()
  748. {
  749. $this->_configCache();
  750. Cache::delete('test_add_key', 'tests');
  751. $result = Cache::add('test_add_key', 'test data', 'tests');
  752. $this->assertTrue($result);
  753. $expected = 'test data';
  754. $result = Cache::read('test_add_key', 'tests');
  755. $this->assertEquals($expected, $result);
  756. $result = Cache::add('test_add_key', 'test data 2', 'tests');
  757. $this->assertFalse($result);
  758. }
  759. /**
  760. * Test getting the registry
  761. *
  762. * @return void
  763. */
  764. public function testGetRegistry()
  765. {
  766. $this->assertInstanceOf(CacheRegistry::class, Cache::getRegistry());
  767. }
  768. /**
  769. * Test setting the registry
  770. *
  771. * @return void
  772. */
  773. public function testSetAndGetRegistry()
  774. {
  775. $registry = new CacheRegistry();
  776. Cache::setRegistry($registry);
  777. $this->assertSame($registry, Cache::getRegistry());
  778. }
  779. /**
  780. * Test getting instances with pool
  781. *
  782. * @return void
  783. */
  784. public function testPool()
  785. {
  786. $this->_configCache();
  787. $pool = Cache::pool('tests');
  788. $this->assertInstanceOf(SimpleCacheInterface::class, $pool);
  789. }
  790. /**
  791. * Test getting instances with pool
  792. *
  793. * @return void
  794. */
  795. public function testPoolCacheDisabled()
  796. {
  797. Cache::disable();
  798. $pool = Cache::pool('tests');
  799. $this->assertInstanceOf(SimpleCacheInterface::class, $pool);
  800. }
  801. }