CacheTest.php 13 KB

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