FileTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. * tearDown method
  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->assertEquals($expecting, $result);
  63. $result = $this->File->name;
  64. $expecting = basename(__FILE__);
  65. $this->assertEquals($expecting, $result);
  66. $result = $this->File->info();
  67. $expecting = array(
  68. 'dirname' => dirname(__FILE__),
  69. 'basename' => basename(__FILE__),
  70. 'extension' => 'php',
  71. 'filename' =>'FileTest',
  72. 'filesize' => filesize($file),
  73. 'mime' => 'text/x-php'
  74. );
  75. $this->assertEquals($expecting, $result);
  76. $result = $this->File->ext();
  77. $expecting = 'php';
  78. $this->assertEquals($expecting, $result);
  79. $result = $this->File->name();
  80. $expecting = 'FileTest';
  81. $this->assertEquals($expecting, $result);
  82. $result = $this->File->md5();
  83. $expecting = md5_file($file);
  84. $this->assertEquals($expecting, $result);
  85. $result = $this->File->md5(true);
  86. $expecting = md5_file($file);
  87. $this->assertEquals($expecting, $result);
  88. $result = $this->File->size();
  89. $expecting = filesize($file);
  90. $this->assertEquals($expecting, $result);
  91. $result = $this->File->owner();
  92. $expecting = fileowner($file);
  93. $this->assertEquals($expecting, $result);
  94. $result = $this->File->group();
  95. $expecting = filegroup($file);
  96. $this->assertEquals($expecting, $result);
  97. $result = $this->File->Folder();
  98. $this->assertInstanceOf('Folder', $result);
  99. $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
  100. $result = $this->File->perms();
  101. $expecting = decoct(0644 & ~umask());
  102. $this->assertEquals($expecting, $result);
  103. }
  104. /**
  105. * testRead method
  106. *
  107. * @return void
  108. */
  109. public function testRead() {
  110. $file = __FILE__;
  111. $this->File = new File($file);
  112. $result = $this->File->read();
  113. $expecting = file_get_contents(__FILE__);
  114. $this->assertEquals($expecting, $result);
  115. $this->assertTrue(!is_resource($this->File->handle));
  116. $this->File->lock = true;
  117. $result = $this->File->read();
  118. $expecting = file_get_contents(__FILE__);
  119. $this->assertEquals($result, trim($expecting));
  120. $this->File->lock = null;
  121. $data = $expecting;
  122. $expecting = substr($data, 0, 3);
  123. $result = $this->File->read(3);
  124. $this->assertEquals($expecting, $result);
  125. $this->assertTrue(is_resource($this->File->handle));
  126. $expecting = substr($data, 3, 3);
  127. $result = $this->File->read(3);
  128. $this->assertEquals($expecting, $result);
  129. }
  130. /**
  131. * testOffset method
  132. *
  133. * @return void
  134. */
  135. public function testOffset() {
  136. $this->File->close();
  137. $result = $this->File->offset();
  138. $this->assertFalse($result);
  139. $this->assertFalse(is_resource($this->File->handle));
  140. $success = $this->File->offset(0);
  141. $this->assertTrue($success);
  142. $this->assertTrue(is_resource($this->File->handle));
  143. $result = $this->File->offset();
  144. $expecting = 0;
  145. $this->assertSame($result, $expecting);
  146. $data = file_get_contents(__FILE__);
  147. $success = $this->File->offset(5);
  148. $expecting = substr($data, 5, 3);
  149. $result = $this->File->read(3);
  150. $this->assertTrue($success);
  151. $this->assertEquals($expecting, $result);
  152. $result = $this->File->offset();
  153. $expecting = 5+3;
  154. $this->assertSame($result, $expecting);
  155. }
  156. /**
  157. * testOpen method
  158. *
  159. * @return void
  160. */
  161. public function testOpen() {
  162. $this->File->handle = null;
  163. $r = $this->File->open();
  164. $this->assertTrue(is_resource($this->File->handle));
  165. $this->assertTrue($r);
  166. $handle = $this->File->handle;
  167. $r = $this->File->open();
  168. $this->assertTrue($r);
  169. $this->assertTrue($handle === $this->File->handle);
  170. $this->assertTrue(is_resource($this->File->handle));
  171. $r = $this->File->open('r', true);
  172. $this->assertTrue($r);
  173. $this->assertFalse($handle === $this->File->handle);
  174. $this->assertTrue(is_resource($this->File->handle));
  175. }
  176. /**
  177. * testClose method
  178. *
  179. * @return void
  180. */
  181. public function testClose() {
  182. $this->File->handle = null;
  183. $this->assertFalse(is_resource($this->File->handle));
  184. $this->assertTrue($this->File->close());
  185. $this->assertFalse(is_resource($this->File->handle));
  186. $this->File->handle = fopen(__FILE__, 'r');
  187. $this->assertTrue(is_resource($this->File->handle));
  188. $this->assertTrue($this->File->close());
  189. $this->assertFalse(is_resource($this->File->handle));
  190. }
  191. /**
  192. * testCreate method
  193. *
  194. * @return void
  195. */
  196. public function testCreate() {
  197. $tmpFile = TMP.'tests' . DS . 'cakephp.file.test.tmp';
  198. $File = new File($tmpFile, true, 0777);
  199. $this->assertTrue($File->exists());
  200. }
  201. /**
  202. * testOpeningNonExistantFileCreatesIt method
  203. *
  204. * @return void
  205. */
  206. public function testOpeningNonExistantFileCreatesIt() {
  207. $someFile = new File(TMP . 'some_file.txt', false);
  208. $this->assertTrue($someFile->open());
  209. $this->assertEquals('', $someFile->read());
  210. $someFile->close();
  211. $someFile->delete();
  212. }
  213. /**
  214. * testPrepare method
  215. *
  216. * @return void
  217. */
  218. public function testPrepare() {
  219. $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
  220. if (DS == '\\') {
  221. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  222. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  223. } else {
  224. $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
  225. }
  226. $this->assertSame(File::prepare($string), $expected);
  227. $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
  228. $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
  229. $this->assertSame(File::prepare($string, true), $expected);
  230. }
  231. /**
  232. * testReadable method
  233. *
  234. * @return void
  235. */
  236. public function testReadable() {
  237. $someFile = new File(TMP . 'some_file.txt', false);
  238. $this->assertTrue($someFile->open());
  239. $this->assertTrue($someFile->readable());
  240. $someFile->close();
  241. $someFile->delete();
  242. }
  243. /**
  244. * testWritable method
  245. *
  246. * @return void
  247. */
  248. public function testWritable() {
  249. $someFile = new File(TMP . 'some_file.txt', false);
  250. $this->assertTrue($someFile->open());
  251. $this->assertTrue($someFile->writable());
  252. $someFile->close();
  253. $someFile->delete();
  254. }
  255. /**
  256. * testExecutable method
  257. *
  258. * @return void
  259. */
  260. public function testExecutable() {
  261. $someFile = new File(TMP . 'some_file.txt', false);
  262. $this->assertTrue($someFile->open());
  263. $this->assertFalse($someFile->executable());
  264. $someFile->close();
  265. $someFile->delete();
  266. }
  267. /**
  268. * testLastAccess method
  269. *
  270. * @return void
  271. */
  272. public function testLastAccess() {
  273. $ts = time();
  274. $someFile = new File(TMP . 'some_file.txt', false);
  275. $this->assertFalse($someFile->lastAccess());
  276. $this->assertTrue($someFile->open());
  277. $this->assertTrue($someFile->lastAccess() >= $ts);
  278. $someFile->close();
  279. $someFile->delete();
  280. }
  281. /**
  282. * testLastChange method
  283. *
  284. * @return void
  285. */
  286. public function testLastChange() {
  287. $ts = time();
  288. $someFile = new File(TMP . 'some_file.txt', false);
  289. $this->assertFalse($someFile->lastChange());
  290. $this->assertTrue($someFile->open('r+'));
  291. $this->assertTrue($someFile->lastChange() >= $ts);
  292. $someFile->write('something');
  293. $this->assertTrue($someFile->lastChange() >= $ts);
  294. $someFile->close();
  295. $someFile->delete();
  296. }
  297. /**
  298. * testWrite method
  299. *
  300. * @return void
  301. */
  302. public function testWrite() {
  303. if (!$tmpFile = $this->_getTmpFile()) {
  304. return false;
  305. };
  306. if (file_exists($tmpFile)) {
  307. unlink($tmpFile);
  308. }
  309. $TmpFile = new File($tmpFile);
  310. $this->assertFalse(file_exists($tmpFile));
  311. $this->assertFalse(is_resource($TmpFile->handle));
  312. $testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
  313. foreach ($testData as $data) {
  314. $r = $TmpFile->write($data);
  315. $this->assertTrue($r);
  316. $this->assertTrue(file_exists($tmpFile));
  317. $this->assertEquals($data, file_get_contents($tmpFile));
  318. $this->assertTrue(is_resource($TmpFile->handle));
  319. $TmpFile->close();
  320. }
  321. unlink($tmpFile);
  322. }
  323. /**
  324. * testAppend method
  325. *
  326. * @return void
  327. */
  328. public function testAppend() {
  329. if (!$tmpFile = $this->_getTmpFile()) {
  330. return false;
  331. };
  332. if (file_exists($tmpFile)) {
  333. unlink($tmpFile);
  334. }
  335. $TmpFile = new File($tmpFile);
  336. $this->assertFalse(file_exists($tmpFile));
  337. $fragments = array('CakePHP\'s', ' test suite', ' was here ...', '');
  338. $data = null;
  339. foreach ($fragments as $fragment) {
  340. $r = $TmpFile->append($fragment);
  341. $this->assertTrue($r);
  342. $this->assertTrue(file_exists($tmpFile));
  343. $data = $data . $fragment;
  344. $this->assertEquals($data, file_get_contents($tmpFile));
  345. $TmpFile->close();
  346. }
  347. }
  348. /**
  349. * testDelete method
  350. *
  351. * @return void
  352. */
  353. public function testDelete() {
  354. if (!$tmpFile = $this->_getTmpFile()) {
  355. return false;
  356. }
  357. if (!file_exists($tmpFile)) {
  358. touch($tmpFile);
  359. }
  360. $TmpFile = new File($tmpFile);
  361. $this->assertTrue(file_exists($tmpFile));
  362. $result = $TmpFile->delete();
  363. $this->assertTrue($result);
  364. $this->assertFalse(file_exists($tmpFile));
  365. $TmpFile = new File('/this/does/not/exist');
  366. $result = $TmpFile->delete();
  367. $this->assertFalse($result);
  368. }
  369. /**
  370. * Windows has issues unlinking files if there are
  371. * active filehandles open.
  372. *
  373. * @return void
  374. */
  375. public function testDeleteAfterRead() {
  376. if (!$tmpFile = $this->_getTmpFile()) {
  377. return false;
  378. }
  379. if (!file_exists($tmpFile)) {
  380. touch($tmpFile);
  381. }
  382. $File = new File($tmpFile);
  383. $File->read();
  384. $this->assertTrue($File->delete());
  385. }
  386. /**
  387. * testCopy method
  388. *
  389. * @return void
  390. */
  391. public function testCopy() {
  392. $dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  393. $file = __FILE__;
  394. $this->File = new File($file);
  395. $result = $this->File->copy($dest);
  396. $this->assertTrue($result);
  397. $result = $this->File->copy($dest, true);
  398. $this->assertTrue($result);
  399. $result = $this->File->copy($dest, false);
  400. $this->assertFalse($result);
  401. $this->File->close();
  402. unlink($dest);
  403. $TmpFile = new File('/this/does/not/exist');
  404. $result = $TmpFile->copy($dest);
  405. $this->assertFalse($result);
  406. $TmpFile->close();
  407. }
  408. /**
  409. * Test mime()
  410. *
  411. * @return void
  412. */
  413. public function testMime() {
  414. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
  415. $file = new File($path);
  416. $this->assertEquals('image/gif', $file->mime());
  417. }
  418. /**
  419. * getTmpFile method
  420. *
  421. * @param bool $paintSkip
  422. * @return void
  423. */
  424. protected function _getTmpFile($paintSkip = true) {
  425. $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
  426. if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
  427. return $tmpFile;
  428. };
  429. if ($paintSkip) {
  430. $trace = debug_backtrace();
  431. $caller = $trace[0]['function'];
  432. $shortPath = dirname($tmpFile);
  433. $message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
  434. $this->markTestSkipped($message);
  435. }
  436. return false;
  437. }
  438. }