CacheTest.php 11 KB

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