CacheTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * CacheTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Cache
  17. * @since CakePHP(tm) v 1.2.0.5432
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('Cache', 'Cache');
  21. /**
  22. * CacheTest class
  23. *
  24. * @package Cake.Test.Case.Cache
  25. */
  26. class CacheTest extends CakeTestCase {
  27. /**
  28. * setUp method
  29. *
  30. * @return void
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $this->_cacheDisable = Configure::read('Cache.disable');
  35. Configure::write('Cache.disable', false);
  36. $this->_defaultCacheConfig = Cache::config('default');
  37. Cache::config('default', array('engine' => 'File', 'path' => TMP . 'tests'));
  38. }
  39. /**
  40. * tearDown method
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. parent::tearDown();
  46. Cache::drop('latest');
  47. Cache::drop('page');
  48. Cache::drop('archive');
  49. Configure::write('Cache.disable', $this->_cacheDisable);
  50. Cache::config('default', $this->_defaultCacheConfig['settings']);
  51. }
  52. /**
  53. * testConfig method
  54. *
  55. * @return void
  56. */
  57. public function testConfig() {
  58. $settings = array('engine' => 'File', 'path' => TMP . 'tests', 'prefix' => 'cake_test_');
  59. $results = Cache::config('new', $settings);
  60. $this->assertEquals(Cache::config('new'), $results);
  61. $this->assertTrue(isset($results['engine']));
  62. $this->assertTrue(isset($results['settings']));
  63. }
  64. /**
  65. * testConfigInvalidEngine method
  66. *
  67. * @expectedException CacheException
  68. * @return void
  69. */
  70. public function testConfigInvalidEngine() {
  71. $settings = array('engine' => 'Imaginary');
  72. Cache::config('imaginary', $settings);
  73. }
  74. /**
  75. * Check that no fatal errors are issued doing normal things when Cache.disable is true.
  76. *
  77. * @return void
  78. */
  79. public function testNonFatalErrorsWithCachedisable() {
  80. Configure::write('Cache.disable', true);
  81. Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
  82. Cache::write('no_save', 'Noooo!', 'test');
  83. Cache::read('no_save', 'test');
  84. Cache::delete('no_save', 'test');
  85. Cache::set('duration', '+10 minutes');
  86. Configure::write('Cache.disable', false);
  87. }
  88. /**
  89. * test configuring CacheEngines in App/libs
  90. *
  91. * @return void
  92. */
  93. public function testConfigWithLibAndPluginEngines() {
  94. App::build(array(
  95. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  96. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  97. ), App::RESET);
  98. CakePlugin::load('TestPlugin');
  99. $settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
  100. $result = Cache::config('libEngine', $settings);
  101. $this->assertEquals(Cache::config('libEngine'), $result);
  102. $settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
  103. $result = Cache::config('pluginLibEngine', $settings);
  104. $this->assertEquals(Cache::config('pluginLibEngine'), $result);
  105. Cache::drop('libEngine');
  106. Cache::drop('pluginLibEngine');
  107. App::build();
  108. CakePlugin::unload();
  109. }
  110. /**
  111. * testInvalidConfig method
  112. *
  113. * Test that the cache class doesn't cause fatal errors with a partial path
  114. *
  115. * @expectedException PHPUnit_Framework_Error_Warning
  116. * @return void
  117. */
  118. public function testInvalidConfig() {
  119. // In debug mode it would auto create the folder.
  120. $debug = Configure::read('debug');
  121. Configure::write('debug', 0);
  122. Cache::config('invalid', array(
  123. 'engine' => 'File',
  124. 'duration' => '+1 year',
  125. 'prefix' => 'testing_invalid_',
  126. 'path' => 'data/',
  127. 'serialize' => true,
  128. 'random' => 'wii'
  129. ));
  130. Cache::read('Test', 'invalid');
  131. Configure::write('debug', $debug);
  132. }
  133. /**
  134. * Test reading from a config that is undefined.
  135. *
  136. * @return void
  137. */
  138. public function testReadNonExistingConfig() {
  139. $this->assertFalse(Cache::read('key', 'totally fake'));
  140. $this->assertFalse(Cache::write('key', 'value', 'totally fake'));
  141. $this->assertFalse(Cache::increment('key', 1, 'totally fake'));
  142. $this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
  143. }
  144. /**
  145. * test that trying to configure classes that don't extend CacheEngine fail.
  146. *
  147. * @expectedException CacheException
  148. * @return void
  149. */
  150. public function testAttemptingToConfigureANonCacheEngineClass() {
  151. $this->getMock('StdClass', array(), array(), 'RubbishEngine');
  152. Cache::config('Garbage', array(
  153. 'engine' => 'Rubbish'
  154. ));
  155. }
  156. /**
  157. * testConfigChange method
  158. *
  159. * @return void
  160. */
  161. public function testConfigChange() {
  162. $_cacheConfigSessions = Cache::config('sessions');
  163. $_cacheConfigTests = Cache::config('tests');
  164. $result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
  165. $this->assertEquals(Cache::settings('sessions'), $result['settings']);
  166. $result = Cache::config('tests', array('engine' => 'File', 'path' => TMP . 'tests'));
  167. $this->assertEquals(Cache::settings('tests'), $result['settings']);
  168. Cache::config('sessions', $_cacheConfigSessions['settings']);
  169. Cache::config('tests', $_cacheConfigTests['settings']);
  170. }
  171. /**
  172. * test that calling config() sets the 'default' configuration up.
  173. *
  174. * @return void
  175. */
  176. public function testConfigSettingDefaultConfigKey() {
  177. Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
  178. Cache::write('value_one', 'I am cached', 'test_name');
  179. $result = Cache::read('value_one', 'test_name');
  180. $this->assertEquals('I am cached', $result);
  181. $result = Cache::read('value_one');
  182. $this->assertEquals(null, $result);
  183. Cache::write('value_one', 'I am in default config!');
  184. $result = Cache::read('value_one');
  185. $this->assertEquals('I am in default config!', $result);
  186. $result = Cache::read('value_one', 'test_name');
  187. $this->assertEquals('I am cached', $result);
  188. Cache::delete('value_one', 'test_name');
  189. Cache::delete('value_one', 'default');
  190. }
  191. /**
  192. * testWritingWithConfig method
  193. *
  194. * @return void
  195. */
  196. public function testWritingWithConfig() {
  197. $_cacheConfigSessions = Cache::config('sessions');
  198. Cache::write('test_something', 'this is the test data', 'tests');
  199. $expected = array(
  200. 'path' => TMP . 'sessions' . DS,
  201. 'prefix' => 'cake_',
  202. 'lock' => true,
  203. 'serialize' => true,
  204. 'duration' => 3600,
  205. 'probability' => 100,
  206. 'engine' => 'File',
  207. 'isWindows' => DIRECTORY_SEPARATOR === '\\',
  208. 'mask' => 0664,
  209. 'groups' => array()
  210. );
  211. $this->assertEquals($expected, Cache::settings('sessions'));
  212. Cache::config('sessions', $_cacheConfigSessions['settings']);
  213. }
  214. /**
  215. * testGroupConfigs method
  216. */
  217. public function testGroupConfigs() {
  218. Cache::config('latest', array(
  219. 'duration' => 300,
  220. 'engine' => 'File',
  221. 'groups' => array(
  222. 'posts', 'comments',
  223. ),
  224. ));
  225. $expected = array(
  226. 'posts' => array('latest'),
  227. 'comments' => array('latest'),
  228. );
  229. $result = Cache::groupConfigs();
  230. $this->assertEquals($expected, $result);
  231. $result = Cache::groupConfigs('posts');
  232. $this->assertEquals(array('posts' => array('latest')), $result);
  233. Cache::config('page', array(
  234. 'duration' => 86400,
  235. 'engine' => 'File',
  236. 'groups' => array(
  237. 'posts', 'archive'
  238. ),
  239. ));
  240. $result = Cache::groupConfigs();
  241. $expected = array(
  242. 'posts' => array('latest', 'page'),
  243. 'comments' => array('latest'),
  244. 'archive' => array('page'),
  245. );
  246. $this->assertEquals($expected, $result);
  247. $result = Cache::groupConfigs('archive');
  248. $this->assertEquals(array('archive' => array('page')), $result);
  249. Cache::config('archive', array(
  250. 'duration' => 86400 * 30,
  251. 'engine' => 'File',
  252. 'groups' => array(
  253. 'posts', 'archive', 'comments',
  254. ),
  255. ));
  256. $result = Cache::groupConfigs('archive');
  257. $this->assertEquals(array('archive' => array('archive', 'page')), $result);
  258. }
  259. /**
  260. * testGroupConfigsThrowsException method
  261. * @expectedException CacheException
  262. */
  263. public function testGroupConfigsThrowsException() {
  264. Cache::groupConfigs('bogus');
  265. }
  266. /**
  267. * test that configured returns an array of the currently configured cache
  268. * settings
  269. *
  270. * @return void
  271. */
  272. public function testConfigured() {
  273. $result = Cache::configured();
  274. $this->assertTrue(in_array('_cake_core_', $result));
  275. $this->assertTrue(in_array('default', $result));
  276. }
  277. /**
  278. * testInitSettings method
  279. *
  280. * @return void
  281. */
  282. public function testInitSettings() {
  283. $initial = Cache::settings();
  284. $override = array('engine' => 'File', 'path' => TMP . 'tests');
  285. Cache::config('for_test', $override);
  286. $settings = Cache::settings();
  287. $expecting = $override + $initial;
  288. $this->assertEquals($settings, $expecting);
  289. }
  290. /**
  291. * test that drop removes cache configs, and that further attempts to use that config
  292. * do not work.
  293. *
  294. * @return void
  295. */
  296. public function testDrop() {
  297. App::build(array(
  298. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  299. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  300. ), App::RESET);
  301. $result = Cache::drop('some_config_that_does_not_exist');
  302. $this->assertFalse($result);
  303. $_testsConfig = Cache::config('tests');
  304. $result = Cache::drop('tests');
  305. $this->assertTrue($result);
  306. Cache::config('unconfigTest', array(
  307. 'engine' => 'TestAppCache'
  308. ));
  309. $this->assertTrue(Cache::isInitialized('unconfigTest'));
  310. $this->assertTrue(Cache::drop('unconfigTest'));
  311. $this->assertFalse(Cache::isInitialized('TestAppCache'));
  312. Cache::config('tests', $_testsConfig);
  313. App::build();
  314. }
  315. /**
  316. * testWriteEmptyValues method
  317. *
  318. * @return void
  319. */
  320. public function testWriteEmptyValues() {
  321. Cache::write('App.falseTest', false);
  322. $this->assertFalse(Cache::read('App.falseTest'));
  323. Cache::write('App.trueTest', true);
  324. $this->assertTrue(Cache::read('App.trueTest'));
  325. Cache::write('App.nullTest', null);
  326. $this->assertNull(Cache::read('App.nullTest'));
  327. Cache::write('App.zeroTest', 0);
  328. $this->assertSame(Cache::read('App.zeroTest'), 0);
  329. Cache::write('App.zeroTest2', '0');
  330. $this->assertSame(Cache::read('App.zeroTest2'), '0');
  331. }
  332. /**
  333. * Test that failed writes cause errors to be triggered.
  334. *
  335. * @return void
  336. */
  337. public function testWriteTriggerError() {
  338. App::build(array(
  339. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  340. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  341. ), App::RESET);
  342. Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
  343. try {
  344. Cache::write('fail', 'value', 'test_trigger');
  345. $this->fail('No exception thrown');
  346. } catch (PHPUnit_Framework_Error $e) {
  347. $this->assertTrue(true);
  348. }
  349. Cache::drop('test_trigger');
  350. App::build();
  351. }
  352. /**
  353. * testCacheDisable method
  354. *
  355. * Check that the "Cache.disable" configuration and a change to it
  356. * (even after a cache config has been setup) is taken into account.
  357. *
  358. * @return void
  359. */
  360. public function testCacheDisable() {
  361. Configure::write('Cache.disable', false);
  362. Cache::config('test_cache_disable_1', array('engine' => 'File', 'path' => TMP . 'tests'));
  363. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  364. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  365. Configure::write('Cache.disable', true);
  366. $this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  367. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  368. Configure::write('Cache.disable', false);
  369. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  370. $this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
  371. Configure::write('Cache.disable', true);
  372. Cache::config('test_cache_disable_2', array('engine' => 'File', 'path' => TMP . 'tests'));
  373. $this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  374. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  375. Configure::write('Cache.disable', false);
  376. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  377. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  378. Configure::write('Cache.disable', true);
  379. $this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  380. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  381. }
  382. /**
  383. * testSet method
  384. *
  385. * @return void
  386. */
  387. public function testSet() {
  388. $_cacheSet = Cache::set();
  389. Cache::set(array('duration' => '+1 year'));
  390. $data = Cache::read('test_cache');
  391. $this->assertFalse($data);
  392. $data = 'this is just a simple test of the cache system';
  393. $write = Cache::write('test_cache', $data);
  394. $this->assertTrue($write);
  395. Cache::set(array('duration' => '+1 year'));
  396. $data = Cache::read('test_cache');
  397. $this->assertEquals('this is just a simple test of the cache system', $data);
  398. Cache::delete('test_cache');
  399. Cache::settings();
  400. Cache::set($_cacheSet);
  401. }
  402. /**
  403. * test set() parameter handling for user cache configs.
  404. *
  405. * @return void
  406. */
  407. public function testSetOnAlternateConfigs() {
  408. Cache::config('file_config', array('engine' => 'File', 'prefix' => 'test_file_'));
  409. Cache::set(array('duration' => '+1 year'), 'file_config');
  410. $settings = Cache::settings('file_config');
  411. $this->assertEquals('test_file_', $settings['prefix']);
  412. $this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
  413. }
  414. }