CacheTest.php 27 KB

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