FilesystemTest.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Filesystem\Tests;
  11. /**
  12. * Test class for Filesystem.
  13. */
  14. class FilesystemTest extends FilesystemTestCase
  15. {
  16. public function testCopyCreatesNewFile()
  17. {
  18. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  19. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  20. file_put_contents($sourceFilePath, 'SOURCE FILE');
  21. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  22. $this->assertFileExists($targetFilePath);
  23. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  24. }
  25. /**
  26. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  27. */
  28. public function testCopyFails()
  29. {
  30. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  31. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  32. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  33. }
  34. /**
  35. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  36. */
  37. public function testCopyUnreadableFileFails()
  38. {
  39. // skip test on Windows; PHP can't easily set file as unreadable on Windows
  40. if ('\\' === DIRECTORY_SEPARATOR) {
  41. $this->markTestSkipped('This test cannot run on Windows.');
  42. }
  43. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  44. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  45. file_put_contents($sourceFilePath, 'SOURCE FILE');
  46. // make sure target cannot be read
  47. $this->filesystem->chmod($sourceFilePath, 0222);
  48. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  49. }
  50. public function testCopyOverridesExistingFileIfModified()
  51. {
  52. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  53. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  54. file_put_contents($sourceFilePath, 'SOURCE FILE');
  55. file_put_contents($targetFilePath, 'TARGET FILE');
  56. touch($targetFilePath, time() - 1000);
  57. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  58. $this->assertFileExists($targetFilePath);
  59. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  60. }
  61. public function testCopyDoesNotOverrideExistingFileByDefault()
  62. {
  63. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  64. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  65. file_put_contents($sourceFilePath, 'SOURCE FILE');
  66. file_put_contents($targetFilePath, 'TARGET FILE');
  67. // make sure both files have the same modification time
  68. $modificationTime = time() - 1000;
  69. touch($sourceFilePath, $modificationTime);
  70. touch($targetFilePath, $modificationTime);
  71. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  72. $this->assertFileExists($targetFilePath);
  73. $this->assertEquals('TARGET FILE', file_get_contents($targetFilePath));
  74. }
  75. public function testCopyOverridesExistingFileIfForced()
  76. {
  77. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  78. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  79. file_put_contents($sourceFilePath, 'SOURCE FILE');
  80. file_put_contents($targetFilePath, 'TARGET FILE');
  81. // make sure both files have the same modification time
  82. $modificationTime = time() - 1000;
  83. touch($sourceFilePath, $modificationTime);
  84. touch($targetFilePath, $modificationTime);
  85. $this->filesystem->copy($sourceFilePath, $targetFilePath, true);
  86. $this->assertFileExists($targetFilePath);
  87. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  88. }
  89. /**
  90. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  91. */
  92. public function testCopyWithOverrideWithReadOnlyTargetFails()
  93. {
  94. // skip test on Windows; PHP can't easily set file as unwritable on Windows
  95. if ('\\' === DIRECTORY_SEPARATOR) {
  96. $this->markTestSkipped('This test cannot run on Windows.');
  97. }
  98. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  99. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  100. file_put_contents($sourceFilePath, 'SOURCE FILE');
  101. file_put_contents($targetFilePath, 'TARGET FILE');
  102. // make sure both files have the same modification time
  103. $modificationTime = time() - 1000;
  104. touch($sourceFilePath, $modificationTime);
  105. touch($targetFilePath, $modificationTime);
  106. // make sure target is read-only
  107. $this->filesystem->chmod($targetFilePath, 0444);
  108. $this->filesystem->copy($sourceFilePath, $targetFilePath, true);
  109. }
  110. public function testCopyCreatesTargetDirectoryIfItDoesNotExist()
  111. {
  112. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  113. $targetFileDirectory = $this->workspace.DIRECTORY_SEPARATOR.'directory';
  114. $targetFilePath = $targetFileDirectory.DIRECTORY_SEPARATOR.'copy_target_file';
  115. file_put_contents($sourceFilePath, 'SOURCE FILE');
  116. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  117. $this->assertTrue(is_dir($targetFileDirectory));
  118. $this->assertFileExists($targetFilePath);
  119. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  120. }
  121. public function testCopyForOriginUrlsAndExistingLocalFileDefaultsToCopy()
  122. {
  123. $sourceFilePath = 'http://symfony.com/images/common/logo/logo_symfony_header.png';
  124. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  125. file_put_contents($targetFilePath, 'TARGET FILE');
  126. $this->filesystem->copy($sourceFilePath, $targetFilePath, false);
  127. $this->assertFileExists($targetFilePath);
  128. $this->assertEquals(file_get_contents($sourceFilePath), file_get_contents($targetFilePath));
  129. }
  130. public function testMkdirCreatesDirectoriesRecursively()
  131. {
  132. $directory = $this->workspace
  133. .DIRECTORY_SEPARATOR.'directory'
  134. .DIRECTORY_SEPARATOR.'sub_directory';
  135. $this->filesystem->mkdir($directory);
  136. $this->assertTrue(is_dir($directory));
  137. }
  138. public function testMkdirCreatesDirectoriesFromArray()
  139. {
  140. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  141. $directories = array(
  142. $basePath.'1', $basePath.'2', $basePath.'3',
  143. );
  144. $this->filesystem->mkdir($directories);
  145. $this->assertTrue(is_dir($basePath.'1'));
  146. $this->assertTrue(is_dir($basePath.'2'));
  147. $this->assertTrue(is_dir($basePath.'3'));
  148. }
  149. public function testMkdirCreatesDirectoriesFromTraversableObject()
  150. {
  151. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  152. $directories = new \ArrayObject(array(
  153. $basePath.'1', $basePath.'2', $basePath.'3',
  154. ));
  155. $this->filesystem->mkdir($directories);
  156. $this->assertTrue(is_dir($basePath.'1'));
  157. $this->assertTrue(is_dir($basePath.'2'));
  158. $this->assertTrue(is_dir($basePath.'3'));
  159. }
  160. /**
  161. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  162. */
  163. public function testMkdirCreatesDirectoriesFails()
  164. {
  165. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  166. $dir = $basePath.'2';
  167. file_put_contents($dir, '');
  168. $this->filesystem->mkdir($dir);
  169. }
  170. public function testTouchCreatesEmptyFile()
  171. {
  172. $file = $this->workspace.DIRECTORY_SEPARATOR.'1';
  173. $this->filesystem->touch($file);
  174. $this->assertFileExists($file);
  175. }
  176. /**
  177. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  178. */
  179. public function testTouchFails()
  180. {
  181. $file = $this->workspace.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR.'2';
  182. $this->filesystem->touch($file);
  183. }
  184. public function testTouchCreatesEmptyFilesFromArray()
  185. {
  186. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  187. $files = array(
  188. $basePath.'1', $basePath.'2', $basePath.'3',
  189. );
  190. $this->filesystem->touch($files);
  191. $this->assertFileExists($basePath.'1');
  192. $this->assertFileExists($basePath.'2');
  193. $this->assertFileExists($basePath.'3');
  194. }
  195. public function testTouchCreatesEmptyFilesFromTraversableObject()
  196. {
  197. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  198. $files = new \ArrayObject(array(
  199. $basePath.'1', $basePath.'2', $basePath.'3',
  200. ));
  201. $this->filesystem->touch($files);
  202. $this->assertFileExists($basePath.'1');
  203. $this->assertFileExists($basePath.'2');
  204. $this->assertFileExists($basePath.'3');
  205. }
  206. public function testRemoveCleansFilesAndDirectoriesIteratively()
  207. {
  208. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  209. mkdir($basePath);
  210. mkdir($basePath.'dir');
  211. touch($basePath.'file');
  212. $this->filesystem->remove($basePath);
  213. $this->assertFileNotExists($basePath);
  214. }
  215. public function testRemoveCleansArrayOfFilesAndDirectories()
  216. {
  217. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  218. mkdir($basePath.'dir');
  219. touch($basePath.'file');
  220. $files = array(
  221. $basePath.'dir', $basePath.'file',
  222. );
  223. $this->filesystem->remove($files);
  224. $this->assertFileNotExists($basePath.'dir');
  225. $this->assertFileNotExists($basePath.'file');
  226. }
  227. public function testRemoveCleansTraversableObjectOfFilesAndDirectories()
  228. {
  229. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  230. mkdir($basePath.'dir');
  231. touch($basePath.'file');
  232. $files = new \ArrayObject(array(
  233. $basePath.'dir', $basePath.'file',
  234. ));
  235. $this->filesystem->remove($files);
  236. $this->assertFileNotExists($basePath.'dir');
  237. $this->assertFileNotExists($basePath.'file');
  238. }
  239. public function testRemoveIgnoresNonExistingFiles()
  240. {
  241. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  242. mkdir($basePath.'dir');
  243. $files = array(
  244. $basePath.'dir', $basePath.'file',
  245. );
  246. $this->filesystem->remove($files);
  247. $this->assertFileNotExists($basePath.'dir');
  248. }
  249. public function testRemoveCleansInvalidLinks()
  250. {
  251. $this->markAsSkippedIfSymlinkIsMissing();
  252. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  253. mkdir($basePath);
  254. mkdir($basePath.'dir');
  255. // create symlink to nonexistent file
  256. @symlink($basePath.'file', $basePath.'file-link');
  257. // create symlink to dir using trailing forward slash
  258. $this->filesystem->symlink($basePath.'dir/', $basePath.'dir-link');
  259. $this->assertTrue(is_dir($basePath.'dir-link'));
  260. // create symlink to nonexistent dir
  261. rmdir($basePath.'dir');
  262. $this->assertFalse('\\' === DIRECTORY_SEPARATOR ? @readlink($basePath.'dir-link') : is_dir($basePath.'dir-link'));
  263. $this->filesystem->remove($basePath);
  264. $this->assertFileNotExists($basePath);
  265. }
  266. public function testFilesExists()
  267. {
  268. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  269. mkdir($basePath);
  270. touch($basePath.'file1');
  271. mkdir($basePath.'folder');
  272. $this->assertTrue($this->filesystem->exists($basePath.'file1'));
  273. $this->assertTrue($this->filesystem->exists($basePath.'folder'));
  274. }
  275. /**
  276. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  277. */
  278. public function testFilesExistsFails()
  279. {
  280. if ('\\' !== DIRECTORY_SEPARATOR) {
  281. $this->markTestSkipped('Test covers edge case on Windows only.');
  282. }
  283. $basePath = $this->workspace.'\\directory\\';
  284. $oldPath = getcwd();
  285. mkdir($basePath);
  286. chdir($basePath);
  287. $file = str_repeat('T', 259 - strlen($basePath));
  288. $path = $basePath.$file;
  289. exec('TYPE NUL >>'.$file); // equivalent of touch, we can not use the php touch() here because it suffers from the same limitation
  290. $this->longPathNamesWindows[] = $path; // save this so we can clean up later
  291. chdir($oldPath);
  292. $this->filesystem->exists($path);
  293. }
  294. public function testFilesExistsTraversableObjectOfFilesAndDirectories()
  295. {
  296. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  297. mkdir($basePath.'dir');
  298. touch($basePath.'file');
  299. $files = new \ArrayObject(array(
  300. $basePath.'dir', $basePath.'file',
  301. ));
  302. $this->assertTrue($this->filesystem->exists($files));
  303. }
  304. public function testFilesNotExistsTraversableObjectOfFilesAndDirectories()
  305. {
  306. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  307. mkdir($basePath.'dir');
  308. touch($basePath.'file');
  309. touch($basePath.'file2');
  310. $files = new \ArrayObject(array(
  311. $basePath.'dir', $basePath.'file', $basePath.'file2',
  312. ));
  313. unlink($basePath.'file');
  314. $this->assertFalse($this->filesystem->exists($files));
  315. }
  316. public function testInvalidFileNotExists()
  317. {
  318. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  319. $this->assertFalse($this->filesystem->exists($basePath.time()));
  320. }
  321. public function testChmodChangesFileMode()
  322. {
  323. $this->markAsSkippedIfChmodIsMissing();
  324. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  325. mkdir($dir);
  326. $file = $dir.DIRECTORY_SEPARATOR.'file';
  327. touch($file);
  328. $this->filesystem->chmod($file, 0400);
  329. $this->filesystem->chmod($dir, 0753);
  330. $this->assertFilePermissions(753, $dir);
  331. $this->assertFilePermissions(400, $file);
  332. }
  333. public function testChmodWrongMod()
  334. {
  335. $this->markAsSkippedIfChmodIsMissing();
  336. $dir = $this->workspace.DIRECTORY_SEPARATOR.'file';
  337. touch($dir);
  338. $this->filesystem->chmod($dir, 'Wrongmode');
  339. }
  340. public function testChmodRecursive()
  341. {
  342. $this->markAsSkippedIfChmodIsMissing();
  343. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  344. mkdir($dir);
  345. $file = $dir.DIRECTORY_SEPARATOR.'file';
  346. touch($file);
  347. $this->filesystem->chmod($file, 0400, 0000, true);
  348. $this->filesystem->chmod($dir, 0753, 0000, true);
  349. $this->assertFilePermissions(753, $dir);
  350. $this->assertFilePermissions(753, $file);
  351. }
  352. public function testChmodAppliesUmask()
  353. {
  354. $this->markAsSkippedIfChmodIsMissing();
  355. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  356. touch($file);
  357. $this->filesystem->chmod($file, 0770, 0022);
  358. $this->assertFilePermissions(750, $file);
  359. }
  360. public function testChmodChangesModeOfArrayOfFiles()
  361. {
  362. $this->markAsSkippedIfChmodIsMissing();
  363. $directory = $this->workspace.DIRECTORY_SEPARATOR.'directory';
  364. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  365. $files = array($directory, $file);
  366. mkdir($directory);
  367. touch($file);
  368. $this->filesystem->chmod($files, 0753);
  369. $this->assertFilePermissions(753, $file);
  370. $this->assertFilePermissions(753, $directory);
  371. }
  372. public function testChmodChangesModeOfTraversableFileObject()
  373. {
  374. $this->markAsSkippedIfChmodIsMissing();
  375. $directory = $this->workspace.DIRECTORY_SEPARATOR.'directory';
  376. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  377. $files = new \ArrayObject(array($directory, $file));
  378. mkdir($directory);
  379. touch($file);
  380. $this->filesystem->chmod($files, 0753);
  381. $this->assertFilePermissions(753, $file);
  382. $this->assertFilePermissions(753, $directory);
  383. }
  384. public function testChmodChangesZeroModeOnSubdirectoriesOnRecursive()
  385. {
  386. $this->markAsSkippedIfChmodIsMissing();
  387. $directory = $this->workspace.DIRECTORY_SEPARATOR.'directory';
  388. $subdirectory = $directory.DIRECTORY_SEPARATOR.'subdirectory';
  389. mkdir($directory);
  390. mkdir($subdirectory);
  391. chmod($subdirectory, 0000);
  392. $this->filesystem->chmod($directory, 0753, 0000, true);
  393. $this->assertFilePermissions(753, $subdirectory);
  394. }
  395. public function testChown()
  396. {
  397. $this->markAsSkippedIfPosixIsMissing();
  398. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  399. mkdir($dir);
  400. $this->filesystem->chown($dir, $this->getFileOwner($dir));
  401. }
  402. public function testChownRecursive()
  403. {
  404. $this->markAsSkippedIfPosixIsMissing();
  405. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  406. mkdir($dir);
  407. $file = $dir.DIRECTORY_SEPARATOR.'file';
  408. touch($file);
  409. $this->filesystem->chown($dir, $this->getFileOwner($dir), true);
  410. }
  411. public function testChownSymlink()
  412. {
  413. $this->markAsSkippedIfSymlinkIsMissing();
  414. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  415. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  416. touch($file);
  417. $this->filesystem->symlink($file, $link);
  418. $this->filesystem->chown($link, $this->getFileOwner($link));
  419. }
  420. /**
  421. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  422. */
  423. public function testChownSymlinkFails()
  424. {
  425. $this->markAsSkippedIfSymlinkIsMissing();
  426. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  427. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  428. touch($file);
  429. $this->filesystem->symlink($file, $link);
  430. $this->filesystem->chown($link, 'user'.time().mt_rand(1000, 9999));
  431. }
  432. /**
  433. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  434. */
  435. public function testChownFail()
  436. {
  437. $this->markAsSkippedIfPosixIsMissing();
  438. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  439. mkdir($dir);
  440. $this->filesystem->chown($dir, 'user'.time().mt_rand(1000, 9999));
  441. }
  442. public function testChgrp()
  443. {
  444. $this->markAsSkippedIfPosixIsMissing();
  445. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  446. mkdir($dir);
  447. $this->filesystem->chgrp($dir, $this->getFileGroup($dir));
  448. }
  449. public function testChgrpRecursive()
  450. {
  451. $this->markAsSkippedIfPosixIsMissing();
  452. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  453. mkdir($dir);
  454. $file = $dir.DIRECTORY_SEPARATOR.'file';
  455. touch($file);
  456. $this->filesystem->chgrp($dir, $this->getFileGroup($dir), true);
  457. }
  458. public function testChgrpSymlink()
  459. {
  460. $this->markAsSkippedIfSymlinkIsMissing();
  461. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  462. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  463. touch($file);
  464. $this->filesystem->symlink($file, $link);
  465. $this->filesystem->chgrp($link, $this->getFileGroup($link));
  466. }
  467. /**
  468. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  469. */
  470. public function testChgrpSymlinkFails()
  471. {
  472. $this->markAsSkippedIfSymlinkIsMissing();
  473. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  474. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  475. touch($file);
  476. $this->filesystem->symlink($file, $link);
  477. $this->filesystem->chgrp($link, 'user'.time().mt_rand(1000, 9999));
  478. }
  479. /**
  480. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  481. */
  482. public function testChgrpFail()
  483. {
  484. $this->markAsSkippedIfPosixIsMissing();
  485. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  486. mkdir($dir);
  487. $this->filesystem->chgrp($dir, 'user'.time().mt_rand(1000, 9999));
  488. }
  489. public function testRename()
  490. {
  491. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  492. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  493. touch($file);
  494. $this->filesystem->rename($file, $newPath);
  495. $this->assertFileNotExists($file);
  496. $this->assertFileExists($newPath);
  497. }
  498. /**
  499. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  500. */
  501. public function testRenameThrowsExceptionIfTargetAlreadyExists()
  502. {
  503. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  504. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  505. touch($file);
  506. touch($newPath);
  507. $this->filesystem->rename($file, $newPath);
  508. }
  509. public function testRenameOverwritesTheTargetIfItAlreadyExists()
  510. {
  511. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  512. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  513. touch($file);
  514. touch($newPath);
  515. $this->filesystem->rename($file, $newPath, true);
  516. $this->assertFileNotExists($file);
  517. $this->assertFileExists($newPath);
  518. }
  519. /**
  520. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  521. */
  522. public function testRenameThrowsExceptionOnError()
  523. {
  524. $file = $this->workspace.DIRECTORY_SEPARATOR.uniqid('fs_test_', true);
  525. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  526. $this->filesystem->rename($file, $newPath);
  527. }
  528. public function testSymlink()
  529. {
  530. if ('\\' === DIRECTORY_SEPARATOR) {
  531. $this->markTestSkipped('Windows does not support creating "broken" symlinks');
  532. }
  533. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  534. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  535. // $file does not exists right now: creating "broken" links is a wanted feature
  536. $this->filesystem->symlink($file, $link);
  537. $this->assertTrue(is_link($link));
  538. // Create the linked file AFTER creating the link
  539. touch($file);
  540. $this->assertEquals($file, readlink($link));
  541. }
  542. /**
  543. * @depends testSymlink
  544. */
  545. public function testRemoveSymlink()
  546. {
  547. $this->markAsSkippedIfSymlinkIsMissing();
  548. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  549. $this->filesystem->remove($link);
  550. $this->assertTrue(!is_link($link));
  551. $this->assertTrue(!is_file($link));
  552. $this->assertTrue(!is_dir($link));
  553. }
  554. public function testSymlinkIsOverwrittenIfPointsToDifferentTarget()
  555. {
  556. $this->markAsSkippedIfSymlinkIsMissing();
  557. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  558. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  559. touch($file);
  560. symlink($this->workspace, $link);
  561. $this->filesystem->symlink($file, $link);
  562. $this->assertTrue(is_link($link));
  563. $this->assertEquals($file, readlink($link));
  564. }
  565. public function testSymlinkIsNotOverwrittenIfAlreadyCreated()
  566. {
  567. $this->markAsSkippedIfSymlinkIsMissing();
  568. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  569. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  570. touch($file);
  571. symlink($file, $link);
  572. $this->filesystem->symlink($file, $link);
  573. $this->assertTrue(is_link($link));
  574. $this->assertEquals($file, readlink($link));
  575. }
  576. public function testSymlinkCreatesTargetDirectoryIfItDoesNotExist()
  577. {
  578. $this->markAsSkippedIfSymlinkIsMissing();
  579. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  580. $link1 = $this->workspace.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'link';
  581. $link2 = $this->workspace.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'subdir'.DIRECTORY_SEPARATOR.'link';
  582. touch($file);
  583. $this->filesystem->symlink($file, $link1);
  584. $this->filesystem->symlink($file, $link2);
  585. $this->assertTrue(is_link($link1));
  586. $this->assertEquals($file, readlink($link1));
  587. $this->assertTrue(is_link($link2));
  588. $this->assertEquals($file, readlink($link2));
  589. }
  590. /**
  591. * @dataProvider providePathsForMakePathRelative
  592. */
  593. public function testMakePathRelative($endPath, $startPath, $expectedPath)
  594. {
  595. $path = $this->filesystem->makePathRelative($endPath, $startPath);
  596. $this->assertEquals($expectedPath, $path);
  597. }
  598. /**
  599. * @return array
  600. */
  601. public function providePathsForMakePathRelative()
  602. {
  603. $paths = array(
  604. array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component', '../'),
  605. array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component/', '../'),
  606. array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component', '../'),
  607. array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'),
  608. array('var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../'),
  609. array('/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'),
  610. array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'),
  611. array('/aa/bb', '/aa/bb', './'),
  612. array('/aa/bb', '/aa/bb/', './'),
  613. array('/aa/bb/', '/aa/bb', './'),
  614. array('/aa/bb/', '/aa/bb/', './'),
  615. array('/aa/bb/cc', '/aa/bb/cc/dd', '../'),
  616. array('/aa/bb/cc', '/aa/bb/cc/dd/', '../'),
  617. array('/aa/bb/cc/', '/aa/bb/cc/dd', '../'),
  618. array('/aa/bb/cc/', '/aa/bb/cc/dd/', '../'),
  619. array('/aa/bb/cc', '/aa', 'bb/cc/'),
  620. array('/aa/bb/cc', '/aa/', 'bb/cc/'),
  621. array('/aa/bb/cc/', '/aa', 'bb/cc/'),
  622. array('/aa/bb/cc/', '/aa/', 'bb/cc/'),
  623. array('/a/aab/bb', '/a/aa', '../aab/bb/'),
  624. array('/a/aab/bb', '/a/aa/', '../aab/bb/'),
  625. array('/a/aab/bb/', '/a/aa', '../aab/bb/'),
  626. array('/a/aab/bb/', '/a/aa/', '../aab/bb/'),
  627. array('/a/aab/bb/', '/', 'a/aab/bb/'),
  628. array('/a/aab/bb/', '/b/aab', '../../a/aab/bb/'),
  629. );
  630. if ('\\' === DIRECTORY_SEPARATOR) {
  631. $paths[] = array('c:\var\lib/symfony/src/Symfony/', 'c:/var/lib/symfony/', 'src/Symfony/');
  632. }
  633. return $paths;
  634. }
  635. public function testMirrorCopiesFilesAndDirectoriesRecursively()
  636. {
  637. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  638. $directory = $sourcePath.'directory'.DIRECTORY_SEPARATOR;
  639. $file1 = $directory.'file1';
  640. $file2 = $sourcePath.'file2';
  641. mkdir($sourcePath);
  642. mkdir($directory);
  643. file_put_contents($file1, 'FILE1');
  644. file_put_contents($file2, 'FILE2');
  645. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  646. $this->filesystem->mirror($sourcePath, $targetPath);
  647. $this->assertTrue(is_dir($targetPath));
  648. $this->assertTrue(is_dir($targetPath.'directory'));
  649. $this->assertFileEquals($file1, $targetPath.'directory'.DIRECTORY_SEPARATOR.'file1');
  650. $this->assertFileEquals($file2, $targetPath.'file2');
  651. $this->filesystem->remove($file1);
  652. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => false));
  653. $this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  654. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => true));
  655. $this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  656. file_put_contents($file1, 'FILE1');
  657. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => true));
  658. $this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  659. $this->filesystem->remove($directory);
  660. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => true));
  661. $this->assertFalse($this->filesystem->exists($targetPath.'directory'));
  662. $this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  663. }
  664. public function testMirrorCreatesEmptyDirectory()
  665. {
  666. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  667. mkdir($sourcePath);
  668. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  669. $this->filesystem->mirror($sourcePath, $targetPath);
  670. $this->assertTrue(is_dir($targetPath));
  671. $this->filesystem->remove($sourcePath);
  672. }
  673. public function testMirrorCopiesLinks()
  674. {
  675. $this->markAsSkippedIfSymlinkIsMissing();
  676. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  677. mkdir($sourcePath);
  678. file_put_contents($sourcePath.'file1', 'FILE1');
  679. symlink($sourcePath.'file1', $sourcePath.'link1');
  680. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  681. $this->filesystem->mirror($sourcePath, $targetPath);
  682. $this->assertTrue(is_dir($targetPath));
  683. $this->assertFileEquals($sourcePath.'file1', $targetPath.'link1');
  684. $this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
  685. }
  686. public function testMirrorCopiesLinkedDirectoryContents()
  687. {
  688. $this->markAsSkippedIfSymlinkIsMissing(true);
  689. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  690. mkdir($sourcePath.'nested/', 0777, true);
  691. file_put_contents($sourcePath.'/nested/file1.txt', 'FILE1');
  692. // Note: We symlink directory, not file
  693. symlink($sourcePath.'nested', $sourcePath.'link1');
  694. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  695. $this->filesystem->mirror($sourcePath, $targetPath);
  696. $this->assertTrue(is_dir($targetPath));
  697. $this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.'link1/file1.txt');
  698. $this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
  699. }
  700. public function testMirrorCopiesRelativeLinkedContents()
  701. {
  702. $this->markAsSkippedIfSymlinkIsMissing(true);
  703. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  704. $oldPath = getcwd();
  705. mkdir($sourcePath.'nested/', 0777, true);
  706. file_put_contents($sourcePath.'/nested/file1.txt', 'FILE1');
  707. // Note: Create relative symlink
  708. chdir($sourcePath);
  709. symlink('nested', 'link1');
  710. chdir($oldPath);
  711. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  712. $this->filesystem->mirror($sourcePath, $targetPath);
  713. $this->assertTrue(is_dir($targetPath));
  714. $this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.'link1/file1.txt');
  715. $this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
  716. $this->assertEquals('\\' === DIRECTORY_SEPARATOR ? realpath($sourcePath.'\nested') : 'nested', readlink($targetPath.DIRECTORY_SEPARATOR.'link1'));
  717. }
  718. /**
  719. * @dataProvider providePathsForIsAbsolutePath
  720. */
  721. public function testIsAbsolutePath($path, $expectedResult)
  722. {
  723. $result = $this->filesystem->isAbsolutePath($path);
  724. $this->assertEquals($expectedResult, $result);
  725. }
  726. /**
  727. * @return array
  728. */
  729. public function providePathsForIsAbsolutePath()
  730. {
  731. return array(
  732. array('/var/lib', true),
  733. array('c:\\\\var\\lib', true),
  734. array('\\var\\lib', true),
  735. array('var/lib', false),
  736. array('../var/lib', false),
  737. array('', false),
  738. array(null, false),
  739. );
  740. }
  741. public function testTempnam()
  742. {
  743. $dirname = $this->workspace;
  744. $filename = $this->filesystem->tempnam($dirname, 'foo');
  745. $this->assertFileExists($filename);
  746. }
  747. public function testTempnamWithFileScheme()
  748. {
  749. $scheme = 'file://';
  750. $dirname = $scheme.$this->workspace;
  751. $filename = $this->filesystem->tempnam($dirname, 'foo');
  752. $this->assertStringStartsWith($scheme, $filename);
  753. $this->assertFileExists($filename);
  754. }
  755. public function testTempnamWithMockScheme()
  756. {
  757. stream_wrapper_register('mock', 'Symfony\Component\Filesystem\Tests\Fixtures\MockStream\MockStream');
  758. $scheme = 'mock://';
  759. $dirname = $scheme.$this->workspace;
  760. $filename = $this->filesystem->tempnam($dirname, 'foo');
  761. $this->assertStringStartsWith($scheme, $filename);
  762. $this->assertFileExists($filename);
  763. }
  764. /**
  765. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  766. */
  767. public function testTempnamWithZlibSchemeFails()
  768. {
  769. $scheme = 'compress.zlib://';
  770. $dirname = $scheme.$this->workspace;
  771. // The compress.zlib:// stream does not support mode x: creates the file, errors "failed to open stream: operation failed" and returns false
  772. $this->filesystem->tempnam($dirname, 'bar');
  773. }
  774. public function testTempnamWithPHPTempSchemeFails()
  775. {
  776. $scheme = 'php://temp';
  777. $dirname = $scheme;
  778. $filename = $this->filesystem->tempnam($dirname, 'bar');
  779. $this->assertStringStartsWith($scheme, $filename);
  780. // The php://temp stream deletes the file after close
  781. $this->assertFileNotExists($filename);
  782. }
  783. /**
  784. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  785. */
  786. public function testTempnamWithPharSchemeFails()
  787. {
  788. // Skip test if Phar disabled phar.readonly must be 0 in php.ini
  789. if (!\Phar::canWrite()) {
  790. $this->markTestSkipped('This test cannot run when phar.readonly is 1.');
  791. }
  792. $scheme = 'phar://';
  793. $dirname = $scheme.$this->workspace;
  794. $pharname = 'foo.phar';
  795. new \Phar($this->workspace.'/'.$pharname, 0, $pharname);
  796. // The phar:// stream does not support mode x: fails to create file, errors "failed to open stream: phar error: "$filename" is not a file in phar "$pharname"" and returns false
  797. $this->filesystem->tempnam($dirname, $pharname.'/bar');
  798. }
  799. /**
  800. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  801. */
  802. public function testTempnamWithHTTPSchemeFails()
  803. {
  804. $scheme = 'http://';
  805. $dirname = $scheme.$this->workspace;
  806. // The http:// scheme is read-only
  807. $this->filesystem->tempnam($dirname, 'bar');
  808. }
  809. public function testTempnamOnUnwritableFallsBackToSysTmp()
  810. {
  811. $scheme = 'file://';
  812. $dirname = $scheme.$this->workspace.DIRECTORY_SEPARATOR.'does_not_exist';
  813. $filename = $this->filesystem->tempnam($dirname, 'bar');
  814. $realTempDir = realpath(sys_get_temp_dir());
  815. $this->assertStringStartsWith(rtrim($scheme.$realTempDir, DIRECTORY_SEPARATOR), $filename);
  816. $this->assertFileExists($filename);
  817. // Tear down
  818. @unlink($filename);
  819. }
  820. public function testDumpFile()
  821. {
  822. $filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
  823. $this->filesystem->dumpFile($filename, 'bar');
  824. $this->assertFileExists($filename);
  825. $this->assertSame('bar', file_get_contents($filename));
  826. // skip mode check on Windows
  827. if ('\\' !== DIRECTORY_SEPARATOR) {
  828. $this->assertFilePermissions(666, $filename);
  829. }
  830. }
  831. public function testDumpFileOverwritesAnExistingFile()
  832. {
  833. $filename = $this->workspace.DIRECTORY_SEPARATOR.'foo.txt';
  834. file_put_contents($filename, 'FOO BAR');
  835. $this->filesystem->dumpFile($filename, 'bar');
  836. $this->assertFileExists($filename);
  837. $this->assertSame('bar', file_get_contents($filename));
  838. }
  839. public function testDumpFileWithFileScheme()
  840. {
  841. if (defined('HHVM_VERSION')) {
  842. $this->markTestSkipped('HHVM does not handle the file:// scheme correctly');
  843. }
  844. $scheme = 'file://';
  845. $filename = $scheme.$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
  846. $this->filesystem->dumpFile($filename, 'bar', null);
  847. $this->assertFileExists($filename);
  848. $this->assertSame('bar', file_get_contents($filename));
  849. }
  850. public function testDumpFileWithZlibScheme()
  851. {
  852. $scheme = 'compress.zlib://';
  853. $filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
  854. $this->filesystem->dumpFile($filename, 'bar', null);
  855. // Zlib stat uses file:// wrapper so remove scheme
  856. $this->assertFileExists(str_replace($scheme, '', $filename));
  857. $this->assertSame('bar', file_get_contents($filename));
  858. }
  859. public function testCopyShouldKeepExecutionPermission()
  860. {
  861. $this->markAsSkippedIfChmodIsMissing();
  862. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  863. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  864. file_put_contents($sourceFilePath, 'SOURCE FILE');
  865. chmod($sourceFilePath, 0745);
  866. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  867. $this->assertFilePermissions(767, $targetFilePath);
  868. }
  869. }