CacheTest.php 26 KB

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