CacheTest.php 23 KB

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