CacheTest.php 24 KB

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