CacheTest.php 25 KB

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