CacheTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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\Core\Plugin;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * CacheTest class
  23. */
  24. class CacheTest extends TestCase
  25. {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. Cache::enable();
  35. }
  36. /**
  37. * tearDown method
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. parent::tearDown();
  44. Cache::drop('tests');
  45. Cache::drop('test_trigger');
  46. }
  47. /**
  48. * Configure cache settings for test
  49. *
  50. * @return void
  51. */
  52. protected function _configCache()
  53. {
  54. Cache::config('tests', [
  55. 'engine' => 'File',
  56. 'path' => TMP,
  57. 'prefix' => 'test_'
  58. ]);
  59. }
  60. /**
  61. * Check that no fatal errors are issued doing normal things when Cache.disable is true.
  62. *
  63. * @return void
  64. */
  65. public function testNonFatalErrorsWithCacheDisable()
  66. {
  67. Cache::disable();
  68. $this->_configCache();
  69. $this->assertNull(Cache::write('no_save', 'Noooo!', 'tests'));
  70. $this->assertFalse(Cache::read('no_save', 'tests'));
  71. $this->assertNull(Cache::delete('no_save', 'tests'));
  72. }
  73. /**
  74. * Check that a null instance is returned from engine() when caching is disabled.
  75. *
  76. * @return void
  77. */
  78. public function testNullEngineWhenCacheDisable()
  79. {
  80. $this->_configCache();
  81. Cache::disable();
  82. $result = Cache::engine('tests');
  83. $this->assertInstanceOf('Cake\Cache\Engine\NullEngine', $result);
  84. }
  85. /**
  86. * Test configuring an invalid class fails
  87. *
  88. * @expectedException \RuntimeException
  89. * @expectedExceptionMessage Cache engines must use Cake\Cache\CacheEngine
  90. * @return void
  91. */
  92. public function testConfigInvalidClassType()
  93. {
  94. Cache::config('tests', [
  95. 'className' => '\StdClass'
  96. ]);
  97. Cache::engine('tests');
  98. }
  99. /**
  100. * Test engine init failing causes an error
  101. *
  102. * @expectedException \RuntimeException
  103. * @expectedExceptionMessage not properly configured
  104. * @return void
  105. */
  106. public function testConfigFailedInit()
  107. {
  108. $mock = $this->getMockForAbstractClass('Cake\Cache\CacheEngine', [], '', true, true, true, ['init']);
  109. $mock->method('init')->will($this->returnValue(false));
  110. Cache::config('tests', [
  111. 'engine' => $mock
  112. ]);
  113. Cache::engine('tests');
  114. }
  115. /**
  116. * test configuring CacheEngines in App/libs
  117. *
  118. * @return void
  119. */
  120. public function testConfigWithLibAndPluginEngines()
  121. {
  122. static::setAppNamespace();
  123. Plugin::load('TestPlugin');
  124. $config = ['engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  125. Cache::config('libEngine', $config);
  126. $engine = Cache::engine('libEngine');
  127. $this->assertInstanceOf('TestApp\Cache\Engine\TestAppCacheEngine', $engine);
  128. $config = ['engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  129. $result = Cache::config('pluginLibEngine', $config);
  130. $engine = Cache::engine('pluginLibEngine');
  131. $this->assertInstanceOf('TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine);
  132. Cache::drop('libEngine');
  133. Cache::drop('pluginLibEngine');
  134. Plugin::unload();
  135. }
  136. /**
  137. * Test write from a config that is undefined.
  138. *
  139. * @expectedException \InvalidArgumentException
  140. * @return void
  141. */
  142. public function testWriteNonExistingConfig()
  143. {
  144. $this->assertFalse(Cache::write('key', 'value', 'totally fake'));
  145. }
  146. /**
  147. * Test write from a config that is undefined.
  148. *
  149. * @expectedException \InvalidArgumentException
  150. * @return void
  151. */
  152. public function testIncrementNonExistingConfig()
  153. {
  154. $this->assertFalse(Cache::increment('key', 1, 'totally fake'));
  155. }
  156. /**
  157. * Test write from a config that is undefined.
  158. *
  159. * @expectedException \InvalidArgumentException
  160. * @return void
  161. */
  162. public function testDecrementNonExistingConfig()
  163. {
  164. $this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
  165. }
  166. /**
  167. * Data provider for valid config data sets.
  168. *
  169. * @return array
  170. */
  171. public static function configProvider()
  172. {
  173. return [
  174. 'Array of data using engine key.' => [[
  175. 'engine' => 'File',
  176. 'path' => TMP . 'tests',
  177. 'prefix' => 'cake_test_'
  178. ]],
  179. 'Array of data using classname key.' => [[
  180. 'className' => 'File',
  181. 'path' => TMP . 'tests',
  182. 'prefix' => 'cake_test_'
  183. ]],
  184. 'Direct instance' => [new FileEngine()],
  185. ];
  186. }
  187. /**
  188. * testConfig method
  189. *
  190. * @dataProvider configProvider
  191. * @return void
  192. */
  193. public function testConfigVariants($config)
  194. {
  195. $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
  196. Cache::config('tests', $config);
  197. $engine = Cache::engine('tests');
  198. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  199. $this->assertContains('tests', Cache::configured());
  200. }
  201. /**
  202. * testConfigInvalidEngine method
  203. *
  204. * @expectedException \BadMethodCallException
  205. * @return void
  206. */
  207. public function testConfigInvalidEngine()
  208. {
  209. $config = ['engine' => 'Imaginary'];
  210. Cache::config('test', $config);
  211. Cache::engine('test');
  212. }
  213. /**
  214. * test that trying to configure classes that don't extend CacheEngine fail.
  215. *
  216. * @expectedException \BadMethodCallException
  217. * @return void
  218. */
  219. public function testConfigInvalidObject()
  220. {
  221. $this->getMockBuilder(\StdClass::class)
  222. ->setMockClassName('RubbishEngine')
  223. ->getMock();
  224. Cache::config('test', [
  225. 'engine' => '\RubbishEngine'
  226. ]);
  227. Cache::engine('tests');
  228. }
  229. /**
  230. * Ensure you cannot reconfigure a cache adapter.
  231. *
  232. * @expectedException \BadMethodCallException
  233. * @return void
  234. */
  235. public function testConfigErrorOnReconfigure()
  236. {
  237. Cache::config('tests', ['engine' => 'File', 'path' => TMP]);
  238. Cache::config('tests', ['engine' => 'Apc']);
  239. }
  240. /**
  241. * Test reading configuration.
  242. *
  243. * @return void
  244. */
  245. public function testConfigRead()
  246. {
  247. $config = [
  248. 'engine' => 'File',
  249. 'path' => TMP,
  250. 'prefix' => 'cake_'
  251. ];
  252. Cache::config('tests', $config);
  253. $expected = $config;
  254. $expected['className'] = $config['engine'];
  255. unset($expected['engine']);
  256. $this->assertEquals($expected, Cache::config('tests'));
  257. }
  258. /**
  259. * test config() with dotted name
  260. *
  261. * @return void
  262. */
  263. public function testConfigDottedAlias()
  264. {
  265. Cache::config('cache.dotted', [
  266. 'className' => 'File',
  267. 'path' => TMP,
  268. 'prefix' => 'cache_value_'
  269. ]);
  270. $engine = Cache::engine('cache.dotted');
  271. $this->assertContains('cache.dotted', Cache::configured());
  272. $this->assertNotContains('dotted', Cache::configured());
  273. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  274. Cache::drop('cache.dotted');
  275. }
  276. /**
  277. * testGroupConfigs method
  278. */
  279. public function testGroupConfigs()
  280. {
  281. Cache::drop('test');
  282. Cache::config('latest', [
  283. 'duration' => 300,
  284. 'engine' => 'File',
  285. 'groups' => ['posts', 'comments'],
  286. ]);
  287. $expected = [
  288. 'posts' => ['latest'],
  289. 'comments' => ['latest'],
  290. ];
  291. $result = Cache::groupConfigs();
  292. $this->assertEquals($expected, $result);
  293. $result = Cache::groupConfigs('posts');
  294. $this->assertEquals(['posts' => ['latest']], $result);
  295. Cache::config('page', [
  296. 'duration' => 86400,
  297. 'engine' => 'File',
  298. 'groups' => ['posts', 'archive'],
  299. ]);
  300. $result = Cache::groupConfigs();
  301. $expected = [
  302. 'posts' => ['latest', 'page'],
  303. 'comments' => ['latest'],
  304. 'archive' => ['page']
  305. ];
  306. $this->assertEquals($expected, $result);
  307. $result = Cache::groupConfigs('archive');
  308. $this->assertEquals(['archive' => ['page']], $result);
  309. Cache::config('archive', [
  310. 'duration' => 86400 * 30,
  311. 'engine' => 'File',
  312. 'groups' => ['posts', 'archive', 'comments'],
  313. ]);
  314. $result = Cache::groupConfigs('archive');
  315. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  316. }
  317. /**
  318. * testGroupConfigsWithCacheInstance method
  319. */
  320. public function testGroupConfigsWithCacheInstance()
  321. {
  322. Cache::drop('test');
  323. $cache = new FileEngine();
  324. $cache->init([
  325. 'duration' => 300,
  326. 'engine' => 'File',
  327. 'groups' => ['users', 'comments'],
  328. ]);
  329. Cache::config('cached', $cache);
  330. $result = Cache::groupConfigs('users');
  331. $this->assertEquals(['users' => ['cached']], $result);
  332. }
  333. /**
  334. * testGroupConfigsThrowsException method
  335. * @expectedException \InvalidArgumentException
  336. */
  337. public function testGroupConfigsThrowsException()
  338. {
  339. Cache::groupConfigs('bogus');
  340. }
  341. /**
  342. * test that configured returns an array of the currently configured cache
  343. * config
  344. *
  345. * @return void
  346. */
  347. public function testConfigured()
  348. {
  349. Cache::drop('default');
  350. $result = Cache::configured();
  351. $this->assertContains('_cake_core_', $result);
  352. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  353. }
  354. /**
  355. * test that drop removes cache configs, and that further attempts to use that config
  356. * do not work.
  357. *
  358. * @return void
  359. */
  360. public function testDrop()
  361. {
  362. static::setAppNamespace();
  363. $result = Cache::drop('some_config_that_does_not_exist');
  364. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  365. Cache::config('unconfigTest', [
  366. 'engine' => 'TestAppCache'
  367. ]);
  368. $this->assertInstanceOf(
  369. 'TestApp\Cache\Engine\TestAppCacheEngine',
  370. Cache::engine('unconfigTest')
  371. );
  372. $this->assertTrue(Cache::drop('unconfigTest'));
  373. }
  374. /**
  375. * testWriteEmptyValues method
  376. *
  377. * @return void
  378. */
  379. public function testWriteEmptyValues()
  380. {
  381. $this->_configCache();
  382. Cache::write('App.falseTest', false, 'tests');
  383. $this->assertSame(Cache::read('App.falseTest', 'tests'), false);
  384. Cache::write('App.trueTest', true, 'tests');
  385. $this->assertSame(Cache::read('App.trueTest', 'tests'), true);
  386. Cache::write('App.nullTest', null, 'tests');
  387. $this->assertSame(Cache::read('App.nullTest', 'tests'), null);
  388. Cache::write('App.zeroTest', 0, 'tests');
  389. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  390. Cache::write('App.zeroTest2', '0', 'tests');
  391. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  392. }
  393. /**
  394. * testWriteEmptyValues method
  395. *
  396. * @expectedException \InvalidArgumentException
  397. * @expectedExceptionMessage An empty value is not valid as a cache key
  398. * @return void
  399. */
  400. public function testWriteEmptyKey()
  401. {
  402. $this->_configCache();
  403. Cache::write(null, 'not null', 'tests');
  404. }
  405. /**
  406. * testReadWriteMany method
  407. *
  408. * @return void
  409. */
  410. public function testReadWriteMany()
  411. {
  412. $this->_configCache();
  413. $data = [
  414. 'App.falseTest' => false,
  415. 'App.trueTest' => true,
  416. 'App.nullTest' => null,
  417. 'App.zeroTest' => 0,
  418. 'App.zeroTest2' => '0'
  419. ];
  420. Cache::writeMany($data, 'tests');
  421. $read = Cache::readMany(array_keys($data), 'tests');
  422. $this->assertSame($read['App.falseTest'], false);
  423. $this->assertSame($read['App.trueTest'], true);
  424. $this->assertSame($read['App.nullTest'], null);
  425. $this->assertSame($read['App.zeroTest'], 0);
  426. $this->assertSame($read['App.zeroTest2'], '0');
  427. }
  428. /**
  429. * testDeleteMany method
  430. *
  431. * @return void
  432. */
  433. public function testDeleteMany()
  434. {
  435. $this->_configCache();
  436. $data = [
  437. 'App.falseTest' => false,
  438. 'App.trueTest' => true,
  439. 'App.nullTest' => null,
  440. 'App.zeroTest' => 0,
  441. 'App.zeroTest2' => '0'
  442. ];
  443. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  444. Cache::deleteMany(array_keys($data), 'tests');
  445. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  446. $this->assertSame($read['App.falseTest'], false);
  447. $this->assertSame($read['App.trueTest'], false);
  448. $this->assertSame($read['App.nullTest'], false);
  449. $this->assertSame($read['App.zeroTest'], false);
  450. $this->assertSame($read['App.zeroTest2'], false);
  451. $this->assertSame($read['App.keepTest'], 'keepMe');
  452. }
  453. /**
  454. * Test that failed writes cause errors to be triggered.
  455. *
  456. * @expectedException \PHPUnit\Framework\Error\Error
  457. * @return void
  458. */
  459. public function testWriteTriggerError()
  460. {
  461. static::setAppNamespace();
  462. Cache::config('test_trigger', [
  463. 'engine' => 'TestAppCache',
  464. 'prefix' => ''
  465. ]);
  466. Cache::write('fail', 'value', 'test_trigger');
  467. }
  468. /**
  469. * testCacheDisable method
  470. *
  471. * Check that the "Cache.disable" configuration and a change to it
  472. * (even after a cache config has been setup) is taken into account.
  473. *
  474. * @return void
  475. */
  476. public function testCacheDisable()
  477. {
  478. Cache::enable();
  479. Cache::config('test_cache_disable_1', [
  480. 'engine' => 'File',
  481. 'path' => TMP . 'tests'
  482. ]);
  483. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  484. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  485. Cache::disable();
  486. $this->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  487. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  488. Cache::enable();
  489. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  490. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  491. Cache::clear(false, 'test_cache_disable_1');
  492. Cache::disable();
  493. Cache::config('test_cache_disable_2', [
  494. 'engine' => 'File',
  495. 'path' => TMP . 'tests'
  496. ]);
  497. $this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  498. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  499. Cache::enable();
  500. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  501. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  502. Cache::disable();
  503. $this->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  504. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  505. Cache::enable();
  506. Cache::clear(false, 'test_cache_disable_2');
  507. }
  508. /**
  509. * test clearAll() method
  510. *
  511. * @return void
  512. */
  513. public function testClearAll()
  514. {
  515. Cache::config('configTest', [
  516. 'engine' => 'File',
  517. 'path' => TMP . 'tests'
  518. ]);
  519. Cache::config('anotherConfigTest', [
  520. 'engine' => 'File',
  521. 'path' => TMP . 'tests'
  522. ]);
  523. Cache::write('key_1', 'hello', 'configTest');
  524. Cache::write('key_2', 'hello again', 'anotherConfigTest');
  525. $this->assertSame(Cache::read('key_1', 'configTest'), 'hello');
  526. $this->assertSame(Cache::read('key_2', 'anotherConfigTest'), 'hello again');
  527. $result = Cache::clearAll();
  528. $this->assertTrue($result['configTest']);
  529. $this->assertTrue($result['anotherConfigTest']);
  530. $this->assertFalse(Cache::read('key_1', 'configTest'));
  531. $this->assertFalse(Cache::read('key_2', 'anotherConfigTest'));
  532. Cache::drop('configTest');
  533. Cache::drop('anotherConfigTest');
  534. }
  535. /**
  536. * Test toggling enabled state of cache.
  537. *
  538. * @return void
  539. */
  540. public function testEnableDisableEnabled()
  541. {
  542. $this->assertNull(Cache::enable());
  543. $this->assertTrue(Cache::enabled(), 'Should be on');
  544. $this->assertNull(Cache::disable());
  545. $this->assertFalse(Cache::enabled(), 'Should be off');
  546. }
  547. /**
  548. * test remember method.
  549. *
  550. * @return void
  551. */
  552. public function testRemember()
  553. {
  554. $this->_configCache();
  555. $counter = 0;
  556. $cacher = function () use ($counter) {
  557. return 'This is some data ' . $counter;
  558. };
  559. $expected = 'This is some data 0';
  560. $result = Cache::remember('test_key', $cacher, 'tests');
  561. $this->assertEquals($expected, $result);
  562. $counter = 1;
  563. $result = Cache::remember('test_key', $cacher, 'tests');
  564. $this->assertEquals($expected, $result);
  565. }
  566. /**
  567. * Test add method.
  568. *
  569. * @return void
  570. */
  571. public function testAdd()
  572. {
  573. $this->_configCache();
  574. Cache::delete('test_add_key', 'tests');
  575. $result = Cache::add('test_add_key', 'test data', 'tests');
  576. $this->assertTrue($result);
  577. $expected = 'test data';
  578. $result = Cache::read('test_add_key', 'tests');
  579. $this->assertEquals($expected, $result);
  580. $result = Cache::add('test_add_key', 'test data 2', 'tests');
  581. $this->assertFalse($result);
  582. }
  583. /**
  584. * test registry method
  585. *
  586. * @return void
  587. */
  588. public function testRegistry()
  589. {
  590. $this->assertInstanceOf('Cake\Cache\CacheRegistry', Cache::registry());
  591. }
  592. /**
  593. * test registry method setting
  594. *
  595. * @return void
  596. */
  597. public function testRegistrySet()
  598. {
  599. $registry = new CacheRegistry();
  600. Cache::registry($registry);
  601. $this->assertSame($registry, Cache::registry());
  602. }
  603. }