PageRenderTime 26ms CodeModel.GetById 41ms RepoModel.GetById 5ms app.codeStats 0ms

/trunk/lib/Cake/Test/Case/Cache/CacheTest.php

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