PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Utility/FileTest.php

https://github.com/gustavor/lore
PHP | 486 lines | 344 code | 43 blank | 99 comment | 13 complexity | cedda28b993ec0ecdbe46c48e6aebdcf MD5 | raw file
  1. <?php
  2. /**
  3. * FileTest 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.Utility
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('File', 'Utility');
  20. App::uses('Folder', 'Utility');
  21. /**
  22. * FileTest class
  23. *
  24. * @package Cake.Test.Case.Utility
  25. */
  26. class FileTest extends CakeTestCase {
  27. /**
  28. * File property
  29. *
  30. * @var mixed null
  31. */
  32. public $File = null;
  33. /**
  34. * setup the test case
  35. *
  36. * @return void
  37. */
  38. public function setUp() {
  39. parent::setUp();
  40. $file = __FILE__;
  41. $this->File = new File($file);
  42. }
  43. /**
  44. * tear down for test.
  45. *
  46. * @return void
  47. */
  48. public function teardown() {
  49. parent::teardown();
  50. $this->File->close();
  51. unset($this->File);
  52. }
  53. /**
  54. * testBasic method
  55. *
  56. * @return void
  57. */
  58. public function testBasic() {
  59. $file = __FILE__;
  60. $result = $this->File->pwd();
  61. $expecting = $file;
  62. $this->assertEqual($result, $expecting);
  63. $result = $this->File->name;
  64. $expecting = basename(__FILE__);
  65. $this->assertEqual($result, $expecting);
  66. $result = $this->File->info();
  67. $expecting = array(
  68. 'dirname' => dirname(__FILE__), 'basename' => basename(__FILE__),
  69. 'extension' => 'php', 'filename' =>'FileTest'
  70. );
  71. $this->assertEqual($result, $expecting);
  72. $result = $this->File->ext();
  73. $expecting = 'php';
  74. $this->assertEqual($result, $expecting);
  75. $result = $this->File->name();
  76. $expecting = 'FileTest';
  77. $this->assertEqual($result, $expecting);
  78. $result = $this->File->md5();
  79. $expecting = md5_file($file);
  80. $this->assertEqual($result, $expecting);
  81. $result = $this->File->md5(true);
  82. $expecting = md5_file($file);
  83. $this->assertEqual($result, $expecting);
  84. $result = $this->File->size();
  85. $expecting = filesize($file);
  86. $this->assertEqual($result, $expecting);
  87. $result = $this->File->owner();
  88. $expecting = fileowner($file);
  89. $this->assertEqual($result, $expecting);
  90. $result = $this->File->group();
  91. $expecting = filegroup($file);
  92. $this->assertEqual($result, $expecting);
  93. $result = $this->File->Folder();
  94. $this->assertIsA($result, 'Folder');
  95. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
  96. $result = $this->File->perms();
  97. $expecting = '0644';
  98. $this->assertEqual($result, $expecting);
  99. }
  100. /**
  101. * testRead method
  102. *
  103. * @return void
  104. */
  105. public function testRead() {
  106. $file = __FILE__;
  107. $this->File = new File($file);
  108. $result = $this->File->read();
  109. $expecting = file_get_contents(__FILE__);
  110. $this->assertEqual($result, $expecting);
  111. $this->assertTrue(!is_resource($this->File->handle));
  112. $this->File->lock = true;
  113. $result = $this->File->read();
  114. $expecting = file_get_contents(__FILE__);
  115. $this->assertEqual($result, trim($expecting));
  116. $this->File->lock = null;
  117. $data = $expecting;
  118. $expecting = substr($data, 0, 3);
  119. $result = $this->File->read(3);
  120. $this->assertEqual($result, $expecting);
  121. $this->assertTrue(is_resource($this->File->handle));
  122. $expecting = substr($data, 3, 3);
  123. $result = $this->File->read(3);
  124. $this->assertEqual($result, $expecting);
  125. }
  126. /**
  127. * testOffset method
  128. *
  129. * @return void
  130. */
  131. public function testOffset() {
  132. $this->File->close();
  133. $result = $this->File->offset();
  134. $this->assertFalse($result);
  135. $this->assertFalse(is_resource($this->File->handle));
  136. $success = $this->File->offset(0);
  137. $this->assertTrue($success);
  138. $this->assertTrue(is_resource($this->File->handle));
  139. $result = $this->File->offset();
  140. $expecting = 0;
  141. $this->assertIdentical($result, $expecting);
  142. $data = file_get_contents(__FILE__);
  143. $success = $this->File->offset(5);
  144. $expecting = substr($data, 5, 3);
  145. $result = $this->File->read(3);
  146. $this->assertTrue($success);
  147. $this->assertEqual($result, $expecting);
  148. $result = $this->File->offset();
  149. $expecting = 5+3;
  150. $this->assertIdentical($result, $expecting);
  151. }
  152. /**
  153. * testOpen method
  154. *
  155. * @return void
  156. */
  157. public function testOpen() {
  158. $this->File->handle = null;
  159. $r = $this->File->open();
  160. $this->assertTrue(is_resource($this->File->handle));
  161. $this->assertTrue($r);
  162. $handle = $this->File->handle;
  163. $r = $this->File->open();
  164. $this->assertTrue($r);
  165. $this->assertTrue($handle === $this->File->handle);
  166. $this->assertTrue(is_resource($this->File->handle));
  167. $r = $this->File->open('r', true);
  168. $this->assertTrue($r);
  169. $this->assertFalse($handle === $this->File->handle);
  170. $this->assertTrue(is_resource($this->File->handle));
  171. }
  172. /**
  173. * testClose method
  174. *
  175. * @return void
  176. */
  177. public function testClose() {
  178. $this->File->handle = null;
  179. $this->assertFalse(is_resource($this->File->handle));
  180. $this->assertTrue($this->File->close());
  181. $this->assertFalse(is_resource($this->File->handle));
  182. $this->File->handle = fopen(__FILE__, 'r');
  183. $this->assertTrue(is_resource($this->File->handle));
  184. $this->assertTrue($this->File->close());
  185. $this->assertFalse(is_resource($this->File->handle));
  186. }
  187. /**
  188. * testCreate method
  189. *
  190. * @return void
  191. */
  192. public function testCreate() {
  193. $tmpFile = TMP.'tests'.DS.'cakephp.file.test.tmp';
  194. $File = new File($tmpFile, true, 0777);
  195. $this->assertTrue($File->exists());
  196. }
  197. /**
  198. * testOpeningNonExistantFileCreatesIt method
  199. *
  200. * @return void
  201. */
  202. public function testOpeningNonExistantFileCreatesIt() {
  203. $someFile = new File(TMP . 'some_file.txt', false);
  204. $this->assertTrue($someFile->open());
  205. $this->assertEqual($someFile->read(), '');
  206. $someFile->close();
  207. $someFile->delete();
  208. }
  209. /**
  210. * testPrepare method
  211. *
  212. * @return void
  213. */
  214. public function testPrepare() {
  215. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  216. if (DS == '\\') {
  217. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  218. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  219. } else {
  220. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  221. }
  222. $this->assertIdentical(File::prepare($string), $expected);
  223. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  224. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  225. $this->assertIdentical(File::prepare($string, true), $expected);
  226. }
  227. /**
  228. * testReadable method
  229. *
  230. * @return void
  231. */
  232. public function testReadable() {
  233. $someFile = new File(TMP . 'some_file.txt', false);
  234. $this->assertTrue($someFile->open());
  235. $this->assertTrue($someFile->readable());
  236. $someFile->close();
  237. $someFile->delete();
  238. }
  239. /**
  240. * testWritable method
  241. *
  242. * @return void
  243. */
  244. public function testWritable() {
  245. $someFile = new File(TMP . 'some_file.txt', false);
  246. $this->assertTrue($someFile->open());
  247. $this->assertTrue($someFile->writable());
  248. $someFile->close();
  249. $someFile->delete();
  250. }
  251. /**
  252. * testExecutable method
  253. *
  254. * @return void
  255. */
  256. public function testExecutable() {
  257. $someFile = new File(TMP . 'some_file.txt', false);
  258. $this->assertTrue($someFile->open());
  259. $this->assertFalse($someFile->executable());
  260. $someFile->close();
  261. $someFile->delete();
  262. }
  263. /**
  264. * testLastAccess method
  265. *
  266. * @return void
  267. */
  268. public function testLastAccess() {
  269. $someFile = new File(TMP . 'some_file.txt', false);
  270. $this->assertFalse($someFile->lastAccess());
  271. $this->assertTrue($someFile->open());
  272. $this->assertEqual($someFile->lastAccess(), time());
  273. $someFile->close();
  274. $someFile->delete();
  275. }
  276. /**
  277. * testLastChange method
  278. *
  279. * @return void
  280. */
  281. public function testLastChange() {
  282. $someFile = new File(TMP . 'some_file.txt', false);
  283. $this->assertFalse($someFile->lastChange());
  284. $this->assertTrue($someFile->open('r+'));
  285. $this->assertEqual($someFile->lastChange(), time());
  286. $someFile->write('something');
  287. $this->assertEqual($someFile->lastChange(), time());
  288. $someFile->close();
  289. $someFile->delete();
  290. }
  291. /**
  292. * testWrite method
  293. *
  294. * @return void
  295. */
  296. public function testWrite() {
  297. if (!$tmpFile = $this->_getTmpFile()) {
  298. return false;
  299. };
  300. if (file_exists($tmpFile)) {
  301. unlink($tmpFile);
  302. }
  303. $TmpFile = new File($tmpFile);
  304. $this->assertFalse(file_exists($tmpFile));
  305. $this->assertFalse(is_resource($TmpFile->handle));
  306. $testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
  307. foreach ($testData as $data) {
  308. $r = $TmpFile->write($data);
  309. $this->assertTrue($r);
  310. $this->assertTrue(file_exists($tmpFile));
  311. $this->assertEqual($data, file_get_contents($tmpFile));
  312. $this->assertTrue(is_resource($TmpFile->handle));
  313. $TmpFile->close();
  314. }
  315. unlink($tmpFile);
  316. }
  317. /**
  318. * testAppend method
  319. *
  320. * @return void
  321. */
  322. public function testAppend() {
  323. if (!$tmpFile = $this->_getTmpFile()) {
  324. return false;
  325. };
  326. if (file_exists($tmpFile)) {
  327. unlink($tmpFile);
  328. }
  329. $TmpFile = new File($tmpFile);
  330. $this->assertFalse(file_exists($tmpFile));
  331. $fragments = array('CakePHP\'s', ' test suite', ' was here ...', '');
  332. $data = null;
  333. foreach ($fragments as $fragment) {
  334. $r = $TmpFile->append($fragment);
  335. $this->assertTrue($r);
  336. $this->assertTrue(file_exists($tmpFile));
  337. $data = $data.$fragment;
  338. $this->assertEqual($data, file_get_contents($tmpFile));
  339. $TmpFile->close();
  340. }
  341. }
  342. /**
  343. * testDelete method
  344. *
  345. * @return void
  346. */
  347. public function testDelete() {
  348. if (!$tmpFile = $this->_getTmpFile()) {
  349. return false;
  350. }
  351. if (!file_exists($tmpFile)) {
  352. touch($tmpFile);
  353. }
  354. $TmpFile = new File($tmpFile);
  355. $this->assertTrue(file_exists($tmpFile));
  356. $result = $TmpFile->delete();
  357. $this->assertTrue($result);
  358. $this->assertFalse(file_exists($tmpFile));
  359. $TmpFile = new File('/this/does/not/exist');
  360. $result = $TmpFile->delete();
  361. $this->assertFalse($result);
  362. }
  363. /**
  364. * Windows has issues unlinking files if there are
  365. * active filehandles open.
  366. *
  367. * @return void
  368. */
  369. function testDeleteAfterRead() {
  370. if (!$tmpFile = $this->_getTmpFile()) {
  371. return false;
  372. }
  373. if (!file_exists($tmpFile)) {
  374. touch($tmpFile);
  375. }
  376. $file =& new File($tmpFile);
  377. $file->read();
  378. $this->assertTrue($file->delete());
  379. }
  380. /**
  381. * testCopy method
  382. *
  383. * @return void
  384. */
  385. public function testCopy() {
  386. $dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  387. $file = __FILE__;
  388. $this->File = new File($file);
  389. $result = $this->File->copy($dest);
  390. $this->assertTrue($result);
  391. $result = $this->File->copy($dest, true);
  392. $this->assertTrue($result);
  393. $result = $this->File->copy($dest, false);
  394. $this->assertFalse($result);
  395. $this->File->close();
  396. unlink($dest);
  397. $TmpFile = new File('/this/does/not/exist');
  398. $result = $TmpFile->copy($dest);
  399. $this->assertFalse($result);
  400. $TmpFile->close();
  401. }
  402. /**
  403. * getTmpFile method
  404. *
  405. * @param bool $paintSkip
  406. * @return void
  407. */
  408. function _getTmpFile($paintSkip = true) {
  409. $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  410. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  411. return $tmpFile;
  412. };
  413. if ($paintSkip) {
  414. $trace = debug_backtrace();
  415. $caller = $trace[0]['function'];
  416. $shortPath = dirname($tmpFile);
  417. $message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  418. $this->markTestSkipped($message);
  419. }
  420. return false;
  421. }
  422. }