CacheTest.php 12 KB

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