PageRenderTime 88ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/cache/file.test.php

https://github.com/purushoth85/wildflower
PHP | 355 lines | 216 code | 37 blank | 102 comment | 2 complexity | 0dd616ec6433e09f6246c41bc8181a57 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * FileEngineTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  18. * @package cake
  19. * @subpackage cake.tests.cases.libs.cache
  20. * @since CakePHP(tm) v 1.2.0.5434
  21. * @version $Revision$
  22. * @modifiedby $LastChangedBy$
  23. * @lastmodified $Date$
  24. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  25. */
  26. if (!class_exists('Cache')) {
  27. require LIBS . 'cache.php';
  28. }
  29. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  30. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  31. }
  32. /**
  33. * FileEngineTest class
  34. *
  35. * @package cake
  36. * @subpackage cake.tests.cases.libs.cache
  37. */
  38. class FileEngineTest extends CakeTestCase {
  39. /**
  40. * config property
  41. *
  42. * @var array
  43. * @access public
  44. */
  45. var $config = array();
  46. /**
  47. * startCase method
  48. *
  49. * @access public
  50. * @return void
  51. */
  52. function startCase() {
  53. $this->_cacheDisable = Configure::read('Cache.disable');
  54. $this->_cacheConfig = Cache::config('default');
  55. Configure::write('Cache.disable', false);
  56. Cache::config('default', array('engine' => 'File', 'path' => CACHE));
  57. }
  58. /**
  59. * endCase method
  60. *
  61. * @access public
  62. * @return void
  63. */
  64. function endCase() {
  65. Configure::write('Cache.disable', $this->_cacheDisable);
  66. Cache::config('default', $this->_cacheConfig['settings']);
  67. }
  68. /**
  69. * testCacheDirChange method
  70. *
  71. * @access public
  72. * @return void
  73. */
  74. function testCacheDirChange() {
  75. $result = Cache::config('sessions', array('engine'=> 'File', 'path' => TMP . 'sessions'));
  76. $this->assertEqual($result['settings'], Cache::settings('File'));
  77. $this->assertNotEqual($result, Cache::settings('File'));
  78. $result = Cache::config('tests', array('engine'=> 'File', 'path' => TMP . 'tests'));
  79. $this->assertEqual($result['settings'], Cache::settings('File'));
  80. $this->assertNotEqual($result, Cache::settings('File'));
  81. }
  82. /**
  83. * testReadAndWriteCache method
  84. *
  85. * @access public
  86. * @return void
  87. */
  88. function testReadAndWriteCache() {
  89. Cache::config('default');
  90. $result = Cache::write(null, 'here');
  91. $this->assertFalse($result);
  92. Cache::set(array('duration' => 1));
  93. $result = Cache::read('test');
  94. $expecting = '';
  95. $this->assertEqual($result, $expecting);
  96. $data = 'this is a test of the emergency broadcasting system';
  97. $result = Cache::write('test', $data);
  98. $this->assertTrue(file_exists(CACHE . 'cake_test'));
  99. $result = Cache::read('test');
  100. $expecting = $data;
  101. $this->assertEqual($result, $expecting);
  102. Cache::delete('test');
  103. }
  104. /**
  105. * testExpiry method
  106. *
  107. * @access public
  108. * @return void
  109. */
  110. function testExpiry() {
  111. Cache::set(array('duration' => 1));
  112. $result = Cache::read('test');
  113. $this->assertFalse($result);
  114. $data = 'this is a test of the emergency broadcasting system';
  115. $result = Cache::write('other_test', $data);
  116. $this->assertTrue($result);
  117. sleep(2);
  118. $result = Cache::read('other_test');
  119. $this->assertFalse($result);
  120. Cache::set(array('duration' => "+1 second"));
  121. $data = 'this is a test of the emergency broadcasting system';
  122. $result = Cache::write('other_test', $data);
  123. $this->assertTrue($result);
  124. sleep(2);
  125. $result = Cache::read('other_test');
  126. $this->assertFalse($result);
  127. }
  128. /**
  129. * testDeleteCache method
  130. *
  131. * @access public
  132. * @return void
  133. */
  134. function testDeleteCache() {
  135. $data = 'this is a test of the emergency broadcasting system';
  136. $result = Cache::write('delete_test', $data);
  137. $this->assertTrue($result);
  138. $result = Cache::delete('delete_test');
  139. $this->assertTrue($result);
  140. $this->assertFalse(file_exists(TMP . 'tests' . DS . 'delete_test'));
  141. $result = Cache::delete('delete_test');
  142. $this->assertFalse($result);
  143. }
  144. /**
  145. * testSerialize method
  146. *
  147. * @access public
  148. * @return void
  149. */
  150. function testSerialize() {
  151. Cache::engine('File', array('serialize' => true));
  152. $data = 'this is a test of the emergency broadcasting system';
  153. $write = Cache::write('serialize_test', $data, 1);
  154. $this->assertTrue($write);
  155. Cache::engine('File', array('serialize' => false));
  156. $read = Cache::read('serialize_test');
  157. $newread = Cache::read('serialize_test');
  158. $delete = Cache::delete('serialize_test');
  159. $this->assertIdentical($read, serialize($data));
  160. $this->assertIdentical(unserialize($newread), $data);
  161. }
  162. /**
  163. * testClear method
  164. *
  165. * @access public
  166. * @return void
  167. */
  168. function testClear() {
  169. Cache::engine('File', array('duration' => 1));
  170. $data = 'this is a test of the emergency broadcasting system';
  171. $write = Cache::write('serialize_test1', $data);
  172. $write = Cache::write('serialize_test2', $data);
  173. $write = Cache::write('serialize_test3', $data);
  174. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
  175. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
  176. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
  177. sleep(2);
  178. $result = Cache::clear(true);
  179. $this->assertTrue($result);
  180. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
  181. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
  182. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
  183. $data = 'this is a test of the emergency broadcasting system';
  184. $write = Cache::write('serialize_test1', $data);
  185. $write = Cache::write('serialize_test2', $data);
  186. $write = Cache::write('serialize_test3', $data);
  187. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test1'));
  188. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test2'));
  189. $this->assertTrue(file_exists(CACHE . 'cake_serialize_test3'));
  190. $result = Cache::clear();
  191. $this->assertTrue($result);
  192. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test1'));
  193. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test2'));
  194. $this->assertFalse(file_exists(CACHE . 'cake_serialize_test3'));
  195. $result = Cache::engine('File', array('path' => CACHE . 'views'));
  196. $data = 'this is a test of the emergency broadcasting system';
  197. $write = Cache::write('controller_view_1', $data);
  198. $write = Cache::write('controller_view_2', $data);
  199. $write = Cache::write('controller_view_3', $data);
  200. $write = Cache::write('controller_view_10', $data);
  201. $write = Cache::write('controller_view_11', $data);
  202. $write = Cache::write('controller_view_12', $data);
  203. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
  204. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
  205. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
  206. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
  207. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
  208. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
  209. clearCache('controller_view_1', 'views', '');
  210. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
  211. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
  212. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
  213. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
  214. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
  215. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
  216. clearCache('controller_view', 'views', '');
  217. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
  218. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
  219. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
  220. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
  221. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
  222. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
  223. $write = Cache::write('controller_view_1', $data);
  224. $write = Cache::write('controller_view_2', $data);
  225. $write = Cache::write('controller_view_3', $data);
  226. $write = Cache::write('controller_view_10', $data);
  227. $write = Cache::write('controller_view_11', $data);
  228. $write = Cache::write('controller_view_12', $data);
  229. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
  230. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
  231. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
  232. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
  233. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
  234. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
  235. clearCache(array('controller_view_2', 'controller_view_11', 'controller_view_12'), 'views', '');
  236. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_1'));
  237. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_2'));
  238. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_3'));
  239. $this->assertTrue(file_exists(CACHE . 'views'. DS . 'cake_controller_view_10'));
  240. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_11'));
  241. $this->assertFalse(file_exists(CACHE . 'views'. DS . 'cake_controller_view_12'));
  242. clearCache('controller_view');
  243. Cache::engine('File', array('path' => CACHE));
  244. }
  245. /**
  246. * testKeyPath method
  247. *
  248. * @access public
  249. * @return void
  250. */
  251. function testKeyPath() {
  252. $result = Cache::write('views.countries.something', 'here');
  253. $this->assertTrue($result);
  254. $this->assertTrue(file_exists(CACHE . 'cake_views_countries_something'));
  255. $result = Cache::read('views.countries.something');
  256. $this->assertEqual($result, 'here');
  257. $result = Cache::clear();
  258. $this->assertTrue($result);
  259. }
  260. /**
  261. * testRemoveWindowsSlashesFromCache method
  262. *
  263. * @access public
  264. * @return void
  265. */
  266. function testRemoveWindowsSlashesFromCache() {
  267. Cache::engine('File', array('isWindows' => true, 'prefix' => null, 'path' => TMP));
  268. $expected = array (
  269. 'C:\dev\prj2\sites\cake\libs' => array(
  270. 0 => 'C:\dev\prj2\sites\cake\libs', 1 => 'C:\dev\prj2\sites\cake\libs\view',
  271. 2 => 'C:\dev\prj2\sites\cake\libs\view\scaffolds', 3 => 'C:\dev\prj2\sites\cake\libs\view\pages',
  272. 4 => 'C:\dev\prj2\sites\cake\libs\view\layouts', 5 => 'C:\dev\prj2\sites\cake\libs\view\layouts\xml',
  273. 6 => 'C:\dev\prj2\sites\cake\libs\view\layouts\rss', 7 => 'C:\dev\prj2\sites\cake\libs\view\layouts\js',
  274. 8 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email', 9 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\text',
  275. 10 => 'C:\dev\prj2\sites\cake\libs\view\layouts\email\html', 11 => 'C:\dev\prj2\sites\cake\libs\view\helpers',
  276. 12 => 'C:\dev\prj2\sites\cake\libs\view\errors', 13 => 'C:\dev\prj2\sites\cake\libs\view\elements',
  277. 14 => 'C:\dev\prj2\sites\cake\libs\view\elements\email', 15 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\text',
  278. 16 => 'C:\dev\prj2\sites\cake\libs\view\elements\email\html', 17 => 'C:\dev\prj2\sites\cake\libs\model',
  279. 18 => 'C:\dev\prj2\sites\cake\libs\model\datasources', 19 => 'C:\dev\prj2\sites\cake\libs\model\datasources\dbo',
  280. 20 => 'C:\dev\prj2\sites\cake\libs\model\behaviors', 21 => 'C:\dev\prj2\sites\cake\libs\controller',
  281. 22 => 'C:\dev\prj2\sites\cake\libs\controller\components', 23 => 'C:\dev\prj2\sites\cake\libs\cache'),
  282. 'C:\dev\prj2\sites\main_site\vendors' => array(
  283. 0 => 'C:\dev\prj2\sites\main_site\vendors', 1 => 'C:\dev\prj2\sites\main_site\vendors\shells',
  284. 2 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates', 3 => 'C:\dev\prj2\sites\main_site\vendors\shells\templates\cdc_project',
  285. 4 => 'C:\dev\prj2\sites\main_site\vendors\shells\tasks', 5 => 'C:\dev\prj2\sites\main_site\vendors\js',
  286. 6 => 'C:\dev\prj2\sites\main_site\vendors\css'),
  287. 'C:\dev\prj2\sites\vendors' => array(
  288. 0 => 'C:\dev\prj2\sites\vendors', 1 => 'C:\dev\prj2\sites\vendors\simpletest',
  289. 2 => 'C:\dev\prj2\sites\vendors\simpletest\test', 3 => 'C:\dev\prj2\sites\vendors\simpletest\test\support',
  290. 4 => 'C:\dev\prj2\sites\vendors\simpletest\test\support\collector', 5 => 'C:\dev\prj2\sites\vendors\simpletest\extensions',
  291. 6 => 'C:\dev\prj2\sites\vendors\simpletest\extensions\testdox', 7 => 'C:\dev\prj2\sites\vendors\simpletest\docs',
  292. 8 => 'C:\dev\prj2\sites\vendors\simpletest\docs\fr', 9 => 'C:\dev\prj2\sites\vendors\simpletest\docs\en'),
  293. 'C:\dev\prj2\sites\main_site\views\helpers' => array(
  294. 0 => 'C:\dev\prj2\sites\main_site\views\helpers'));
  295. $data = Cache::write('test_dir_map', $expected);
  296. $data = Cache::read('test_dir_map');
  297. Cache::delete('test_dir_map');
  298. $this->assertEqual($expected, $data);
  299. }
  300. /**
  301. * testWriteQuotedString method
  302. *
  303. * @access public
  304. * @return void
  305. */
  306. function testWriteQuotedString() {
  307. Cache::engine('File', array('path' => TMP . 'tests'));
  308. Cache::write('App.doubleQuoteTest', '"this is a quoted string"');
  309. $this->assertIdentical(Cache::read('App.doubleQuoteTest'), '"this is a quoted string"');
  310. Cache::write('App.singleQuoteTest', "'this is a quoted string'");
  311. $this->assertIdentical(Cache::read('App.singleQuoteTest'), "'this is a quoted string'");
  312. Cache::engine('File', array('isWindows' => true, 'path' => TMP . 'tests'));
  313. $this->assertIdentical(Cache::read('App.doubleQuoteTest'), '"this is a quoted string"');
  314. Cache::write('App.singleQuoteTest', "'this is a quoted string'");
  315. $this->assertIdentical(Cache::read('App.singleQuoteTest'), "'this is a quoted string'");
  316. }
  317. }
  318. ?>