FileEngineTest.php 12 KB

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