CacheTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Cache;
  16. use Cake\Cache\Cache;
  17. use Cake\Cache\CacheRegistry;
  18. use Cake\Cache\Engine\FileEngine;
  19. use Cake\Cache\Engine\NullEngine;
  20. use Cake\Core\Plugin;
  21. use Cake\TestSuite\TestCase;
  22. use InvalidArgumentException;
  23. use PHPUnit\Framework\Error\Error;
  24. /**
  25. * CacheTest class
  26. */
  27. class CacheTest extends TestCase
  28. {
  29. /**
  30. * setUp method
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. parent::setUp();
  37. Cache::enable();
  38. }
  39. /**
  40. * tearDown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  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' => TMP,
  60. 'prefix' => 'test_'
  61. ]);
  62. }
  63. /**
  64. * tests Cache::engine() fallback
  65. *
  66. * @return void
  67. */
  68. public function testCacheEngineFallback()
  69. {
  70. $filename = tempnam(TMP, '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' => TMP,
  80. 'prefix' => 'test_',
  81. ]);
  82. $engine = Cache::engine('tests');
  83. $path = $engine->getConfig('path');
  84. $this->assertSame(TMP, $path);
  85. Cache::drop('tests');
  86. Cache::drop('tests_fallback');
  87. unlink($filename);
  88. }
  89. /**
  90. * tests you can disable Cache::engine() fallback
  91. *
  92. * @return void
  93. */
  94. public function testCacheEngineFallbackDisabled()
  95. {
  96. $this->expectException(Error::class);
  97. $filename = tempnam(TMP, 'tmp_');
  98. Cache::setConfig('tests', [
  99. 'engine' => 'File',
  100. 'path' => $filename,
  101. 'prefix' => 'test_',
  102. 'fallback' => false
  103. ]);
  104. $engine = Cache::engine('tests');
  105. }
  106. /**
  107. * tests handling misconfiguration of fallback
  108. *
  109. * @return void
  110. */
  111. public function testCacheEngineFallbackToSelf()
  112. {
  113. $filename = tempnam(TMP, '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::engine('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::engine() fallback when using groups
  133. *
  134. * @return void
  135. */
  136. public function testCacheFallbackWithGroups()
  137. {
  138. $filename = tempnam(TMP, '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' => TMP,
  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(TMP, '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' => TMP . '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->assertFalse(Cache::read('grouped', 'tests'));
  190. $this->assertFalse(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->assertNull(Cache::write('no_save', 'Noooo!', 'tests'));
  207. $this->assertFalse(Cache::read('no_save', 'tests'));
  208. $this->assertNull(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::engine('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. $this->expectException(Error::class);
  230. $this->expectExceptionMessage('Cache engines must use Cake\Cache\CacheEngine');
  231. Cache::setConfig('tests', [
  232. 'className' => '\StdClass'
  233. ]);
  234. $result = Cache::engine('tests');
  235. $this->assertInstanceOf(NullEngine::class, $result);
  236. }
  237. /**
  238. * Test engine init failing triggers an error but falls back to NullEngine
  239. *
  240. * @return void
  241. */
  242. public function testConfigFailedInit()
  243. {
  244. $this->expectException(Error::class);
  245. $this->expectExceptionMessage('is not properly configured');
  246. $mock = $this->getMockForAbstractClass('Cake\Cache\CacheEngine', [], '', true, true, true, ['init']);
  247. $mock->method('init')->will($this->returnValue(false));
  248. Cache::setConfig('tests', [
  249. 'engine' => $mock
  250. ]);
  251. $result = Cache::engine('tests');
  252. $this->assertInstanceOf(NullEngine::class, $result);
  253. }
  254. /**
  255. * test configuring CacheEngines in App/libs
  256. *
  257. * @return void
  258. */
  259. public function testConfigWithLibAndPluginEngines()
  260. {
  261. static::setAppNamespace();
  262. Plugin::load('TestPlugin');
  263. $config = ['engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  264. Cache::setConfig('libEngine', $config);
  265. $engine = Cache::engine('libEngine');
  266. $this->assertInstanceOf('TestApp\Cache\Engine\TestAppCacheEngine', $engine);
  267. $config = ['engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  268. $result = Cache::setConfig('pluginLibEngine', $config);
  269. $engine = Cache::engine('pluginLibEngine');
  270. $this->assertInstanceOf('TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine);
  271. Cache::drop('libEngine');
  272. Cache::drop('pluginLibEngine');
  273. Plugin::unload();
  274. }
  275. /**
  276. * Test write from a config that is undefined.
  277. *
  278. * @return void
  279. */
  280. public function testWriteNonExistingConfig()
  281. {
  282. $this->expectException(\InvalidArgumentException::class);
  283. $this->assertFalse(Cache::write('key', 'value', 'totally fake'));
  284. }
  285. /**
  286. * Test write from a config that is undefined.
  287. *
  288. * @return void
  289. */
  290. public function testIncrementNonExistingConfig()
  291. {
  292. $this->expectException(\InvalidArgumentException::class);
  293. $this->assertFalse(Cache::increment('key', 1, 'totally fake'));
  294. }
  295. /**
  296. * Test write from a config that is undefined.
  297. *
  298. * @return void
  299. */
  300. public function testDecrementNonExistingConfig()
  301. {
  302. $this->expectException(\InvalidArgumentException::class);
  303. $this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
  304. }
  305. /**
  306. * Data provider for valid config data sets.
  307. *
  308. * @return array
  309. */
  310. public static function configProvider()
  311. {
  312. return [
  313. 'Array of data using engine key.' => [[
  314. 'engine' => 'File',
  315. 'path' => TMP . 'tests',
  316. 'prefix' => 'cake_test_'
  317. ]],
  318. 'Array of data using classname key.' => [[
  319. 'className' => 'File',
  320. 'path' => TMP . 'tests',
  321. 'prefix' => 'cake_test_'
  322. ]],
  323. 'Direct instance' => [new FileEngine()],
  324. ];
  325. }
  326. /**
  327. * testConfig method
  328. *
  329. * @dataProvider configProvider
  330. * @return void
  331. */
  332. public function testConfigVariants($config)
  333. {
  334. $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
  335. Cache::setConfig('tests', $config);
  336. $engine = Cache::engine('tests');
  337. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  338. $this->assertContains('tests', Cache::configured());
  339. }
  340. /**
  341. * testConfigInvalidEngine method
  342. *
  343. * @return void
  344. */
  345. public function testConfigInvalidEngine()
  346. {
  347. $this->expectException(\BadMethodCallException::class);
  348. $config = ['engine' => 'Imaginary'];
  349. Cache::setConfig('test', $config);
  350. Cache::engine('test');
  351. }
  352. /**
  353. * test that trying to configure classes that don't extend CacheEngine fail.
  354. *
  355. * @return void
  356. */
  357. public function testConfigInvalidObject()
  358. {
  359. $this->expectException(\BadMethodCallException::class);
  360. $this->getMockBuilder(\StdClass::class)
  361. ->setMockClassName('RubbishEngine')
  362. ->getMock();
  363. Cache::setConfig('test', [
  364. 'engine' => '\RubbishEngine'
  365. ]);
  366. Cache::engine('tests');
  367. }
  368. /**
  369. * Ensure you cannot reconfigure a cache adapter.
  370. *
  371. * @return void
  372. */
  373. public function testConfigErrorOnReconfigure()
  374. {
  375. $this->expectException(\BadMethodCallException::class);
  376. Cache::setConfig('tests', ['engine' => 'File', 'path' => TMP]);
  377. Cache::setConfig('tests', ['engine' => 'Apc']);
  378. }
  379. /**
  380. * Test reading configuration.
  381. *
  382. * @return void
  383. */
  384. public function testConfigRead()
  385. {
  386. $config = [
  387. 'engine' => 'File',
  388. 'path' => TMP,
  389. 'prefix' => 'cake_'
  390. ];
  391. Cache::setConfig('tests', $config);
  392. $expected = $config;
  393. $expected['className'] = $config['engine'];
  394. unset($expected['engine']);
  395. $this->assertEquals($expected, Cache::getConfig('tests'));
  396. }
  397. /**
  398. * test config() with dotted name
  399. *
  400. * @return void
  401. */
  402. public function testConfigDottedAlias()
  403. {
  404. Cache::setConfig('cache.dotted', [
  405. 'className' => 'File',
  406. 'path' => TMP,
  407. 'prefix' => 'cache_value_'
  408. ]);
  409. $engine = Cache::engine('cache.dotted');
  410. $this->assertContains('cache.dotted', Cache::configured());
  411. $this->assertNotContains('dotted', Cache::configured());
  412. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  413. Cache::drop('cache.dotted');
  414. }
  415. /**
  416. * testGroupConfigs method
  417. */
  418. public function testGroupConfigs()
  419. {
  420. Cache::drop('test');
  421. Cache::setConfig('latest', [
  422. 'duration' => 300,
  423. 'engine' => 'File',
  424. 'groups' => ['posts', 'comments'],
  425. ]);
  426. $result = Cache::groupConfigs();
  427. $this->assertArrayHasKey('posts', $result);
  428. $this->assertContains('latest', $result['posts']);
  429. $this->assertArrayHasKey('comments', $result);
  430. $this->assertContains('latest', $result['comments']);
  431. $result = Cache::groupConfigs('posts');
  432. $this->assertEquals(['posts' => ['latest']], $result);
  433. Cache::setConfig('page', [
  434. 'duration' => 86400,
  435. 'engine' => 'File',
  436. 'groups' => ['posts', 'archive'],
  437. ]);
  438. $result = Cache::groupConfigs();
  439. $this->assertArrayHasKey('posts', $result);
  440. $this->assertContains('latest', $result['posts']);
  441. $this->assertContains('page', $result['posts']);
  442. $this->assertArrayHasKey('comments', $result);
  443. $this->assertContains('latest', $result['comments']);
  444. $this->assertNotContains('page', $result['comments']);
  445. $this->assertArrayHasKey('archive', $result);
  446. $this->assertContains('page', $result['archive']);
  447. $this->assertNotContains('latest', $result['archive']);
  448. $result = Cache::groupConfigs('archive');
  449. $this->assertEquals(['archive' => ['page']], $result);
  450. Cache::setConfig('archive', [
  451. 'duration' => 86400 * 30,
  452. 'engine' => 'File',
  453. 'groups' => ['posts', 'archive', 'comments'],
  454. ]);
  455. $result = Cache::groupConfigs('archive');
  456. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  457. }
  458. /**
  459. * testGroupConfigsWithCacheInstance method
  460. */
  461. public function testGroupConfigsWithCacheInstance()
  462. {
  463. Cache::drop('test');
  464. $cache = new FileEngine();
  465. $cache->init([
  466. 'duration' => 300,
  467. 'engine' => 'File',
  468. 'groups' => ['users', 'comments'],
  469. ]);
  470. Cache::setConfig('cached', $cache);
  471. $result = Cache::groupConfigs('users');
  472. $this->assertEquals(['users' => ['cached']], $result);
  473. }
  474. /**
  475. * testGroupConfigsThrowsException method
  476. */
  477. public function testGroupConfigsThrowsException()
  478. {
  479. $this->expectException(\InvalidArgumentException::class);
  480. Cache::groupConfigs('bogus');
  481. }
  482. /**
  483. * test that configured returns an array of the currently configured cache
  484. * config
  485. *
  486. * @return void
  487. */
  488. public function testConfigured()
  489. {
  490. Cache::drop('default');
  491. $result = Cache::configured();
  492. $this->assertContains('_cake_core_', $result);
  493. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  494. }
  495. /**
  496. * test that drop removes cache configs, and that further attempts to use that config
  497. * do not work.
  498. *
  499. * @return void
  500. */
  501. public function testDrop()
  502. {
  503. static::setAppNamespace();
  504. $result = Cache::drop('some_config_that_does_not_exist');
  505. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  506. Cache::setConfig('unconfigTest', [
  507. 'engine' => 'TestAppCache'
  508. ]);
  509. $this->assertInstanceOf(
  510. 'TestApp\Cache\Engine\TestAppCacheEngine',
  511. Cache::engine('unconfigTest')
  512. );
  513. $this->assertTrue(Cache::drop('unconfigTest'));
  514. }
  515. /**
  516. * testWriteEmptyValues method
  517. *
  518. * @return void
  519. */
  520. public function testWriteEmptyValues()
  521. {
  522. $this->_configCache();
  523. Cache::write('App.falseTest', false, 'tests');
  524. $this->assertFalse(Cache::read('App.falseTest', 'tests'));
  525. Cache::write('App.trueTest', true, 'tests');
  526. $this->assertTrue(Cache::read('App.trueTest', 'tests'));
  527. Cache::write('App.nullTest', null, 'tests');
  528. $this->assertNull(Cache::read('App.nullTest', 'tests'));
  529. Cache::write('App.zeroTest', 0, 'tests');
  530. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  531. Cache::write('App.zeroTest2', '0', 'tests');
  532. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  533. }
  534. /**
  535. * testWriteEmptyValues method
  536. *
  537. * @return void
  538. */
  539. public function testWriteEmptyKey()
  540. {
  541. $this->expectException(\InvalidArgumentException::class);
  542. $this->expectExceptionMessage('An empty value is not valid as a cache key');
  543. $this->_configCache();
  544. Cache::write(null, 'not null', 'tests');
  545. }
  546. /**
  547. * testReadWriteMany method
  548. *
  549. * @return void
  550. */
  551. public function testReadWriteMany()
  552. {
  553. $this->_configCache();
  554. $data = [
  555. 'App.falseTest' => false,
  556. 'App.trueTest' => true,
  557. 'App.nullTest' => null,
  558. 'App.zeroTest' => 0,
  559. 'App.zeroTest2' => '0'
  560. ];
  561. Cache::writeMany($data, 'tests');
  562. $read = Cache::readMany(array_keys($data), 'tests');
  563. $this->assertFalse($read['App.falseTest']);
  564. $this->assertTrue($read['App.trueTest']);
  565. $this->assertNull($read['App.nullTest']);
  566. $this->assertSame($read['App.zeroTest'], 0);
  567. $this->assertSame($read['App.zeroTest2'], '0');
  568. }
  569. /**
  570. * testDeleteMany method
  571. *
  572. * @return void
  573. */
  574. public function testDeleteMany()
  575. {
  576. $this->_configCache();
  577. $data = [
  578. 'App.falseTest' => false,
  579. 'App.trueTest' => true,
  580. 'App.nullTest' => null,
  581. 'App.zeroTest' => 0,
  582. 'App.zeroTest2' => '0'
  583. ];
  584. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  585. Cache::deleteMany(array_keys($data), 'tests');
  586. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  587. $this->assertFalse($read['App.falseTest']);
  588. $this->assertFalse($read['App.trueTest']);
  589. $this->assertFalse($read['App.nullTest']);
  590. $this->assertFalse($read['App.zeroTest']);
  591. $this->assertFalse($read['App.zeroTest2']);
  592. $this->assertSame($read['App.keepTest'], 'keepMe');
  593. }
  594. /**
  595. * Test that failed writes cause errors to be triggered.
  596. *
  597. * @return void
  598. */
  599. public function testWriteTriggerError()
  600. {
  601. $this->expectException(\PHPUnit\Framework\Error\Error::class);
  602. static::setAppNamespace();
  603. Cache::setConfig('test_trigger', [
  604. 'engine' => 'TestAppCache',
  605. 'prefix' => ''
  606. ]);
  607. Cache::write('fail', 'value', 'test_trigger');
  608. }
  609. /**
  610. * testCacheDisable method
  611. *
  612. * Check that the "Cache.disable" configuration and a change to it
  613. * (even after a cache config has been setup) is taken into account.
  614. *
  615. * @return void
  616. */
  617. public function testCacheDisable()
  618. {
  619. Cache::enable();
  620. Cache::setConfig('test_cache_disable_1', [
  621. 'engine' => 'File',
  622. 'path' => TMP . '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->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  628. $this->assertFalse(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(false, 'test_cache_disable_1');
  633. Cache::disable();
  634. Cache::setConfig('test_cache_disable_2', [
  635. 'engine' => 'File',
  636. 'path' => TMP . 'tests'
  637. ]);
  638. $this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  639. $this->assertFalse(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->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  645. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  646. Cache::enable();
  647. Cache::clear(false, 'test_cache_disable_2');
  648. }
  649. /**
  650. * test clearAll() method
  651. *
  652. * @return void
  653. */
  654. public function testClearAll()
  655. {
  656. Cache::setConfig('configTest', [
  657. 'engine' => 'File',
  658. 'path' => TMP . 'tests'
  659. ]);
  660. Cache::setConfig('anotherConfigTest', [
  661. 'engine' => 'File',
  662. 'path' => TMP . '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->assertFalse(Cache::read('key_1', 'configTest'));
  672. $this->assertFalse(Cache::read('key_2', 'anotherConfigTest'));
  673. Cache::drop('configTest');
  674. Cache::drop('anotherConfigTest');
  675. }
  676. /**
  677. * Test toggling enabled state of cache.
  678. *
  679. * @return void
  680. */
  681. public function testEnableDisableEnabled()
  682. {
  683. $this->assertNull(Cache::enable());
  684. $this->assertTrue(Cache::enabled(), 'Should be on');
  685. $this->assertNull(Cache::disable());
  686. $this->assertFalse(Cache::enabled(), 'Should be off');
  687. }
  688. /**
  689. * test remember method.
  690. *
  691. * @return void
  692. */
  693. public function testRemember()
  694. {
  695. $this->_configCache();
  696. $counter = 0;
  697. $cacher = function () use ($counter) {
  698. return 'This is some data ' . $counter;
  699. };
  700. $expected = 'This is some data 0';
  701. $result = Cache::remember('test_key', $cacher, 'tests');
  702. $this->assertEquals($expected, $result);
  703. $counter = 1;
  704. $result = Cache::remember('test_key', $cacher, 'tests');
  705. $this->assertEquals($expected, $result);
  706. }
  707. /**
  708. * Test add method.
  709. *
  710. * @return void
  711. */
  712. public function testAdd()
  713. {
  714. $this->_configCache();
  715. Cache::delete('test_add_key', 'tests');
  716. $result = Cache::add('test_add_key', 'test data', 'tests');
  717. $this->assertTrue($result);
  718. $expected = 'test data';
  719. $result = Cache::read('test_add_key', 'tests');
  720. $this->assertEquals($expected, $result);
  721. $result = Cache::add('test_add_key', 'test data 2', 'tests');
  722. $this->assertFalse($result);
  723. }
  724. /**
  725. * Test getting the registry
  726. *
  727. * @return void
  728. */
  729. public function testGetRegistry()
  730. {
  731. $this->assertInstanceOf(CacheRegistry::class, Cache::getRegistry());
  732. }
  733. /**
  734. * Test setting the registry
  735. *
  736. * @return void
  737. */
  738. public function testSetAndGetRegistry()
  739. {
  740. $registry = new CacheRegistry();
  741. Cache::setRegistry($registry);
  742. $this->assertSame($registry, Cache::getRegistry());
  743. }
  744. }