CacheTest.php 12 KB

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