CacheTest.php 13 KB

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