CacheTest.php 23 KB

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