CacheTest.php 19 KB

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