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

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

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