CacheTest.php 24 KB

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