PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php

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