CacheTest.php 11 KB

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