CacheTest.php 26 KB

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