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

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

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