PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/manuperazafa/elsartenbackend
PHP | 522 lines | 401 code | 34 blank | 87 comment | 0 complexity | d9c9de0cb72b336f8d8272561d5a9d8f MD5 | raw file
  1. <?php
  2. /**
  3. * CacheTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Cache
  15. * @since CakePHP(tm) v 1.2.0.5432
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Cache', 'Cache');
  19. /**
  20. * CacheTest class
  21. *
  22. * @package Cake.Test.Case.Cache
  23. */
  24. class CacheTest extends CakeTestCase {
  25. protected $_count = 0;
  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. Cache::drop('latest');
  46. Cache::drop('page');
  47. Cache::drop('archive');
  48. Configure::write('Cache.disable', $this->_cacheDisable);
  49. Cache::config('default', $this->_defaultCacheConfig['settings']);
  50. }
  51. /**
  52. * testConfig method
  53. *
  54. * @return void
  55. */
  56. public function testConfig() {
  57. $settings = array('engine' => 'File', 'path' => TMP . 'tests', 'prefix' => 'cake_test_');
  58. $results = Cache::config('new', $settings);
  59. $this->assertEquals(Cache::config('new'), $results);
  60. $this->assertTrue(isset($results['engine']));
  61. $this->assertTrue(isset($results['settings']));
  62. }
  63. /**
  64. * testConfigInvalidEngine method
  65. *
  66. * @expectedException CacheException
  67. * @return void
  68. */
  69. public function testConfigInvalidEngine() {
  70. $settings = array('engine' => 'Imaginary');
  71. Cache::config('imaginary', $settings);
  72. }
  73. /**
  74. * Check that no fatal errors are issued doing normal things when Cache.disable is true.
  75. *
  76. * @return void
  77. */
  78. public function testNonFatalErrorsWithCachedisable() {
  79. Configure::write('Cache.disable', true);
  80. Cache::config('test', array('engine' => 'File', 'path' => TMP, 'prefix' => 'error_test_'));
  81. Cache::write('no_save', 'Noooo!', 'test');
  82. Cache::read('no_save', 'test');
  83. Cache::delete('no_save', 'test');
  84. Cache::set('duration', '+10 minutes');
  85. Configure::write('Cache.disable', false);
  86. }
  87. /**
  88. * test configuring CacheEngines in App/libs
  89. *
  90. * @return void
  91. */
  92. public function testConfigWithLibAndPluginEngines() {
  93. App::build(array(
  94. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  95. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  96. ), App::RESET);
  97. CakePlugin::load('TestPlugin');
  98. $settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
  99. $result = Cache::config('libEngine', $settings);
  100. $this->assertEquals(Cache::config('libEngine'), $result);
  101. $settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
  102. $result = Cache::config('pluginLibEngine', $settings);
  103. $this->assertEquals(Cache::config('pluginLibEngine'), $result);
  104. Cache::drop('libEngine');
  105. Cache::drop('pluginLibEngine');
  106. App::build();
  107. CakePlugin::unload();
  108. }
  109. /**
  110. * testInvalidConfig method
  111. *
  112. * Test that the cache class doesn't cause fatal errors with a partial path
  113. *
  114. * @expectedException PHPUnit_Framework_Error_Warning
  115. * @return void
  116. */
  117. public function testInvalidConfig() {
  118. // In debug mode it would auto create the folder.
  119. $debug = Configure::read('debug');
  120. Configure::write('debug', 0);
  121. Cache::config('invalid', array(
  122. 'engine' => 'File',
  123. 'duration' => '+1 year',
  124. 'prefix' => 'testing_invalid_',
  125. 'path' => 'data/',
  126. 'serialize' => true,
  127. 'random' => 'wii'
  128. ));
  129. Cache::read('Test', 'invalid');
  130. Configure::write('debug', $debug);
  131. }
  132. /**
  133. * Test reading from a config that is undefined.
  134. *
  135. * @return void
  136. */
  137. public function testReadNonExistingConfig() {
  138. $this->assertFalse(Cache::read('key', 'totally fake'));
  139. $this->assertFalse(Cache::write('key', 'value', 'totally fake'));
  140. $this->assertFalse(Cache::increment('key', 1, 'totally fake'));
  141. $this->assertFalse(Cache::decrement('key', 1, 'totally fake'));
  142. }
  143. /**
  144. * test that trying to configure classes that don't extend CacheEngine fail.
  145. *
  146. * @expectedException CacheException
  147. * @return void
  148. */
  149. public function testAttemptingToConfigureANonCacheEngineClass() {
  150. $this->getMock('StdClass', array(), array(), 'RubbishEngine');
  151. Cache::config('Garbage', array(
  152. 'engine' => 'Rubbish'
  153. ));
  154. }
  155. /**
  156. * testConfigChange method
  157. *
  158. * @return void
  159. */
  160. public function testConfigChange() {
  161. $_cacheConfigSessions = Cache::config('sessions');
  162. $_cacheConfigTests = Cache::config('tests');
  163. $result = Cache::config('sessions', array('engine' => 'File', 'path' => TMP . 'sessions'));
  164. $this->assertEquals(Cache::settings('sessions'), $result['settings']);
  165. $result = Cache::config('tests', array('engine' => 'File', 'path' => TMP . 'tests'));
  166. $this->assertEquals(Cache::settings('tests'), $result['settings']);
  167. Cache::config('sessions', $_cacheConfigSessions['settings']);
  168. Cache::config('tests', $_cacheConfigTests['settings']);
  169. }
  170. /**
  171. * test that calling config() sets the 'default' configuration up.
  172. *
  173. * @return void
  174. */
  175. public function testConfigSettingDefaultConfigKey() {
  176. Cache::config('test_name', array('engine' => 'File', 'prefix' => 'test_name_'));
  177. Cache::write('value_one', 'I am cached', 'test_name');
  178. $result = Cache::read('value_one', 'test_name');
  179. $this->assertEquals('I am cached', $result);
  180. $result = Cache::read('value_one');
  181. $this->assertEquals(null, $result);
  182. Cache::write('value_one', 'I am in default config!');
  183. $result = Cache::read('value_one');
  184. $this->assertEquals('I am in default config!', $result);
  185. $result = Cache::read('value_one', 'test_name');
  186. $this->assertEquals('I am cached', $result);
  187. Cache::delete('value_one', 'test_name');
  188. Cache::delete('value_one', 'default');
  189. }
  190. /**
  191. * testWritingWithConfig method
  192. *
  193. * @return void
  194. */
  195. public function testWritingWithConfig() {
  196. $_cacheConfigSessions = Cache::config('sessions');
  197. Cache::write('test_something', 'this is the test data', 'tests');
  198. $expected = array(
  199. 'path' => TMP . 'sessions' . DS,
  200. 'prefix' => 'cake_',
  201. 'lock' => true,
  202. 'serialize' => true,
  203. 'duration' => 3600,
  204. 'probability' => 100,
  205. 'engine' => 'File',
  206. 'isWindows' => DIRECTORY_SEPARATOR === '\\',
  207. 'mask' => 0664,
  208. 'groups' => array()
  209. );
  210. $this->assertEquals($expected, Cache::settings('sessions'));
  211. Cache::config('sessions', $_cacheConfigSessions['settings']);
  212. }
  213. /**
  214. * testGroupConfigs method
  215. *
  216. * @return void
  217. */
  218. public function testGroupConfigs() {
  219. Cache::config('latest', array(
  220. 'duration' => 300,
  221. 'engine' => 'File',
  222. 'groups' => array(
  223. 'posts', 'comments',
  224. ),
  225. ));
  226. $expected = array(
  227. 'posts' => array('latest'),
  228. 'comments' => array('latest'),
  229. );
  230. $result = Cache::groupConfigs();
  231. $this->assertEquals($expected, $result);
  232. $result = Cache::groupConfigs('posts');
  233. $this->assertEquals(array('posts' => array('latest')), $result);
  234. Cache::config('page', array(
  235. 'duration' => 86400,
  236. 'engine' => 'File',
  237. 'groups' => array(
  238. 'posts', 'archive'
  239. ),
  240. ));
  241. $result = Cache::groupConfigs();
  242. $expected = array(
  243. 'posts' => array('latest', 'page'),
  244. 'comments' => array('latest'),
  245. 'archive' => array('page'),
  246. );
  247. $this->assertEquals($expected, $result);
  248. $result = Cache::groupConfigs('archive');
  249. $this->assertEquals(array('archive' => array('page')), $result);
  250. Cache::config('archive', array(
  251. 'duration' => 86400 * 30,
  252. 'engine' => 'File',
  253. 'groups' => array(
  254. 'posts', 'archive', 'comments',
  255. ),
  256. ));
  257. $result = Cache::groupConfigs('archive');
  258. $this->assertEquals(array('archive' => array('archive', 'page')), $result);
  259. }
  260. /**
  261. * testGroupConfigsThrowsException method
  262. *
  263. * @expectedException CacheException
  264. * @return void
  265. */
  266. public function testGroupConfigsThrowsException() {
  267. Cache::groupConfigs('bogus');
  268. }
  269. /**
  270. * test that configured returns an array of the currently configured cache
  271. * settings
  272. *
  273. * @return void
  274. */
  275. public function testConfigured() {
  276. $result = Cache::configured();
  277. $this->assertTrue(in_array('_cake_core_', $result));
  278. $this->assertTrue(in_array('default', $result));
  279. }
  280. /**
  281. * testInitSettings method
  282. *
  283. * @return void
  284. */
  285. public function testInitSettings() {
  286. $initial = Cache::settings();
  287. $override = array('engine' => 'File', 'path' => TMP . 'tests');
  288. Cache::config('for_test', $override);
  289. $settings = Cache::settings();
  290. $expecting = $override + $initial;
  291. $this->assertEquals($settings, $expecting);
  292. }
  293. /**
  294. * test that drop removes cache configs, and that further attempts to use that config
  295. * do not work.
  296. *
  297. * @return void
  298. */
  299. public function testDrop() {
  300. App::build(array(
  301. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  302. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  303. ), App::RESET);
  304. $result = Cache::drop('some_config_that_does_not_exist');
  305. $this->assertFalse($result);
  306. $_testsConfig = Cache::config('tests');
  307. $result = Cache::drop('tests');
  308. $this->assertTrue($result);
  309. Cache::config('unconfigTest', array(
  310. 'engine' => 'TestAppCache'
  311. ));
  312. $this->assertTrue(Cache::isInitialized('unconfigTest'));
  313. $this->assertTrue(Cache::drop('unconfigTest'));
  314. $this->assertFalse(Cache::isInitialized('TestAppCache'));
  315. Cache::config('tests', $_testsConfig);
  316. App::build();
  317. }
  318. /**
  319. * testWriteEmptyValues method
  320. *
  321. * @return void
  322. */
  323. public function testWriteEmptyValues() {
  324. Cache::write('App.falseTest', false);
  325. $this->assertFalse(Cache::read('App.falseTest'));
  326. Cache::write('App.trueTest', true);
  327. $this->assertTrue(Cache::read('App.trueTest'));
  328. Cache::write('App.nullTest', null);
  329. $this->assertNull(Cache::read('App.nullTest'));
  330. Cache::write('App.zeroTest', 0);
  331. $this->assertSame(Cache::read('App.zeroTest'), 0);
  332. Cache::write('App.zeroTest2', '0');
  333. $this->assertSame(Cache::read('App.zeroTest2'), '0');
  334. }
  335. /**
  336. * Test that failed writes cause errors to be triggered.
  337. *
  338. * @return void
  339. */
  340. public function testWriteTriggerError() {
  341. App::build(array(
  342. 'Lib' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Lib' . DS),
  343. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  344. ), App::RESET);
  345. Cache::config('test_trigger', array('engine' => 'TestAppCache', 'prefix' => ''));
  346. try {
  347. Cache::write('fail', 'value', 'test_trigger');
  348. $this->fail('No exception thrown');
  349. } catch (PHPUnit_Framework_Error $e) {
  350. $this->assertTrue(true);
  351. }
  352. Cache::drop('test_trigger');
  353. App::build();
  354. }
  355. /**
  356. * testCacheDisable method
  357. *
  358. * Check that the "Cache.disable" configuration and a change to it
  359. * (even after a cache config has been setup) is taken into account.
  360. *
  361. * @return void
  362. */
  363. public function testCacheDisable() {
  364. Configure::write('Cache.disable', false);
  365. Cache::config('test_cache_disable_1', array('engine' => 'File', 'path' => TMP . 'tests'));
  366. $this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
  367. $this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');
  368. Configure::write('Cache.disable', true);
  369. $this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
  370. $this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));
  371. Configure::write('Cache.disable', false);
  372. $this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
  373. $this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');
  374. Configure::write('Cache.disable', true);
  375. Cache::config('test_cache_disable_2', array('engine' => 'File', 'path' => TMP . 'tests'));
  376. $this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
  377. $this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));
  378. Configure::write('Cache.disable', false);
  379. $this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
  380. $this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');
  381. Configure::write('Cache.disable', true);
  382. $this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
  383. $this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
  384. }
  385. /**
  386. * testSet method
  387. *
  388. * @return void
  389. */
  390. public function testSet() {
  391. $_cacheSet = Cache::set();
  392. Cache::set(array('duration' => '+1 year'));
  393. $data = Cache::read('test_cache');
  394. $this->assertFalse($data);
  395. $data = 'this is just a simple test of the cache system';
  396. $write = Cache::write('test_cache', $data);
  397. $this->assertTrue($write);
  398. Cache::set(array('duration' => '+1 year'));
  399. $data = Cache::read('test_cache');
  400. $this->assertEquals('this is just a simple test of the cache system', $data);
  401. Cache::delete('test_cache');
  402. Cache::settings();
  403. Cache::set($_cacheSet);
  404. }
  405. /**
  406. * test set() parameter handling for user cache configs.
  407. *
  408. * @return void
  409. */
  410. public function testSetOnAlternateConfigs() {
  411. Cache::config('file_config', array('engine' => 'File', 'prefix' => 'test_file_'));
  412. Cache::set(array('duration' => '+1 year'), 'file_config');
  413. $settings = Cache::settings('file_config');
  414. $this->assertEquals('test_file_', $settings['prefix']);
  415. $this->assertEquals(strtotime('+1 year') - time(), $settings['duration']);
  416. }
  417. /**
  418. * test remember method.
  419. *
  420. * @return void
  421. */
  422. public function testRemember() {
  423. $expected = 'This is some data 0';
  424. $result = Cache::remember('test_key', array($this, 'cacher'), 'default');
  425. $this->assertEquals($expected, $result);
  426. $this->_count = 1;
  427. $result = Cache::remember('test_key', array($this, 'cacher'), 'default');
  428. $this->assertEquals($expected, $result);
  429. }
  430. /**
  431. * Method for testing Cache::remember()
  432. *
  433. * @return string
  434. */
  435. public function cacher() {
  436. return 'This is some data ' . $this->_count;
  437. }
  438. }