CacheTest.php 19 KB

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