CacheTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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\Engine\FileEngine;
  18. use Cake\Core\App;
  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 CacheEngines in App/libs
  89. *
  90. * @return void
  91. */
  92. public function testConfigWithLibAndPluginEngines()
  93. {
  94. Configure::write('App.namespace', 'TestApp');
  95. Plugin::load('TestPlugin');
  96. $config = ['engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  97. Cache::config('libEngine', $config);
  98. $engine = Cache::engine('libEngine');
  99. $this->assertInstanceOf('TestApp\Cache\Engine\TestAppCacheEngine', $engine);
  100. $config = ['engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_'];
  101. $result = Cache::config('pluginLibEngine', $config);
  102. $engine = Cache::engine('pluginLibEngine');
  103. $this->assertInstanceOf('TestPlugin\Cache\Engine\TestPluginCacheEngine', $engine);
  104. Cache::drop('libEngine');
  105. Cache::drop('pluginLibEngine');
  106. Plugin::unload();
  107. }
  108. /**
  109. * Test write from a config that is undefined.
  110. *
  111. * @expectedException \InvalidArgumentException
  112. * @return void
  113. */
  114. public function testWriteNonExistingConfig()
  115. {
  116. $this->assertFalse(Cache::write('key', 'value', 'totally fake'));
  117. }
  118. /**
  119. * Test write from a config that is undefined.
  120. *
  121. * @expectedException \InvalidArgumentException
  122. * @return void
  123. */
  124. public function testIncrementNonExistingConfig()
  125. {
  126. $this->assertFalse(Cache::increment('key', 1, 'totally fake'));
  127. }
  128. /**
  129. * Test write from a config that is undefined.
  130. *
  131. * @expectedException \InvalidArgumentException
  132. * @return void
  133. */
  134. public function testDecrementNonExistingConfig()
  135. {
  136. $this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
  137. }
  138. /**
  139. * Data provider for valid config data sets.
  140. *
  141. * @return array
  142. */
  143. public static function configProvider()
  144. {
  145. return [
  146. 'Array of data using engine key.' => [[
  147. 'engine' => 'File',
  148. 'path' => TMP . 'tests',
  149. 'prefix' => 'cake_test_'
  150. ]],
  151. 'Array of data using classname key.' => [[
  152. 'className' => 'File',
  153. 'path' => TMP . 'tests',
  154. 'prefix' => 'cake_test_'
  155. ]],
  156. 'Direct instance' => [new FileEngine()],
  157. ];
  158. }
  159. /**
  160. * testConfig method
  161. *
  162. * @dataProvider configProvider
  163. * @return void
  164. */
  165. public function testConfigVariants($config)
  166. {
  167. $this->assertNotContains('test', Cache::configured(), 'test config should not exist.');
  168. Cache::config('tests', $config);
  169. $engine = Cache::engine('tests');
  170. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  171. $this->assertContains('tests', Cache::configured());
  172. }
  173. /**
  174. * testConfigInvalidEngine method
  175. *
  176. * @expectedException \BadMethodCallException
  177. * @return void
  178. */
  179. public function testConfigInvalidEngine()
  180. {
  181. $config = ['engine' => 'Imaginary'];
  182. Cache::config('test', $config);
  183. Cache::engine('test');
  184. }
  185. /**
  186. * test that trying to configure classes that don't extend CacheEngine fail.
  187. *
  188. * @expectedException \BadMethodCallException
  189. * @return void
  190. */
  191. public function testConfigInvalidObject()
  192. {
  193. $this->getMock('\StdClass', [], [], 'RubbishEngine');
  194. Cache::config('test', [
  195. 'engine' => '\RubbishEngine'
  196. ]);
  197. Cache::engine('tests');
  198. }
  199. /**
  200. * Ensure you cannot reconfigure a cache adapter.
  201. *
  202. * @expectedException \BadMethodCallException
  203. * @return void
  204. */
  205. public function testConfigErrorOnReconfigure()
  206. {
  207. Cache::config('tests', ['engine' => 'File', 'path' => TMP]);
  208. Cache::config('tests', ['engine' => 'Apc']);
  209. }
  210. /**
  211. * Test reading configuration.
  212. *
  213. * @return void
  214. */
  215. public function testConfigRead()
  216. {
  217. $config = [
  218. 'engine' => 'File',
  219. 'path' => TMP,
  220. 'prefix' => 'cake_'
  221. ];
  222. Cache::config('tests', $config);
  223. $expected = $config;
  224. $expected['className'] = $config['engine'];
  225. unset($expected['engine']);
  226. $this->assertEquals($expected, Cache::config('tests'));
  227. }
  228. /**
  229. * test config() with dotted name
  230. *
  231. * @return void
  232. */
  233. public function testConfigDottedAlias()
  234. {
  235. Cache::config('cache.dotted', [
  236. 'className' => 'File',
  237. 'path' => TMP,
  238. 'prefix' => 'cache_value_'
  239. ]);
  240. $engine = Cache::engine('cache.dotted');
  241. $this->assertContains('cache.dotted', Cache::configured());
  242. $this->assertNotContains('dotted', Cache::configured());
  243. $this->assertInstanceOf('Cake\Cache\Engine\FileEngine', $engine);
  244. Cache::drop('cache.dotted');
  245. }
  246. /**
  247. * testGroupConfigs method
  248. */
  249. public function testGroupConfigs()
  250. {
  251. Cache::drop('test');
  252. Cache::config('latest', [
  253. 'duration' => 300,
  254. 'engine' => 'File',
  255. 'groups' => ['posts', 'comments'],
  256. ]);
  257. $expected = [
  258. 'posts' => ['latest'],
  259. 'comments' => ['latest'],
  260. ];
  261. $result = Cache::groupConfigs();
  262. $this->assertEquals($expected, $result);
  263. $result = Cache::groupConfigs('posts');
  264. $this->assertEquals(['posts' => ['latest']], $result);
  265. Cache::config('page', [
  266. 'duration' => 86400,
  267. 'engine' => 'File',
  268. 'groups' => ['posts', 'archive'],
  269. ]);
  270. $result = Cache::groupConfigs();
  271. $expected = [
  272. 'posts' => ['latest', 'page'],
  273. 'comments' => ['latest'],
  274. 'archive' => ['page']
  275. ];
  276. $this->assertEquals($expected, $result);
  277. $result = Cache::groupConfigs('archive');
  278. $this->assertEquals(['archive' => ['page']], $result);
  279. Cache::config('archive', [
  280. 'duration' => 86400 * 30,
  281. 'engine' => 'File',
  282. 'groups' => ['posts', 'archive', 'comments'],
  283. ]);
  284. $result = Cache::groupConfigs('archive');
  285. $this->assertEquals(['archive' => ['archive', 'page']], $result);
  286. }
  287. /**
  288. * testGroupConfigsThrowsException method
  289. * @expectedException \InvalidArgumentException
  290. */
  291. public function testGroupConfigsThrowsException()
  292. {
  293. Cache::groupConfigs('bogus');
  294. }
  295. /**
  296. * test that configured returns an array of the currently configured cache
  297. * config
  298. *
  299. * @return void
  300. */
  301. public function testConfigured()
  302. {
  303. Cache::drop('default');
  304. $result = Cache::configured();
  305. $this->assertContains('_cake_core_', $result);
  306. $this->assertNotContains('default', $result, 'Unconnected engines should not display.');
  307. }
  308. /**
  309. * test that drop removes cache configs, and that further attempts to use that config
  310. * do not work.
  311. *
  312. * @return void
  313. */
  314. public function testDrop()
  315. {
  316. Configure::write('App.namespace', 'TestApp');
  317. $result = Cache::drop('some_config_that_does_not_exist');
  318. $this->assertFalse($result, 'Drop should not succeed when config is missing.');
  319. Cache::config('unconfigTest', [
  320. 'engine' => 'TestAppCache'
  321. ]);
  322. $this->assertInstanceOf(
  323. 'TestApp\Cache\Engine\TestAppCacheEngine',
  324. Cache::engine('unconfigTest')
  325. );
  326. $this->assertTrue(Cache::drop('unconfigTest'));
  327. }
  328. /**
  329. * testWriteEmptyValues method
  330. *
  331. * @return void
  332. */
  333. public function testWriteEmptyValues()
  334. {
  335. $this->_configCache();
  336. Cache::write('App.falseTest', false, 'tests');
  337. $this->assertSame(Cache::read('App.falseTest', 'tests'), false);
  338. Cache::write('App.trueTest', true, 'tests');
  339. $this->assertSame(Cache::read('App.trueTest', 'tests'), true);
  340. Cache::write('App.nullTest', null, 'tests');
  341. $this->assertSame(Cache::read('App.nullTest', 'tests'), null);
  342. Cache::write('App.zeroTest', 0, 'tests');
  343. $this->assertSame(Cache::read('App.zeroTest', 'tests'), 0);
  344. Cache::write('App.zeroTest2', '0', 'tests');
  345. $this->assertSame(Cache::read('App.zeroTest2', 'tests'), '0');
  346. }
  347. /**
  348. * testWriteEmptyValues method
  349. *
  350. * @expectedException \InvalidArgumentException
  351. * @expectedExceptionMessage An empty value is not valid as a cache key
  352. * @return void
  353. */
  354. public function testWriteEmptyKey()
  355. {
  356. $this->_configCache();
  357. Cache::write(null, 'not null', 'tests');
  358. }
  359. /**
  360. * testReadWriteMany method
  361. *
  362. * @return void
  363. */
  364. public function testReadWriteMany()
  365. {
  366. $this->_configCache();
  367. $data = [
  368. 'App.falseTest' => false,
  369. 'App.trueTest' => true,
  370. 'App.nullTest' => null,
  371. 'App.zeroTest' => 0,
  372. 'App.zeroTest2' => '0'
  373. ];
  374. Cache::writeMany($data, 'tests');
  375. $read = Cache::readMany(array_keys($data), 'tests');
  376. $this->assertSame($read['App.falseTest'], false);
  377. $this->assertSame($read['App.trueTest'], true);
  378. $this->assertSame($read['App.nullTest'], null);
  379. $this->assertSame($read['App.zeroTest'], 0);
  380. $this->assertSame($read['App.zeroTest2'], '0');
  381. }
  382. /**
  383. * testDeleteMany method
  384. *
  385. * @return void
  386. */
  387. public function testDeleteMany()
  388. {
  389. $this->_configCache();
  390. $data = [
  391. 'App.falseTest' => false,
  392. 'App.trueTest' => true,
  393. 'App.nullTest' => null,
  394. 'App.zeroTest' => 0,
  395. 'App.zeroTest2' => '0'
  396. ];
  397. Cache::writeMany(array_merge($data, ['App.keepTest' => 'keepMe']), 'tests');
  398. Cache::deleteMany(array_keys($data), 'tests');
  399. $read = Cache::readMany(array_merge(array_keys($data), ['App.keepTest']), 'tests');
  400. $this->assertSame($read['App.falseTest'], false);
  401. $this->assertSame($read['App.trueTest'], false);
  402. $this->assertSame($read['App.nullTest'], false);
  403. $this->assertSame($read['App.zeroTest'], false);
  404. $this->assertSame($read['App.zeroTest2'], false);
  405. $this->assertSame($read['App.keepTest'], 'keepMe');
  406. }
  407. /**
  408. * Test that failed writes cause errors to be triggered.
  409. *
  410. * @expectedException \PHPUnit_Framework_Error
  411. * @return void
  412. */
  413. public function testWriteTriggerError()
  414. {
  415. Configure::write('App.namespace', 'TestApp');
  416. Cache::config('test_trigger', [
  417. 'engine' => 'TestAppCache',
  418. 'prefix' => ''
  419. ]);
  420. Cache::write('fail', 'value', 'test_trigger');
  421. }
  422. /**
  423. * testCacheDisable method
  424. *
  425. * Check that the "Cache.disable" configuration and a change to it
  426. * (even after a cache config has been setup) is taken into account.
  427. *
  428. * @return void
  429. */
  430. public function testCacheDisable()
  431. {
  432. Cache::enable();
  433. Cache::config('test_cache_disable_1', [
  434. 'engine' => 'File',
  435. 'path' => TMP . 'tests'
  436. ]);
  437. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  438. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  439. Cache::disable();
  440. $this->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  441. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  442. Cache::enable();
  443. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  444. $this->assertSame('hello', Cache::read('key_3', 'test_cache_disable_1'));
  445. Cache::clear(false, 'test_cache_disable_1');
  446. Cache::disable();
  447. Cache::config('test_cache_disable_2', [
  448. 'engine' => 'File',
  449. 'path' => TMP . 'tests'
  450. ]);
  451. $this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  452. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  453. Cache::enable();
  454. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  455. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  456. Cache::disable();
  457. $this->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  458. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  459. Cache::enable();
  460. Cache::clear(false, 'test_cache_disable_2');
  461. }
  462. /**
  463. * Test toggling enabled state of cache.
  464. *
  465. * @return void
  466. */
  467. public function testEnableDisableEnabled()
  468. {
  469. $this->assertNull(Cache::enable());
  470. $this->assertTrue(Cache::enabled(), 'Should be on');
  471. $this->assertNull(Cache::disable());
  472. $this->assertFalse(Cache::enabled(), 'Should be off');
  473. }
  474. /**
  475. * test remember method.
  476. *
  477. * @return void
  478. */
  479. public function testRemember()
  480. {
  481. $this->_configCache();
  482. $counter = 0;
  483. $cacher = function () use ($counter) {
  484. return 'This is some data ' . $counter;
  485. };
  486. $expected = 'This is some data 0';
  487. $result = Cache::remember('test_key', $cacher, 'tests');
  488. $this->assertEquals($expected, $result);
  489. $counter = 1;
  490. $result = Cache::remember('test_key', $cacher, 'tests');
  491. $this->assertEquals($expected, $result);
  492. }
  493. }