CacheTest.php 25 KB

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