CacheTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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->getMock('\StdClass', [], [], 'RubbishEngine');
  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. * testGroupConfigsThrowsException method
  319. * @expectedException \InvalidArgumentException
  320. */
  321. public function testGroupConfigsThrowsException()
  322. {
  323. Cache::groupConfigs('bogus');
  324. }
  325. /**
  326. * test that configured returns an array of the currently configured cache
  327. * config
  328. *
  329. * @return void
  330. */
  331. public function testConfigured()
  332. {
  333. Cache::drop('default');
  334. $result = Cache::configured();
  335. $this->assertContains('_cake_core_', $result);
  336. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  337. }
  338. /**
  339. * test that drop removes cache configs, and that further attempts to use that config
  340. * do not work.
  341. *
  342. * @return void
  343. */
  344. public function testDrop()
  345. {
  346. Configure::write('App.namespace', 'TestApp');
  347. $result = Cache::drop('some_config_that_does_not_exist');
  348. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  349. Cache::config('unconfigTest', [
  350. 'engine' => 'TestAppCache'
  351. ]);
  352. $this->assertInstanceOf(
  353. 'TestApp\Cache\Engine\TestAppCacheEngine',
  354. Cache::engine('unconfigTest')
  355. );
  356. $this->assertTrue(Cache::drop('unconfigTest'));
  357. }
  358. /**
  359. * testWriteEmptyValues method
  360. *
  361. * @return void
  362. */
  363. public function testWriteEmptyValues()
  364. {
  365. $this->_configCache();
  366. Cache::write('App.falseTest', false, 'tests');
  367. $this->assertSame(Cache::read('App.falseTest', 'tests'), false);
  368. Cache::write('App.trueTest', true, 'tests');
  369. $this->assertSame(Cache::read('App.trueTest', 'tests'), true);
  370. Cache::write('App.nullTest', null, 'tests');
  371. $this->assertSame(Cache::read('App.nullTest', 'tests'), null);
  372. Cache::write('App.zeroTest', 0, 'tests');
  373. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  374. Cache::write('App.zeroTest2', '0', 'tests');
  375. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  376. }
  377. /**
  378. * testWriteEmptyValues method
  379. *
  380. * @expectedException \InvalidArgumentException
  381. * @expectedExceptionMessage An empty value is not valid as a cache key
  382. * @return void
  383. */
  384. public function testWriteEmptyKey()
  385. {
  386. $this->_configCache();
  387. Cache::write(null, 'not null', 'tests');
  388. }
  389. /**
  390. * testReadWriteMany method
  391. *
  392. * @return void
  393. */
  394. public function testReadWriteMany()
  395. {
  396. $this->_configCache();
  397. $data = [
  398. 'App.falseTest' => false,
  399. 'App.trueTest' => true,
  400. 'App.nullTest' => null,
  401. 'App.zeroTest' => 0,
  402. 'App.zeroTest2' => '0'
  403. ];
  404. Cache::writeMany($data, 'tests');
  405. $read = Cache::readMany(array_keys($data), 'tests');
  406. $this->assertSame($read['App.falseTest'], false);
  407. $this->assertSame($read['App.trueTest'], true);
  408. $this->assertSame($read['App.nullTest'], null);
  409. $this->assertSame($read['App.zeroTest'], 0);
  410. $this->assertSame($read['App.zeroTest2'], '0');
  411. }
  412. /**
  413. * testDeleteMany method
  414. *
  415. * @return void
  416. */
  417. public function testDeleteMany()
  418. {
  419. $this->_configCache();
  420. $data = [
  421. 'App.falseTest' => false,
  422. 'App.trueTest' => true,
  423. 'App.nullTest' => null,
  424. 'App.zeroTest' => 0,
  425. 'App.zeroTest2' => '0'
  426. ];
  427. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  428. Cache::deleteMany(array_keys($data), 'tests');
  429. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  430. $this->assertSame($read['App.falseTest'], false);
  431. $this->assertSame($read['App.trueTest'], false);
  432. $this->assertSame($read['App.nullTest'], false);
  433. $this->assertSame($read['App.zeroTest'], false);
  434. $this->assertSame($read['App.zeroTest2'], false);
  435. $this->assertSame($read['App.keepTest'], 'keepMe');
  436. }
  437. /**
  438. * Test that failed writes cause errors to be triggered.
  439. *
  440. * @expectedException \PHPUnit_Framework_Error
  441. * @return void
  442. */
  443. public function testWriteTriggerError()
  444. {
  445. Configure::write('App.namespace', 'TestApp');
  446. Cache::config('test_trigger', [
  447. 'engine' => 'TestAppCache',
  448. 'prefix' => ''
  449. ]);
  450. Cache::write('fail', 'value', 'test_trigger');
  451. }
  452. /**
  453. * testCacheDisable method
  454. *
  455. * Check that the "Cache.disable" configuration and a change to it
  456. * (even after a cache config has been setup) is taken into account.
  457. *
  458. * @return void
  459. */
  460. public function testCacheDisable()
  461. {
  462. Cache::enable();
  463. Cache::config('test_cache_disable_1', [
  464. 'engine' => 'File',
  465. 'path' => TMP . 'tests'
  466. ]);
  467. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  468. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  469. Cache::disable();
  470. $this->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  471. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  472. Cache::enable();
  473. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  474. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  475. Cache::clear(false, 'test_cache_disable_1');
  476. Cache::disable();
  477. Cache::config('test_cache_disable_2', [
  478. 'engine' => 'File',
  479. 'path' => TMP . 'tests'
  480. ]);
  481. $this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  482. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  483. Cache::enable();
  484. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  485. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  486. Cache::disable();
  487. $this->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  488. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  489. Cache::enable();
  490. Cache::clear(false, 'test_cache_disable_2');
  491. }
  492. /**
  493. * test clearAll() method
  494. *
  495. * @return void
  496. */
  497. public function testClearAll()
  498. {
  499. Cache::config('configTest', [
  500. 'engine' => 'File',
  501. 'path' => TMP . 'tests'
  502. ]);
  503. Cache::config('anotherConfigTest', [
  504. 'engine' => 'File',
  505. 'path' => TMP . 'tests'
  506. ]);
  507. Cache::write('key_1', 'hello', 'configTest');
  508. Cache::write('key_2', 'hello again', 'anotherConfigTest');
  509. $this->assertSame(Cache::read('key_1', 'configTest'), 'hello');
  510. $this->assertSame(Cache::read('key_2', 'anotherConfigTest'), 'hello again');
  511. $result = Cache::clearAll();
  512. $this->assertTrue($result['configTest']);
  513. $this->assertTrue($result['anotherConfigTest']);
  514. $this->assertFalse(Cache::read('key_1', 'configTest'));
  515. $this->assertFalse(Cache::read('key_2', 'anotherConfigTest'));
  516. Cache::drop('configTest');
  517. Cache::drop('anotherConfigTest');
  518. }
  519. /**
  520. * Test toggling enabled state of cache.
  521. *
  522. * @return void
  523. */
  524. public function testEnableDisableEnabled()
  525. {
  526. $this->assertNull(Cache::enable());
  527. $this->assertTrue(Cache::enabled(), 'Should be on');
  528. $this->assertNull(Cache::disable());
  529. $this->assertFalse(Cache::enabled(), 'Should be off');
  530. }
  531. /**
  532. * test remember method.
  533. *
  534. * @return void
  535. */
  536. public function testRemember()
  537. {
  538. $this->_configCache();
  539. $counter = 0;
  540. $cacher = function () use ($counter) {
  541. return 'This is some data ' . $counter;
  542. };
  543. $expected = 'This is some data 0';
  544. $result = Cache::remember('test_key', $cacher, 'tests');
  545. $this->assertEquals($expected, $result);
  546. $counter = 1;
  547. $result = Cache::remember('test_key', $cacher, 'tests');
  548. $this->assertEquals($expected, $result);
  549. }
  550. /**
  551. * Test add method.
  552. *
  553. * @return void
  554. */
  555. public function testAdd()
  556. {
  557. $this->_configCache();
  558. Cache::delete('test_add_key', 'tests');
  559. $result = Cache::add('test_add_key', 'test data', 'tests');
  560. $this->assertTrue($result);
  561. $expected = 'test data';
  562. $result = Cache::read('test_add_key', 'tests');
  563. $this->assertEquals($expected, $result);
  564. $result = Cache::add('test_add_key', 'test data 2', 'tests');
  565. $this->assertFalse($result);
  566. }
  567. /**
  568. * test registry method
  569. *
  570. * @return void
  571. */
  572. public function testRegistry()
  573. {
  574. $this->assertInstanceOf('Cake\Cache\CacheRegistry', Cache::registry());
  575. }
  576. /**
  577. * test registry method setting
  578. *
  579. * @return void
  580. */
  581. public function testRegistrySet()
  582. {
  583. $registry = new CacheRegistry();
  584. Cache::registry($registry);
  585. $this->assertSame($registry, Cache::registry());
  586. }
  587. }