FilesystemTest.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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->assertStringEqualsFile($targetFilePath, 'SOURCE FILE');
  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. if (!getenv('USER') || 'root' === getenv('USER')) {
  44. $this->markTestSkipped('This test will fail if run under superuser');
  45. }
  46. $sourceFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_source_file';
  47. $targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
  48. file_put_contents($sourceFilePath, 'SOURCE FILE');
  49. // make sure target cannot be read
  50. $this->filesystem->chmod($sourceFilePath, 0222);
  51. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  52. }
  53. public function testCopyOverridesExistingFileIfModified()
  54. {
  55. $sourceFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_source_file';
  56. $targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
  57. file_put_contents($sourceFilePath, 'SOURCE FILE');
  58. file_put_contents($targetFilePath, 'TARGET FILE');
  59. touch($targetFilePath, time() - 1000);
  60. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  61. $this->assertFileExists($targetFilePath);
  62. $this->assertStringEqualsFile($targetFilePath, 'SOURCE FILE');
  63. }
  64. public function testCopyDoesNotOverrideExistingFileByDefault()
  65. {
  66. $sourceFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_source_file';
  67. $targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
  68. file_put_contents($sourceFilePath, 'SOURCE FILE');
  69. file_put_contents($targetFilePath, 'TARGET FILE');
  70. // make sure both files have the same modification time
  71. $modificationTime = time() - 1000;
  72. touch($sourceFilePath, $modificationTime);
  73. touch($targetFilePath, $modificationTime);
  74. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  75. $this->assertFileExists($targetFilePath);
  76. $this->assertStringEqualsFile($targetFilePath, 'TARGET FILE');
  77. }
  78. public function testCopyOverridesExistingFileIfForced()
  79. {
  80. $sourceFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_source_file';
  81. $targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
  82. file_put_contents($sourceFilePath, 'SOURCE FILE');
  83. file_put_contents($targetFilePath, 'TARGET FILE');
  84. // make sure both files have the same modification time
  85. $modificationTime = time() - 1000;
  86. touch($sourceFilePath, $modificationTime);
  87. touch($targetFilePath, $modificationTime);
  88. $this->filesystem->copy($sourceFilePath, $targetFilePath, true);
  89. $this->assertFileExists($targetFilePath);
  90. $this->assertStringEqualsFile($targetFilePath, 'SOURCE FILE');
  91. }
  92. /**
  93. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  94. */
  95. public function testCopyWithOverrideWithReadOnlyTargetFails()
  96. {
  97. // skip test on Windows; PHP can't easily set file as unwritable on Windows
  98. if ('\\' === \DIRECTORY_SEPARATOR) {
  99. $this->markTestSkipped('This test cannot run on Windows.');
  100. }
  101. if (!getenv('USER') || 'root' === getenv('USER')) {
  102. $this->markTestSkipped('This test will fail if run under superuser');
  103. }
  104. $sourceFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_source_file';
  105. $targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
  106. file_put_contents($sourceFilePath, 'SOURCE FILE');
  107. file_put_contents($targetFilePath, 'TARGET FILE');
  108. // make sure both files have the same modification time
  109. $modificationTime = time() - 1000;
  110. touch($sourceFilePath, $modificationTime);
  111. touch($targetFilePath, $modificationTime);
  112. // make sure target is read-only
  113. $this->filesystem->chmod($targetFilePath, 0444);
  114. $this->filesystem->copy($sourceFilePath, $targetFilePath, true);
  115. }
  116. public function testCopyCreatesTargetDirectoryIfItDoesNotExist()
  117. {
  118. $sourceFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_source_file';
  119. $targetFileDirectory = $this->workspace.\DIRECTORY_SEPARATOR.'directory';
  120. $targetFilePath = $targetFileDirectory.\DIRECTORY_SEPARATOR.'copy_target_file';
  121. file_put_contents($sourceFilePath, 'SOURCE FILE');
  122. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  123. $this->assertTrue(is_dir($targetFileDirectory));
  124. $this->assertFileExists($targetFilePath);
  125. $this->assertStringEqualsFile($targetFilePath, 'SOURCE FILE');
  126. }
  127. /**
  128. * @group network
  129. */
  130. public function testCopyForOriginUrlsAndExistingLocalFileDefaultsToCopy()
  131. {
  132. if (!\in_array('https', stream_get_wrappers())) {
  133. $this->markTestSkipped('"https" stream wrapper is not enabled.');
  134. }
  135. $sourceFilePath = 'https://symfony.com/images/common/logo/logo_symfony_header.png';
  136. $targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
  137. file_put_contents($targetFilePath, 'TARGET FILE');
  138. $this->filesystem->copy($sourceFilePath, $targetFilePath, false);
  139. $this->assertFileExists($targetFilePath);
  140. $this->assertEquals(file_get_contents($sourceFilePath), file_get_contents($targetFilePath));
  141. }
  142. public function testMkdirCreatesDirectoriesRecursively()
  143. {
  144. $directory = $this->workspace
  145. .\DIRECTORY_SEPARATOR.'directory'
  146. .\DIRECTORY_SEPARATOR.'sub_directory';
  147. $this->filesystem->mkdir($directory);
  148. $this->assertTrue(is_dir($directory));
  149. }
  150. public function testMkdirCreatesDirectoriesFromArray()
  151. {
  152. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  153. $directories = [
  154. $basePath.'1', $basePath.'2', $basePath.'3',
  155. ];
  156. $this->filesystem->mkdir($directories);
  157. $this->assertTrue(is_dir($basePath.'1'));
  158. $this->assertTrue(is_dir($basePath.'2'));
  159. $this->assertTrue(is_dir($basePath.'3'));
  160. }
  161. public function testMkdirCreatesDirectoriesFromTraversableObject()
  162. {
  163. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  164. $directories = new \ArrayObject([
  165. $basePath.'1', $basePath.'2', $basePath.'3',
  166. ]);
  167. $this->filesystem->mkdir($directories);
  168. $this->assertTrue(is_dir($basePath.'1'));
  169. $this->assertTrue(is_dir($basePath.'2'));
  170. $this->assertTrue(is_dir($basePath.'3'));
  171. }
  172. /**
  173. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  174. */
  175. public function testMkdirCreatesDirectoriesFails()
  176. {
  177. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  178. $dir = $basePath.'2';
  179. file_put_contents($dir, '');
  180. $this->filesystem->mkdir($dir);
  181. }
  182. public function testTouchCreatesEmptyFile()
  183. {
  184. $file = $this->workspace.\DIRECTORY_SEPARATOR.'1';
  185. $this->filesystem->touch($file);
  186. $this->assertFileExists($file);
  187. }
  188. /**
  189. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  190. */
  191. public function testTouchFails()
  192. {
  193. $file = $this->workspace.\DIRECTORY_SEPARATOR.'1'.\DIRECTORY_SEPARATOR.'2';
  194. $this->filesystem->touch($file);
  195. }
  196. public function testTouchCreatesEmptyFilesFromArray()
  197. {
  198. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  199. $files = [
  200. $basePath.'1', $basePath.'2', $basePath.'3',
  201. ];
  202. $this->filesystem->touch($files);
  203. $this->assertFileExists($basePath.'1');
  204. $this->assertFileExists($basePath.'2');
  205. $this->assertFileExists($basePath.'3');
  206. }
  207. public function testTouchCreatesEmptyFilesFromTraversableObject()
  208. {
  209. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  210. $files = new \ArrayObject([
  211. $basePath.'1', $basePath.'2', $basePath.'3',
  212. ]);
  213. $this->filesystem->touch($files);
  214. $this->assertFileExists($basePath.'1');
  215. $this->assertFileExists($basePath.'2');
  216. $this->assertFileExists($basePath.'3');
  217. }
  218. public function testRemoveCleansFilesAndDirectoriesIteratively()
  219. {
  220. $basePath = $this->workspace.\DIRECTORY_SEPARATOR.'directory'.\DIRECTORY_SEPARATOR;
  221. mkdir($basePath);
  222. mkdir($basePath.'dir');
  223. touch($basePath.'file');
  224. $this->filesystem->remove($basePath);
  225. $this->assertFileNotExists($basePath);
  226. }
  227. public function testRemoveCleansArrayOfFilesAndDirectories()
  228. {
  229. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  230. mkdir($basePath.'dir');
  231. touch($basePath.'file');
  232. $files = [
  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 testRemoveCleansTraversableObjectOfFilesAndDirectories()
  240. {
  241. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  242. mkdir($basePath.'dir');
  243. touch($basePath.'file');
  244. $files = new \ArrayObject([
  245. $basePath.'dir', $basePath.'file',
  246. ]);
  247. $this->filesystem->remove($files);
  248. $this->assertFileNotExists($basePath.'dir');
  249. $this->assertFileNotExists($basePath.'file');
  250. }
  251. public function testRemoveIgnoresNonExistingFiles()
  252. {
  253. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  254. mkdir($basePath.'dir');
  255. $files = [
  256. $basePath.'dir', $basePath.'file',
  257. ];
  258. $this->filesystem->remove($files);
  259. $this->assertFileNotExists($basePath.'dir');
  260. }
  261. public function testRemoveCleansInvalidLinks()
  262. {
  263. $this->markAsSkippedIfSymlinkIsMissing();
  264. $basePath = $this->workspace.\DIRECTORY_SEPARATOR.'directory'.\DIRECTORY_SEPARATOR;
  265. mkdir($basePath);
  266. mkdir($basePath.'dir');
  267. // create symlink to nonexistent file
  268. @symlink($basePath.'file', $basePath.'file-link');
  269. // create symlink to dir using trailing forward slash
  270. $this->filesystem->symlink($basePath.'dir/', $basePath.'dir-link');
  271. $this->assertTrue(is_dir($basePath.'dir-link'));
  272. // create symlink to nonexistent dir
  273. rmdir($basePath.'dir');
  274. $this->assertFalse('\\' === \DIRECTORY_SEPARATOR ? @readlink($basePath.'dir-link') : is_dir($basePath.'dir-link'));
  275. $this->filesystem->remove($basePath);
  276. $this->assertFileNotExists($basePath);
  277. }
  278. public function testFilesExists()
  279. {
  280. $basePath = $this->workspace.\DIRECTORY_SEPARATOR.'directory'.\DIRECTORY_SEPARATOR;
  281. mkdir($basePath);
  282. touch($basePath.'file1');
  283. mkdir($basePath.'folder');
  284. $this->assertTrue($this->filesystem->exists($basePath.'file1'));
  285. $this->assertTrue($this->filesystem->exists($basePath.'folder'));
  286. }
  287. /**
  288. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  289. */
  290. public function testFilesExistsFails()
  291. {
  292. if ('\\' !== \DIRECTORY_SEPARATOR) {
  293. $this->markTestSkipped('Long file names are an issue on Windows');
  294. }
  295. $basePath = $this->workspace.'\\directory\\';
  296. $maxPathLength = PHP_MAXPATHLEN - 2;
  297. $oldPath = getcwd();
  298. mkdir($basePath);
  299. chdir($basePath);
  300. $file = str_repeat('T', $maxPathLength - \strlen($basePath) + 1);
  301. $path = $basePath.$file;
  302. exec('TYPE NUL >>'.$file); // equivalent of touch, we can not use the php touch() here because it suffers from the same limitation
  303. $this->longPathNamesWindows[] = $path; // save this so we can clean up later
  304. chdir($oldPath);
  305. $this->filesystem->exists($path);
  306. }
  307. public function testFilesExistsTraversableObjectOfFilesAndDirectories()
  308. {
  309. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  310. mkdir($basePath.'dir');
  311. touch($basePath.'file');
  312. $files = new \ArrayObject([
  313. $basePath.'dir', $basePath.'file',
  314. ]);
  315. $this->assertTrue($this->filesystem->exists($files));
  316. }
  317. public function testFilesNotExistsTraversableObjectOfFilesAndDirectories()
  318. {
  319. $basePath = $this->workspace.\DIRECTORY_SEPARATOR;
  320. mkdir($basePath.'dir');
  321. touch($basePath.'file');
  322. touch($basePath.'file2');
  323. $files = new \ArrayObject([
  324. $basePath.'dir', $basePath.'file', $basePath.'file2',
  325. ]);
  326. unlink($basePath.'file');
  327. $this->assertFalse($this->filesystem->exists($files));
  328. }
  329. public function testInvalidFileNotExists()
  330. {
  331. $basePath = $this->workspace.\DIRECTORY_SEPARATOR.'directory'.\DIRECTORY_SEPARATOR;
  332. $this->assertFalse($this->filesystem->exists($basePath.time()));
  333. }
  334. public function testChmodChangesFileMode()
  335. {
  336. $this->markAsSkippedIfChmodIsMissing();
  337. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  338. mkdir($dir);
  339. $file = $dir.\DIRECTORY_SEPARATOR.'file';
  340. touch($file);
  341. $this->filesystem->chmod($file, 0400);
  342. $this->filesystem->chmod($dir, 0753);
  343. $this->assertFilePermissions(753, $dir);
  344. $this->assertFilePermissions(400, $file);
  345. }
  346. public function testChmodWithWrongModLeavesPreviousPermissionsUntouched()
  347. {
  348. $this->markAsSkippedIfChmodIsMissing();
  349. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  350. touch($dir);
  351. $permissions = fileperms($dir);
  352. $this->filesystem->chmod($dir, 'Wrongmode');
  353. $this->assertSame($permissions, fileperms($dir));
  354. }
  355. public function testChmodRecursive()
  356. {
  357. $this->markAsSkippedIfChmodIsMissing();
  358. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  359. mkdir($dir);
  360. $file = $dir.\DIRECTORY_SEPARATOR.'file';
  361. touch($file);
  362. $this->filesystem->chmod($file, 0400, 0000, true);
  363. $this->filesystem->chmod($dir, 0753, 0000, true);
  364. $this->assertFilePermissions(753, $dir);
  365. $this->assertFilePermissions(753, $file);
  366. }
  367. public function testChmodAppliesUmask()
  368. {
  369. $this->markAsSkippedIfChmodIsMissing();
  370. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  371. touch($file);
  372. $this->filesystem->chmod($file, 0770, 0022);
  373. $this->assertFilePermissions(750, $file);
  374. }
  375. public function testChmodChangesModeOfArrayOfFiles()
  376. {
  377. $this->markAsSkippedIfChmodIsMissing();
  378. $directory = $this->workspace.\DIRECTORY_SEPARATOR.'directory';
  379. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  380. $files = [$directory, $file];
  381. mkdir($directory);
  382. touch($file);
  383. $this->filesystem->chmod($files, 0753);
  384. $this->assertFilePermissions(753, $file);
  385. $this->assertFilePermissions(753, $directory);
  386. }
  387. public function testChmodChangesModeOfTraversableFileObject()
  388. {
  389. $this->markAsSkippedIfChmodIsMissing();
  390. $directory = $this->workspace.\DIRECTORY_SEPARATOR.'directory';
  391. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  392. $files = new \ArrayObject([$directory, $file]);
  393. mkdir($directory);
  394. touch($file);
  395. $this->filesystem->chmod($files, 0753);
  396. $this->assertFilePermissions(753, $file);
  397. $this->assertFilePermissions(753, $directory);
  398. }
  399. public function testChmodChangesZeroModeOnSubdirectoriesOnRecursive()
  400. {
  401. $this->markAsSkippedIfChmodIsMissing();
  402. $directory = $this->workspace.\DIRECTORY_SEPARATOR.'directory';
  403. $subdirectory = $directory.\DIRECTORY_SEPARATOR.'subdirectory';
  404. mkdir($directory);
  405. mkdir($subdirectory);
  406. chmod($subdirectory, 0000);
  407. $this->filesystem->chmod($directory, 0753, 0000, true);
  408. $this->assertFilePermissions(753, $subdirectory);
  409. }
  410. public function testChown()
  411. {
  412. $this->markAsSkippedIfPosixIsMissing();
  413. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  414. mkdir($dir);
  415. $owner = $this->getFileOwner($dir);
  416. $this->filesystem->chown($dir, $owner);
  417. $this->assertSame($owner, $this->getFileOwner($dir));
  418. }
  419. public function testChownRecursive()
  420. {
  421. $this->markAsSkippedIfPosixIsMissing();
  422. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  423. mkdir($dir);
  424. $file = $dir.\DIRECTORY_SEPARATOR.'file';
  425. touch($file);
  426. $owner = $this->getFileOwner($dir);
  427. $this->filesystem->chown($dir, $owner, true);
  428. $this->assertSame($owner, $this->getFileOwner($file));
  429. }
  430. public function testChownSymlink()
  431. {
  432. $this->markAsSkippedIfSymlinkIsMissing();
  433. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  434. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  435. touch($file);
  436. $this->filesystem->symlink($file, $link);
  437. $owner = $this->getFileOwner($link);
  438. $this->filesystem->chown($link, $owner);
  439. $this->assertSame($owner, $this->getFileOwner($link));
  440. }
  441. public function testChownLink()
  442. {
  443. $this->markAsSkippedIfLinkIsMissing();
  444. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  445. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  446. touch($file);
  447. $this->filesystem->hardlink($file, $link);
  448. $owner = $this->getFileOwner($link);
  449. $this->filesystem->chown($link, $owner);
  450. $this->assertSame($owner, $this->getFileOwner($link));
  451. }
  452. /**
  453. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  454. */
  455. public function testChownSymlinkFails()
  456. {
  457. $this->markAsSkippedIfSymlinkIsMissing();
  458. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  459. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  460. touch($file);
  461. $this->filesystem->symlink($file, $link);
  462. $this->filesystem->chown($link, 'user'.time().mt_rand(1000, 9999));
  463. }
  464. /**
  465. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  466. */
  467. public function testChownLinkFails()
  468. {
  469. $this->markAsSkippedIfLinkIsMissing();
  470. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  471. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  472. touch($file);
  473. $this->filesystem->hardlink($file, $link);
  474. $this->filesystem->chown($link, 'user'.time().mt_rand(1000, 9999));
  475. }
  476. /**
  477. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  478. */
  479. public function testChownFail()
  480. {
  481. $this->markAsSkippedIfPosixIsMissing();
  482. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  483. mkdir($dir);
  484. $this->filesystem->chown($dir, 'user'.time().mt_rand(1000, 9999));
  485. }
  486. public function testChgrp()
  487. {
  488. $this->markAsSkippedIfPosixIsMissing();
  489. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  490. mkdir($dir);
  491. $group = $this->getFileGroup($dir);
  492. $this->filesystem->chgrp($dir, $group);
  493. $this->assertSame($group, $this->getFileGroup($dir));
  494. }
  495. public function testChgrpRecursive()
  496. {
  497. $this->markAsSkippedIfPosixIsMissing();
  498. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  499. mkdir($dir);
  500. $file = $dir.\DIRECTORY_SEPARATOR.'file';
  501. touch($file);
  502. $group = $this->getFileGroup($dir);
  503. $this->filesystem->chgrp($dir, $group, true);
  504. $this->assertSame($group, $this->getFileGroup($file));
  505. }
  506. public function testChgrpSymlink()
  507. {
  508. $this->markAsSkippedIfSymlinkIsMissing();
  509. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  510. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  511. touch($file);
  512. $this->filesystem->symlink($file, $link);
  513. $group = $this->getFileGroup($link);
  514. $this->filesystem->chgrp($link, $group);
  515. $this->assertSame($group, $this->getFileGroup($link));
  516. }
  517. public function testChgrpLink()
  518. {
  519. $this->markAsSkippedIfLinkIsMissing();
  520. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  521. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  522. touch($file);
  523. $this->filesystem->hardlink($file, $link);
  524. $group = $this->getFileGroup($link);
  525. $this->filesystem->chgrp($link, $group);
  526. $this->assertSame($group, $this->getFileGroup($link));
  527. }
  528. /**
  529. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  530. */
  531. public function testChgrpSymlinkFails()
  532. {
  533. $this->markAsSkippedIfSymlinkIsMissing();
  534. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  535. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  536. touch($file);
  537. $this->filesystem->symlink($file, $link);
  538. $this->filesystem->chgrp($link, 'user'.time().mt_rand(1000, 9999));
  539. }
  540. /**
  541. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  542. */
  543. public function testChgrpLinkFails()
  544. {
  545. $this->markAsSkippedIfLinkIsMissing();
  546. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  547. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  548. touch($file);
  549. $this->filesystem->hardlink($file, $link);
  550. $this->filesystem->chgrp($link, 'user'.time().mt_rand(1000, 9999));
  551. }
  552. /**
  553. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  554. */
  555. public function testChgrpFail()
  556. {
  557. $this->markAsSkippedIfPosixIsMissing();
  558. $dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
  559. mkdir($dir);
  560. $this->filesystem->chgrp($dir, 'user'.time().mt_rand(1000, 9999));
  561. }
  562. public function testRename()
  563. {
  564. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  565. $newPath = $this->workspace.\DIRECTORY_SEPARATOR.'new_file';
  566. touch($file);
  567. $this->filesystem->rename($file, $newPath);
  568. $this->assertFileNotExists($file);
  569. $this->assertFileExists($newPath);
  570. }
  571. /**
  572. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  573. */
  574. public function testRenameThrowsExceptionIfTargetAlreadyExists()
  575. {
  576. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  577. $newPath = $this->workspace.\DIRECTORY_SEPARATOR.'new_file';
  578. touch($file);
  579. touch($newPath);
  580. $this->filesystem->rename($file, $newPath);
  581. }
  582. public function testRenameOverwritesTheTargetIfItAlreadyExists()
  583. {
  584. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  585. $newPath = $this->workspace.\DIRECTORY_SEPARATOR.'new_file';
  586. touch($file);
  587. touch($newPath);
  588. $this->filesystem->rename($file, $newPath, true);
  589. $this->assertFileNotExists($file);
  590. $this->assertFileExists($newPath);
  591. }
  592. /**
  593. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  594. */
  595. public function testRenameThrowsExceptionOnError()
  596. {
  597. $file = $this->workspace.\DIRECTORY_SEPARATOR.uniqid('fs_test_', true);
  598. $newPath = $this->workspace.\DIRECTORY_SEPARATOR.'new_file';
  599. $this->filesystem->rename($file, $newPath);
  600. }
  601. public function testSymlink()
  602. {
  603. if ('\\' === \DIRECTORY_SEPARATOR) {
  604. $this->markTestSkipped('Windows does not support creating "broken" symlinks');
  605. }
  606. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  607. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  608. // $file does not exists right now: creating "broken" links is a wanted feature
  609. $this->filesystem->symlink($file, $link);
  610. $this->assertTrue(is_link($link));
  611. // Create the linked file AFTER creating the link
  612. touch($file);
  613. $this->assertEquals($file, readlink($link));
  614. }
  615. /**
  616. * @depends testSymlink
  617. */
  618. public function testRemoveSymlink()
  619. {
  620. $this->markAsSkippedIfSymlinkIsMissing();
  621. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  622. $this->filesystem->remove($link);
  623. $this->assertFalse(is_link($link));
  624. $this->assertFalse(is_file($link));
  625. $this->assertFalse(is_dir($link));
  626. }
  627. public function testSymlinkIsOverwrittenIfPointsToDifferentTarget()
  628. {
  629. $this->markAsSkippedIfSymlinkIsMissing();
  630. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  631. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  632. touch($file);
  633. symlink($this->workspace, $link);
  634. $this->filesystem->symlink($file, $link);
  635. $this->assertTrue(is_link($link));
  636. $this->assertEquals($file, readlink($link));
  637. }
  638. public function testSymlinkIsNotOverwrittenIfAlreadyCreated()
  639. {
  640. $this->markAsSkippedIfSymlinkIsMissing();
  641. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  642. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  643. touch($file);
  644. symlink($file, $link);
  645. $this->filesystem->symlink($file, $link);
  646. $this->assertTrue(is_link($link));
  647. $this->assertEquals($file, readlink($link));
  648. }
  649. public function testSymlinkCreatesTargetDirectoryIfItDoesNotExist()
  650. {
  651. $this->markAsSkippedIfSymlinkIsMissing();
  652. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  653. $link1 = $this->workspace.\DIRECTORY_SEPARATOR.'dir'.\DIRECTORY_SEPARATOR.'link';
  654. $link2 = $this->workspace.\DIRECTORY_SEPARATOR.'dir'.\DIRECTORY_SEPARATOR.'subdir'.\DIRECTORY_SEPARATOR.'link';
  655. touch($file);
  656. $this->filesystem->symlink($file, $link1);
  657. $this->filesystem->symlink($file, $link2);
  658. $this->assertTrue(is_link($link1));
  659. $this->assertEquals($file, readlink($link1));
  660. $this->assertTrue(is_link($link2));
  661. $this->assertEquals($file, readlink($link2));
  662. }
  663. public function testLink()
  664. {
  665. $this->markAsSkippedIfLinkIsMissing();
  666. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  667. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  668. touch($file);
  669. $this->filesystem->hardlink($file, $link);
  670. $this->assertTrue(is_file($link));
  671. $this->assertEquals(fileinode($file), fileinode($link));
  672. }
  673. /**
  674. * @depends testLink
  675. */
  676. public function testRemoveLink()
  677. {
  678. $this->markAsSkippedIfLinkIsMissing();
  679. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  680. $this->filesystem->remove($link);
  681. $this->assertTrue(!is_file($link));
  682. }
  683. public function testLinkIsOverwrittenIfPointsToDifferentTarget()
  684. {
  685. $this->markAsSkippedIfLinkIsMissing();
  686. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  687. $file2 = $this->workspace.\DIRECTORY_SEPARATOR.'file2';
  688. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  689. touch($file);
  690. touch($file2);
  691. link($file2, $link);
  692. $this->filesystem->hardlink($file, $link);
  693. $this->assertTrue(is_file($link));
  694. $this->assertEquals(fileinode($file), fileinode($link));
  695. }
  696. public function testLinkIsNotOverwrittenIfAlreadyCreated()
  697. {
  698. $this->markAsSkippedIfLinkIsMissing();
  699. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  700. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  701. touch($file);
  702. link($file, $link);
  703. $this->filesystem->hardlink($file, $link);
  704. $this->assertTrue(is_file($link));
  705. $this->assertEquals(fileinode($file), fileinode($link));
  706. }
  707. public function testLinkWithSeveralTargets()
  708. {
  709. $this->markAsSkippedIfLinkIsMissing();
  710. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  711. $link1 = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  712. $link2 = $this->workspace.\DIRECTORY_SEPARATOR.'link2';
  713. touch($file);
  714. $this->filesystem->hardlink($file, [$link1, $link2]);
  715. $this->assertTrue(is_file($link1));
  716. $this->assertEquals(fileinode($file), fileinode($link1));
  717. $this->assertTrue(is_file($link2));
  718. $this->assertEquals(fileinode($file), fileinode($link2));
  719. }
  720. public function testLinkWithSameTarget()
  721. {
  722. $this->markAsSkippedIfLinkIsMissing();
  723. $file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
  724. $link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
  725. touch($file);
  726. // practically same as testLinkIsNotOverwrittenIfAlreadyCreated
  727. $this->filesystem->hardlink($file, [$link, $link]);
  728. $this->assertTrue(is_file($link));
  729. $this->assertEquals(fileinode($file), fileinode($link));
  730. }
  731. public function testReadRelativeLink()
  732. {
  733. $this->markAsSkippedIfSymlinkIsMissing();
  734. if ('\\' === \DIRECTORY_SEPARATOR) {
  735. $this->markTestSkipped('Relative symbolic links are not supported on Windows');
  736. }
  737. $file = $this->workspace.'/file';
  738. $link1 = $this->workspace.'/dir/link';
  739. $link2 = $this->workspace.'/dir/link2';
  740. touch($file);
  741. $this->filesystem->symlink('../file', $link1);
  742. $this->filesystem->symlink('link', $link2);
  743. $this->assertEquals($this->normalize('../file'), $this->filesystem->readlink($link1));
  744. $this->assertEquals('link', $this->filesystem->readlink($link2));
  745. $this->assertEquals($file, $this->filesystem->readlink($link1, true));
  746. $this->assertEquals($file, $this->filesystem->readlink($link2, true));
  747. $this->assertEquals($file, $this->filesystem->readlink($file, true));
  748. }
  749. public function testReadAbsoluteLink()
  750. {
  751. $this->markAsSkippedIfSymlinkIsMissing();
  752. $file = $this->normalize($this->workspace.'/file');
  753. $link1 = $this->normalize($this->workspace.'/dir/link');
  754. $link2 = $this->normalize($this->workspace.'/dir/link2');
  755. touch($file);
  756. $this->filesystem->symlink($file, $link1);
  757. $this->filesystem->symlink($link1, $link2);
  758. $this->assertEquals($file, $this->filesystem->readlink($link1));
  759. $this->assertEquals($link1, $this->filesystem->readlink($link2));
  760. $this->assertEquals($file, $this->filesystem->readlink($link1, true));
  761. $this->assertEquals($file, $this->filesystem->readlink($link2, true));
  762. $this->assertEquals($file, $this->filesystem->readlink($file, true));
  763. }
  764. public function testReadBrokenLink()
  765. {
  766. $this->markAsSkippedIfSymlinkIsMissing();
  767. if ('\\' === \DIRECTORY_SEPARATOR) {
  768. $this->markTestSkipped('Windows does not support creating "broken" symlinks');
  769. }
  770. $file = $this->workspace.'/file';
  771. $link = $this->workspace.'/link';
  772. $this->filesystem->symlink($file, $link);
  773. $this->assertEquals($file, $this->filesystem->readlink($link));
  774. $this->assertNull($this->filesystem->readlink($link, true));
  775. touch($file);
  776. $this->assertEquals($file, $this->filesystem->readlink($link, true));
  777. }
  778. public function testReadLinkDefaultPathDoesNotExist()
  779. {
  780. $this->assertNull($this->filesystem->readlink($this->normalize($this->workspace.'/invalid')));
  781. }
  782. public function testReadLinkDefaultPathNotLink()
  783. {
  784. $file = $this->normalize($this->workspace.'/file');
  785. touch($file);
  786. $this->assertNull($this->filesystem->readlink($file));
  787. }
  788. public function testReadLinkCanonicalizePath()
  789. {
  790. $this->markAsSkippedIfSymlinkIsMissing();
  791. $file = $this->normalize($this->workspace.'/file');
  792. mkdir($this->normalize($this->workspace.'/dir'));
  793. touch($file);
  794. $this->assertEquals($file, $this->filesystem->readlink($this->normalize($this->workspace.'/dir/../file'), true));
  795. }
  796. public function testReadLinkCanonicalizedPathDoesNotExist()
  797. {
  798. $this->assertNull($this->filesystem->readlink($this->normalize($this->workspace.'invalid'), true));
  799. }
  800. /**
  801. * @dataProvider providePathsForMakePathRelative
  802. */
  803. public function testMakePathRelative($endPath, $startPath, $expectedPath)
  804. {
  805. $path = $this->filesystem->makePathRelative($endPath, $startPath);
  806. $this->assertEquals($expectedPath, $path);
  807. }
  808. public function providePathsForMakePathRelative()
  809. {
  810. $paths = [
  811. ['/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component', '../'],
  812. ['/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component/', '../'],
  813. ['/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component', '../'],
  814. ['/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'],
  815. ['/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'],
  816. ['/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'],
  817. ['/aa/bb', '/aa/bb', './'],
  818. ['/aa/bb', '/aa/bb/', './'],
  819. ['/aa/bb/', '/aa/bb', './'],
  820. ['/aa/bb/', '/aa/bb/', './'],
  821. ['/aa/bb/cc', '/aa/bb/cc/dd', '../'],
  822. ['/aa/bb/cc', '/aa/bb/cc/dd/', '../'],
  823. ['/aa/bb/cc/', '/aa/bb/cc/dd', '../'],
  824. ['/aa/bb/cc/', '/aa/bb/cc/dd/', '../'],
  825. ['/aa/bb/cc', '/aa', 'bb/cc/'],
  826. ['/aa/bb/cc', '/aa/', 'bb/cc/'],
  827. ['/aa/bb/cc/', '/aa', 'bb/cc/'],
  828. ['/aa/bb/cc/', '/aa/', 'bb/cc/'],
  829. ['/a/aab/bb', '/a/aa', '../aab/bb/'],
  830. ['/a/aab/bb', '/a/aa/', '../aab/bb/'],
  831. ['/a/aab/bb/', '/a/aa', '../aab/bb/'],
  832. ['/a/aab/bb/', '/a/aa/', '../aab/bb/'],
  833. ['/a/aab/bb/', '/', 'a/aab/bb/'],
  834. ['/a/aab/bb/', '/b/aab', '../../a/aab/bb/'],
  835. ['/aab/bb', '/aa', '../aab/bb/'],
  836. ['/aab', '/aa', '../aab/'],
  837. ['/aa/bb/cc', '/aa/dd/..', 'bb/cc/'],
  838. ['/aa/../bb/cc', '/aa/dd/..', '../bb/cc/'],
  839. ['/aa/bb/../../cc', '/aa/../dd/..', 'cc/'],
  840. ['/../aa/bb/cc', '/aa/dd/..', 'bb/cc/'],
  841. ['/../../aa/../bb/cc', '/aa/dd/..', '../bb/cc/'],
  842. ['C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'],
  843. ['c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc/'],
  844. ['C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'],
  845. ['C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'],
  846. ['C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'],
  847. ];
  848. if ('\\' === \DIRECTORY_SEPARATOR) {
  849. $paths[] = ['c:\var\lib/symfony/src/Symfony/', 'c:/var/lib/symfony/', 'src/Symfony/'];
  850. }
  851. return $paths;
  852. }
  853. /**
  854. * @expectedException \Symfony\Component\Filesystem\Exception\InvalidArgumentException
  855. * @expectedExceptionMessage The start path "var/lib/symfony/src/Symfony/Component" is not absolute.
  856. */
  857. public function testMakePathRelativeWithRelativeStartPath()
  858. {
  859. $this->assertSame('../../../', $this->filesystem->makePathRelative('/var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component'));
  860. }
  861. /**
  862. * @expectedException \Symfony\Component\Filesystem\Exception\InvalidArgumentException
  863. * @expectedExceptionMessage The end path "var/lib/symfony/" is not absolute.
  864. */
  865. public function testMakePathRelativeWithRelativeEndPath()
  866. {
  867. $this->assertSame('../../../', $this->filesystem->makePathRelative('var/lib/symfony/', '/var/lib/symfony/src/Symfony/Component'));
  868. }
  869. public function testMirrorCopiesFilesAndDirectoriesRecursively()
  870. {
  871. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  872. $directory = $sourcePath.'directory'.\DIRECTORY_SEPARATOR;
  873. $file1 = $directory.'file1';
  874. $file2 = $sourcePath.'file2';
  875. mkdir($sourcePath);
  876. mkdir($directory);
  877. file_put_contents($file1, 'FILE1');
  878. file_put_contents($file2, 'FILE2');
  879. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  880. $this->filesystem->mirror($sourcePath, $targetPath);
  881. $this->assertTrue(is_dir($targetPath));
  882. $this->assertTrue(is_dir($targetPath.'directory'));
  883. $this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1');
  884. $this->assertFileEquals($file2, $targetPath.'file2');
  885. $this->filesystem->remove($file1);
  886. $this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => false]);
  887. $this->assertTrue($this->filesystem->exists($targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1'));
  888. $this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);
  889. $this->assertFalse($this->filesystem->exists($targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1'));
  890. file_put_contents($file1, 'FILE1');
  891. $this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);
  892. $this->assertTrue($this->filesystem->exists($targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1'));
  893. $this->filesystem->remove($directory);
  894. $this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);
  895. $this->assertFalse($this->filesystem->exists($targetPath.'directory'));
  896. $this->assertFalse($this->filesystem->exists($targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1'));
  897. }
  898. public function testMirrorCreatesEmptyDirectory()
  899. {
  900. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  901. mkdir($sourcePath);
  902. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  903. $this->filesystem->mirror($sourcePath, $targetPath);
  904. $this->assertTrue(is_dir($targetPath));
  905. $this->filesystem->remove($sourcePath);
  906. }
  907. public function testMirrorCopiesLinks()
  908. {
  909. $this->markAsSkippedIfSymlinkIsMissing();
  910. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  911. mkdir($sourcePath);
  912. file_put_contents($sourcePath.'file1', 'FILE1');
  913. symlink($sourcePath.'file1', $sourcePath.'link1');
  914. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  915. $this->filesystem->mirror($sourcePath, $targetPath);
  916. $this->assertTrue(is_dir($targetPath));
  917. $this->assertFileEquals($sourcePath.'file1', $targetPath.'link1');
  918. $this->assertTrue(is_link($targetPath.\DIRECTORY_SEPARATOR.'link1'));
  919. }
  920. public function testMirrorCopiesLinkedDirectoryContents()
  921. {
  922. $this->markAsSkippedIfSymlinkIsMissing(true);
  923. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  924. mkdir($sourcePath.'nested/', 0777, true);
  925. file_put_contents($sourcePath.'/nested/file1.txt', 'FILE1');
  926. // Note: We symlink directory, not file
  927. symlink($sourcePath.'nested', $sourcePath.'link1');
  928. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  929. $this->filesystem->mirror($sourcePath, $targetPath);
  930. $this->assertTrue(is_dir($targetPath));
  931. $this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.'link1/file1.txt');
  932. $this->assertTrue(is_link($targetPath.\DIRECTORY_SEPARATOR.'link1'));
  933. }
  934. public function testMirrorCopiesRelativeLinkedContents()
  935. {
  936. $this->markAsSkippedIfSymlinkIsMissing(true);
  937. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  938. $oldPath = getcwd();
  939. mkdir($sourcePath.'nested/', 0777, true);
  940. file_put_contents($sourcePath.'/nested/file1.txt', 'FILE1');
  941. // Note: Create relative symlink
  942. chdir($sourcePath);
  943. symlink('nested', 'link1');
  944. chdir($oldPath);
  945. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  946. $this->filesystem->mirror($sourcePath, $targetPath);
  947. $this->assertTrue(is_dir($targetPath));
  948. $this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.'link1/file1.txt');
  949. $this->assertTrue(is_link($targetPath.\DIRECTORY_SEPARATOR.'link1'));
  950. $this->assertEquals('\\' === \DIRECTORY_SEPARATOR ? realpath($sourcePath.'\nested') : 'nested', readlink($targetPath.\DIRECTORY_SEPARATOR.'link1'));
  951. }
  952. public function testMirrorContentsWithSameNameAsSourceOrTargetWithoutDeleteOption()
  953. {
  954. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  955. mkdir($sourcePath);
  956. touch($sourcePath.'source');
  957. touch($sourcePath.'target');
  958. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  959. $oldPath = getcwd();
  960. chdir($this->workspace);
  961. $this->filesystem->mirror('source', $targetPath);
  962. chdir($oldPath);
  963. $this->assertTrue(is_dir($targetPath));
  964. $this->assertFileExists($targetPath.'source');
  965. $this->assertFileExists($targetPath.'target');
  966. }
  967. public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
  968. {
  969. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  970. mkdir($sourcePath);
  971. touch($sourcePath.'source');
  972. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  973. mkdir($targetPath);
  974. touch($targetPath.'source');
  975. touch($targetPath.'target');
  976. $oldPath = getcwd();
  977. chdir($this->workspace);
  978. $this->filesystem->mirror('source', 'target', null, ['delete' => true]);
  979. chdir($oldPath);
  980. $this->assertTrue(is_dir($targetPath));
  981. $this->assertFileExists($targetPath.'source');
  982. $this->assertFileNotExists($targetPath.'target');
  983. }
  984. public function testMirrorWithCustomIterator()
  985. {
  986. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  987. mkdir($sourcePath);
  988. $file = $sourcePath.\DIRECTORY_SEPARATOR.'file';
  989. file_put_contents($file, 'FILE');
  990. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  991. $splFile = new \SplFileInfo($file);
  992. $iterator = new \ArrayObject([$splFile]);
  993. $this->filesystem->mirror($sourcePath, $targetPath, $iterator);
  994. $this->assertTrue(is_dir($targetPath));
  995. $this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file');
  996. }
  997. /**
  998. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  999. * @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
  1000. */
  1001. public function testMirrorWithCustomIteratorWithRelativePath()
  1002. {
  1003. $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  1004. $realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
  1005. mkdir($realSourcePath);
  1006. $file = $realSourcePath.'file';
  1007. file_put_contents($file, 'FILE');
  1008. $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
  1009. $splFile = new \SplFileInfo($file);
  1010. $iterator = new \ArrayObject([$splFile]);
  1011. $this->filesystem->mirror($sourcePath, $targetPath, $iterator);
  1012. }
  1013. /**
  1014. * @dataProvider providePathsForIsAbsolutePath
  1015. */
  1016. public function testIsAbsolutePath($path, $expectedResult)
  1017. {
  1018. $result = $this->filesystem->isAbsolutePath($path);
  1019. $this->assertEquals($expectedResult, $result);
  1020. }
  1021. public function providePathsForIsAbsolutePath()
  1022. {
  1023. return [
  1024. ['/var/lib', true],
  1025. ['c:\\\\var\\lib', true],
  1026. ['\\var\\lib', true],
  1027. ['var/lib', false],
  1028. ['../var/lib', false],
  1029. ['', false],
  1030. [null, false],
  1031. ];
  1032. }
  1033. public function testTempnam()
  1034. {
  1035. $dirname = $this->workspace;
  1036. $filename = $this->filesystem->tempnam($dirname, 'foo');
  1037. $this->assertFileExists($filename);
  1038. }
  1039. public function testTempnamWithFileScheme()
  1040. {
  1041. $scheme = 'file://';
  1042. $dirname = $scheme.$this->workspace;
  1043. $filename = $this->filesystem->tempnam($dirname, 'foo');
  1044. $this->assertStringStartsWith($scheme, $filename);
  1045. $this->assertFileExists($filename);
  1046. }
  1047. public function testTempnamWithMockScheme()
  1048. {
  1049. stream_wrapper_register('mock', 'Symfony\Component\Filesystem\Tests\Fixtures\MockStream\MockStream');
  1050. $scheme = 'mock://';
  1051. $dirname = $scheme.$this->workspace;
  1052. $filename = $this->filesystem->tempnam($dirname, 'foo');
  1053. $this->assertStringStartsWith($scheme, $filename);
  1054. $this->assertFileExists($filename);
  1055. }
  1056. /**
  1057. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  1058. */
  1059. public function testTempnamWithZlibSchemeFails()
  1060. {
  1061. $scheme = 'compress.zlib://';
  1062. $dirname = $scheme.$this->workspace;
  1063. // The compress.zlib:// stream does not support mode x: creates the file, errors "failed to open stream: operation failed" and returns false
  1064. $this->filesystem->tempnam($dirname, 'bar');
  1065. }
  1066. public function testTempnamWithPHPTempSchemeFails()
  1067. {
  1068. $scheme = 'php://temp';
  1069. $dirname = $scheme;
  1070. $filename = $this->filesystem->tempnam($dirname, 'bar');
  1071. $this->assertStringStartsWith($scheme, $filename);
  1072. // The php://temp stream deletes the file after close
  1073. $this->assertFileNotExists($filename);
  1074. }
  1075. /**
  1076. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  1077. */
  1078. public function testTempnamWithPharSchemeFails()
  1079. {
  1080. // Skip test if Phar disabled phar.readonly must be 0 in php.ini
  1081. if (!\Phar::canWrite()) {
  1082. $this->markTestSkipped('This test cannot run when phar.readonly is 1.');
  1083. }
  1084. $scheme = 'phar://';
  1085. $dirname = $scheme.$this->workspace;
  1086. $pharname = 'foo.phar';
  1087. new \Phar($this->workspace.'/'.$pharname, 0, $pharname);
  1088. // 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
  1089. $this->filesystem->tempnam($dirname, $pharname.'/bar');
  1090. }
  1091. /**
  1092. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  1093. */
  1094. public function testTempnamWithHTTPSchemeFails()
  1095. {
  1096. $scheme = 'http://';
  1097. $dirname = $scheme.$this->workspace;
  1098. // The http:// scheme is read-only
  1099. $this->filesystem->tempnam($dirname, 'bar');
  1100. }
  1101. public function testTempnamOnUnwritableFallsBackToSysTmp()
  1102. {
  1103. $scheme = 'file://';
  1104. $dirname = $scheme.$this->workspace.\DIRECTORY_SEPARATOR.'does_not_exist';
  1105. $filename = $this->filesystem->tempnam($dirname, 'bar');
  1106. $realTempDir = realpath(sys_get_temp_dir());
  1107. $this->assertStringStartsWith(rtrim($scheme.$realTempDir, \DIRECTORY_SEPARATOR), $filename);
  1108. $this->assertFileExists($filename);
  1109. // Tear down
  1110. @unlink($filename);
  1111. }
  1112. public function testDumpFile()
  1113. {
  1114. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
  1115. // skip mode check on Windows
  1116. if ('\\' !== \DIRECTORY_SEPARATOR) {
  1117. $oldMask = umask(0002);
  1118. }
  1119. $this->filesystem->dumpFile($filename, 'bar');
  1120. $this->assertFileExists($filename);
  1121. $this->assertStringEqualsFile($filename, 'bar');
  1122. // skip mode check on Windows
  1123. if ('\\' !== \DIRECTORY_SEPARATOR) {
  1124. $this->assertFilePermissions(664, $filename);
  1125. umask($oldMask);
  1126. }
  1127. }
  1128. public function testDumpFileWithArray()
  1129. {
  1130. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
  1131. $this->filesystem->dumpFile($filename, ['bar']);
  1132. $this->assertFileExists($filename);
  1133. $this->assertStringEqualsFile($filename, 'bar');
  1134. }
  1135. public function testDumpFileWithResource()
  1136. {
  1137. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
  1138. $resource = fopen('php://memory', 'rw');
  1139. fwrite($resource, 'bar');
  1140. fseek($resource, 0);
  1141. $this->filesystem->dumpFile($filename, $resource);
  1142. fclose($resource);
  1143. $this->assertFileExists($filename);
  1144. $this->assertStringEqualsFile($filename, 'bar');
  1145. }
  1146. public function testDumpFileOverwritesAnExistingFile()
  1147. {
  1148. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo.txt';
  1149. file_put_contents($filename, 'FOO BAR');
  1150. $this->filesystem->dumpFile($filename, 'bar');
  1151. $this->assertFileExists($filename);
  1152. $this->assertStringEqualsFile($filename, 'bar');
  1153. }
  1154. public function testDumpFileWithFileScheme()
  1155. {
  1156. $scheme = 'file://';
  1157. $filename = $scheme.$this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
  1158. $this->filesystem->dumpFile($filename, 'bar');
  1159. $this->assertFileExists($filename);
  1160. $this->assertStringEqualsFile($filename, 'bar');
  1161. }
  1162. public function testDumpFileWithZlibScheme()
  1163. {
  1164. $scheme = 'compress.zlib://';
  1165. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
  1166. $this->filesystem->dumpFile($filename, 'bar');
  1167. // Zlib stat uses file:// wrapper so remove scheme
  1168. $this->assertFileExists(str_replace($scheme, '', $filename));
  1169. $this->assertStringEqualsFile($filename, 'bar');
  1170. }
  1171. public function testAppendToFile()
  1172. {
  1173. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'bar.txt';
  1174. // skip mode check on Windows
  1175. if ('\\' !== \DIRECTORY_SEPARATOR) {
  1176. $oldMask = umask(0002);
  1177. }
  1178. $this->filesystem->dumpFile($filename, 'foo');
  1179. $this->filesystem->appendToFile($filename, 'bar');
  1180. $this->assertFileExists($filename);
  1181. $this->assertStringEqualsFile($filename, 'foobar');
  1182. // skip mode check on Windows
  1183. if ('\\' !== \DIRECTORY_SEPARATOR) {
  1184. $this->assertFilePermissions(664, $filename);
  1185. umask($oldMask);
  1186. }
  1187. }
  1188. public function testAppendToFileWithScheme()
  1189. {
  1190. $scheme = 'file://';
  1191. $filename = $scheme.$this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
  1192. $this->filesystem->dumpFile($filename, 'foo');
  1193. $this->filesystem->appendToFile($filename, 'bar');
  1194. $this->assertFileExists($filename);
  1195. $this->assertStringEqualsFile($filename, 'foobar');
  1196. }
  1197. public function testAppendToFileWithZlibScheme()
  1198. {
  1199. $scheme = 'compress.zlib://';
  1200. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'baz.txt';
  1201. $this->filesystem->dumpFile($filename, 'foo');
  1202. // Zlib stat uses file:// wrapper so remove it
  1203. $this->assertStringEqualsFile(str_replace($scheme, '', $filename), 'foo');
  1204. $this->filesystem->appendToFile($filename, 'bar');
  1205. $this->assertFileExists($filename);
  1206. $this->assertStringEqualsFile($filename, 'foobar');
  1207. }
  1208. public function testAppendToFileCreateTheFileIfNotExists()
  1209. {
  1210. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo'.\DIRECTORY_SEPARATOR.'bar.txt';
  1211. // skip mode check on Windows
  1212. if ('\\' !== \DIRECTORY_SEPARATOR) {
  1213. $oldMask = umask(0002);
  1214. }
  1215. $this->filesystem->appendToFile($filename, 'bar');
  1216. // skip mode check on Windows
  1217. if ('\\' !== \DIRECTORY_SEPARATOR) {
  1218. $this->assertFilePermissions(664, $filename);
  1219. umask($oldMask);
  1220. }
  1221. $this->assertFileExists($filename);
  1222. $this->assertStringEqualsFile($filename, 'bar');
  1223. }
  1224. public function testDumpKeepsExistingPermissionsWhenOverwritingAnExistingFile()
  1225. {
  1226. $this->markAsSkippedIfChmodIsMissing();
  1227. $filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo.txt';
  1228. file_put_contents($filename, 'FOO BAR');
  1229. chmod($filename, 0745);
  1230. $this->filesystem->dumpFile($filename, 'bar', null);
  1231. $this->assertFilePermissions(745, $filename);
  1232. }
  1233. public function testCopyShouldKeepExecutionPermission()
  1234. {
  1235. $this->markAsSkippedIfChmodIsMissing();
  1236. $sourceFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_source_file';
  1237. $targetFilePath = $this->workspace.\DIRECTORY_SEPARATOR.'copy_target_file';
  1238. file_put_contents($sourceFilePath, 'SOURCE FILE');
  1239. chmod($sourceFilePath, 0745);
  1240. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  1241. $this->assertFilePermissions(767, $targetFilePath);
  1242. }
  1243. /**
  1244. * Normalize the given path (transform each blackslash into a real directory separator).
  1245. */
  1246. private function normalize(string $path): string
  1247. {
  1248. return str_replace('/', \DIRECTORY_SEPARATOR, $path);
  1249. }
  1250. }