CacheTest.php 18 KB

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