FilesystemTest.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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. }