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-2012, 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-2012, 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(Cache::config('new'), $results);
  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. 'Plugin' => 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(Cache::config('libEngine'), $result);
  86. $settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
  87. $result = Cache::config('pluginLibEngine', $settings);
  88. $this->assertEquals(Cache::config('pluginLibEngine'), $result);
  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(Cache::settings('sessions'), $result['settings']);
  146. $result = Cache::config('tests', array('engine' => 'File', 'path' => TMP . 'tests'));
  147. $this->assertEquals(Cache::settings('tests'), $result['settings']);
  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('I am cached', $result);
  161. $result = Cache::read('value_one');
  162. $this->assertEquals(null, $result);
  163. Cache::write('value_one', 'I am in default config!');
  164. $result = Cache::read('value_one');
  165. $this->assertEquals('I am in default config!', $result);
  166. $result = Cache::read('value_one', 'test_name');
  167. $this->assertEquals('I am cached', $result);
  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_something', '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. 'groups' => array()
  190. );
  191. $this->assertEquals($expected, Cache::settings('sessions'));
  192. Cache::config('sessions', $_cacheConfigSessions['settings']);
  193. }
  194. /**
  195. * test that configured returns an array of the currently configured cache
  196. * settings
  197. *
  198. * @return void
  199. */
  200. public function testConfigured() {
  201. $result = Cache::configured();
  202. $this->assertTrue(in_array('_cake_core_', $result));
  203. $this->assertTrue(in_array('default', $result));
  204. }
  205. /**
  206. * testInitSettings method
  207. *
  208. * @return void
  209. */
  210. public function testInitSettings() {
  211. $initial = Cache::settings();
  212. $override = array('engine' => 'File', 'path' => TMP . 'tests');
  213. Cache::config('for_test', $override);
  214. $settings = Cache::settings();
  215. $expecting = $override + $initial;
  216. $this->assertEquals($settings, $expecting);
  217. }
  218. /**
  219. * test that drop removes cache configs, and that further attempts to use that config
  220. * do not work.
  221. *
  222. * @return void
  223. */
  224. public function testDrop() {
  225. App::build(array(
  226. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  227. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  228. ), App::RESET);
  229. $result = Cache::drop('some_config_that_does_not_exist');
  230. $this->assertFalse($result);
  231. $_testsConfig = Cache::config('tests');
  232. $result = Cache::drop('tests');
  233. $this->assertTrue($result);
  234. Cache::config('unconfigTest', array(
  235. 'engine' => 'TestAppCache'
  236. ));
  237. $this->assertTrue(Cache::isInitialized('unconfigTest'));
  238. $this->assertTrue(Cache::drop('unconfigTest'));
  239. $this->assertFalse(Cache::isInitialized('TestAppCache'));
  240. Cache::config('tests', $_testsConfig);
  241. App::build();
  242. }
  243. /**
  244. * testWriteEmptyValues method
  245. *
  246. * @return void
  247. */
  248. public function testWriteEmptyValues() {
  249. Cache::write('App.falseTest', false);
  250. $this->assertSame(Cache::read('App.falseTest'), false);
  251. Cache::write('App.trueTest', true);
  252. $this->assertSame(Cache::read('App.trueTest'), true);
  253. Cache::write('App.nullTest', null);
  254. $this->assertSame(Cache::read('App.nullTest'), null);
  255. Cache::write('App.zeroTest', 0);
  256. $this->assertSame(Cache::read('App.zeroTest'), 0);
  257. Cache::write('App.zeroTest2', '0');
  258. $this->assertSame(Cache::read('App.zeroTest2'), '0');
  259. }
  260. /**
  261. * Test that failed writes cause errors to be triggered.
  262. *
  263. * @return void
  264. */
  265. public function testWriteTriggerError() {
  266. App::build(array(
  267. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  268. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  269. ), App::RESET);
  270. Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
  271. try {
  272. Cache::write('fail', 'value', 'test_trigger');
  273. $this->fail('No exception thrown');
  274. } catch (PHPUnit_Framework_Error $e) {
  275. $this->assertTrue(true);
  276. }
  277. Cache::drop('test_trigger');
  278. App::build();
  279. }
  280. /**
  281. * testCacheDisable method
  282. *
  283. * Check that the "Cache.disable" configuration and a change to it
  284. * (even after a cache config has been setup) is taken into account.
  285. *
  286. * @return void
  287. */
  288. public function testCacheDisable() {
  289. Configure::write('Cache.disable', false);
  290. Cache::config('test_cache_disable_1', array('engine' => 'File', 'path' => TMP . 'tests'));
  291. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  292. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  293. Configure::write('Cache.disable', true);
  294. $this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  295. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  296. Configure::write('Cache.disable', false);
  297. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  298. $this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
  299. Configure::write('Cache.disable', true);
  300. Cache::config('test_cache_disable_2', array('engine' => 'File', 'path' => TMP . 'tests'));
  301. $this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  302. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  303. Configure::write('Cache.disable', false);
  304. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  305. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  306. Configure::write('Cache.disable', true);
  307. $this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  308. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  309. }
  310. /**
  311. * testSet method
  312. *
  313. * @return void
  314. */
  315. public function testSet() {
  316. $_cacheSet = Cache::set();
  317. Cache::set(array('duration' => '+1 year'));
  318. $data = Cache::read('test_cache');
  319. $this->assertFalse($data);
  320. $data = 'this is just a simple test of the cache system';
  321. $write = Cache::write('test_cache', $data);
  322. $this->assertTrue($write);
  323. Cache::set(array('duration' => '+1 year'));
  324. $data = Cache::read('test_cache');
  325. $this->assertEquals('this is just a simple test of the cache system', $data);
  326. Cache::delete('test_cache');
  327. $global = Cache::settings();
  328. Cache::set($_cacheSet);
  329. }
  330. /**
  331. * test set() parameter handling for user cache configs.
  332. *
  333. * @return void
  334. */
  335. public function testSetOnAlternateConfigs() {
  336. Cache::config('file_config', array('engine' => 'File', 'prefix' => 'test_file_'));
  337. Cache::set(array('duration' => '+1 year'), 'file_config');
  338. $settings = Cache::settings('file_config');
  339. $this->assertEquals('test_file_', $settings['prefix']);
  340. $this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
  341. }
  342. }