CacheTest.php 24 KB

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