CacheTest.php 17 KB

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