CacheTest.php 12 KB

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