CacheTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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->assertEqual($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. ), true);
  82. CakePlugin::load('TestPlugin');
  83. $settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
  84. $result = Cache::config('libEngine', $settings);
  85. $this->assertEqual($result, Cache::config('libEngine'));
  86. $settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
  87. $result = Cache::config('pluginLibEngine', $settings);
  88. $this->assertEqual($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 that trying to configure classes that don't extend CacheEngine fail.
  115. *
  116. * @return void
  117. */
  118. public function testAttemptingToConfigureANonCacheEngineClass() {
  119. $this->getMock('StdClass', array(), array(), 'RubbishEngine');
  120. $this->expectException();
  121. Cache::config('Garbage', array(
  122. 'engine' => 'Rubbish'
  123. ));
  124. }
  125. /**
  126. * testConfigChange method
  127. *
  128. * @return void
  129. */
  130. public function testConfigChange() {
  131. $_cacheConfigSessions = Cache::config('sessions');
  132. $_cacheConfigTests = Cache::config('tests');
  133. $result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'sessions'));
  134. $this->assertEqual($result['settings'], Cache::settings('sessions'));
  135. $result = Cache::config('tests', array('engine'=> 'File', 'path' => TMP . 'tests'));
  136. $this->assertEqual($result['settings'], Cache::settings('tests'));
  137. Cache::config('sessions', $_cacheConfigSessions['settings']);
  138. Cache::config('tests', $_cacheConfigTests['settings']);
  139. }
  140. /**
  141. * test that calling config() sets the 'default' configuration up.
  142. *
  143. * @return void
  144. */
  145. public function testConfigSettingDefaultConfigKey() {
  146. Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
  147. Cache::write('value_one', 'I am cached', 'test_name');
  148. $result = Cache::read('value_one', 'test_name');
  149. $this->assertEqual($result, 'I am cached');
  150. $result = Cache::read('value_one');
  151. $this->assertEqual($result, null);
  152. Cache::write('value_one', 'I am in default config!');
  153. $result = Cache::read('value_one');
  154. $this->assertEqual($result, 'I am in default config!');
  155. $result = Cache::read('value_one', 'test_name');
  156. $this->assertEqual($result, 'I am cached');
  157. Cache::delete('value_one', 'test_name');
  158. Cache::delete('value_one', 'default');
  159. }
  160. /**
  161. * testWritingWithConfig method
  162. *
  163. * @return void
  164. */
  165. public function testWritingWithConfig() {
  166. $_cacheConfigSessions = Cache::config('sessions');
  167. Cache::write('test_somthing', 'this is the test data', 'tests');
  168. $expected = array(
  169. 'path' => TMP . 'sessions' . DS,
  170. 'prefix' => 'cake_',
  171. 'lock' => true,
  172. 'serialize' => true,
  173. 'duration' => 3600,
  174. 'probability' => 100,
  175. 'engine' => 'File',
  176. 'isWindows' => DIRECTORY_SEPARATOR == '\\',
  177. 'mask' => 0664
  178. );
  179. $this->assertEqual($expected, Cache::settings('sessions'));
  180. Cache::config('sessions', $_cacheConfigSessions['settings']);
  181. }
  182. /**
  183. * test that configured returns an array of the currently configured cache
  184. * settings
  185. *
  186. * @return void
  187. */
  188. public function testConfigured() {
  189. $result = Cache::configured();
  190. $this->assertTrue(in_array('_cake_core_', $result));
  191. $this->assertTrue(in_array('default', $result));
  192. }
  193. /**
  194. * testInitSettings method
  195. *
  196. * @return void
  197. */
  198. public function testInitSettings() {
  199. $initial = Cache::settings();
  200. $override = array('engine' => 'File', 'path' => TMP . 'tests');
  201. Cache::config('for_test', $override);
  202. $settings = Cache::settings();
  203. $expecting = $override + $initial;
  204. $this->assertEqual($settings, $expecting);
  205. }
  206. /**
  207. * test that drop removes cache configs, and that further attempts to use that config
  208. * do not work.
  209. *
  210. * @return void
  211. */
  212. public function testDrop() {
  213. App::build(array(
  214. 'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  215. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  216. ), true);
  217. $result = Cache::drop('some_config_that_does_not_exist');
  218. $this->assertFalse($result);
  219. $_testsConfig = Cache::config('tests');
  220. $result = Cache::drop('tests');
  221. $this->assertTrue($result);
  222. Cache::config('unconfigTest', array(
  223. 'engine' => 'TestAppCache'
  224. ));
  225. $this->assertTrue(Cache::isInitialized('unconfigTest'));
  226. $this->assertTrue(Cache::drop('unconfigTest'));
  227. $this->assertFalse(Cache::isInitialized('TestAppCache'));
  228. Cache::config('tests', $_testsConfig);
  229. App::build();
  230. }
  231. /**
  232. * testWriteEmptyValues method
  233. *
  234. * @return void
  235. */
  236. public function testWriteEmptyValues() {
  237. Cache::write('App.falseTest', false);
  238. $this->assertIdentical(Cache::read('App.falseTest'), false);
  239. Cache::write('App.trueTest', true);
  240. $this->assertIdentical(Cache::read('App.trueTest'), true);
  241. Cache::write('App.nullTest', null);
  242. $this->assertIdentical(Cache::read('App.nullTest'), null);
  243. Cache::write('App.zeroTest', 0);
  244. $this->assertIdentical(Cache::read('App.zeroTest'), 0);
  245. Cache::write('App.zeroTest2', '0');
  246. $this->assertIdentical(Cache::read('App.zeroTest2'), '0');
  247. }
  248. /**
  249. * Test that failed writes cause errors to be triggered.
  250. *
  251. * @return void
  252. */
  253. public function testWriteTriggerError() {
  254. App::build(array(
  255. 'libs' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  256. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  257. ), true);
  258. Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
  259. try {
  260. Cache::write('fail', 'value', 'test_trigger');
  261. $this->fail('No exception thrown');
  262. } catch (PHPUnit_Framework_Error $e) {
  263. $this->assertTrue(true);
  264. }
  265. Cache::drop('test_trigger');
  266. App::build();
  267. }
  268. /**
  269. * testCacheDisable method
  270. *
  271. * Check that the "Cache.disable" configuration and a change to it
  272. * (even after a cache config has been setup) is taken into account.
  273. *
  274. * @return void
  275. */
  276. public function testCacheDisable() {
  277. Configure::write('Cache.disable', false);
  278. Cache::config('test_cache_disable_1', array('engine'=> 'File', 'path' => TMP . 'tests'));
  279. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  280. $this->assertIdentical(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  281. Configure::write('Cache.disable', true);
  282. $this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  283. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  284. Configure::write('Cache.disable', false);
  285. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  286. $this->assertIdentical(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
  287. Configure::write('Cache.disable', true);
  288. Cache::config('test_cache_disable_2', array('engine'=> 'File', 'path' => TMP . 'tests'));
  289. $this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  290. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  291. Configure::write('Cache.disable', false);
  292. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  293. $this->assertIdentical(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  294. Configure::write('Cache.disable', true);
  295. $this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  296. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  297. }
  298. /**
  299. * testSet method
  300. *
  301. * @return void
  302. */
  303. public function testSet() {
  304. $_cacheSet = Cache::set();
  305. Cache::set(array('duration' => '+1 year'));
  306. $data = Cache::read('test_cache');
  307. $this->assertFalse($data);
  308. $data = 'this is just a simple test of the cache system';
  309. $write = Cache::write('test_cache', $data);
  310. $this->assertTrue($write);
  311. Cache::set(array('duration' => '+1 year'));
  312. $data = Cache::read('test_cache');
  313. $this->assertEqual($data, 'this is just a simple test of the cache system');
  314. Cache::delete('test_cache');
  315. $global = Cache::settings();
  316. Cache::set($_cacheSet);
  317. }
  318. /**
  319. * test set() parameter handling for user cache configs.
  320. *
  321. * @return void
  322. */
  323. public function testSetOnAlternateConfigs() {
  324. Cache::config('file_config', array('engine' => 'File', 'prefix' => 'test_file_'));
  325. Cache::set(array('duration' => '+1 year'), 'file_config');
  326. $settings = Cache::settings('file_config');
  327. $this->assertEquals('test_file_', $settings['prefix']);
  328. $this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
  329. }
  330. }