FilesystemTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. use Symfony\Component\Filesystem\Filesystem;
  12. /**
  13. * Test class for Filesystem.
  14. */
  15. class FilesystemTest extends FilesystemTestCase
  16. {
  17. /**
  18. * @var \Symfony\Component\Filesystem\Filesystem $filesystem
  19. */
  20. private $filesystem = null;
  21. public function setUp()
  22. {
  23. parent::setUp();
  24. $this->filesystem = new Filesystem();
  25. }
  26. public function testCopyCreatesNewFile()
  27. {
  28. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  29. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  30. file_put_contents($sourceFilePath, 'SOURCE FILE');
  31. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  32. $this->assertFileExists($targetFilePath);
  33. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  34. }
  35. /**
  36. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  37. */
  38. public function testCopyFails()
  39. {
  40. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  41. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  42. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  43. }
  44. public function testCopyOverridesExistingFileIfModified()
  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. file_put_contents($targetFilePath, 'TARGET FILE');
  50. touch($targetFilePath, time() - 1000);
  51. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  52. $this->assertFileExists($targetFilePath);
  53. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  54. }
  55. public function testCopyDoesNotOverrideExistingFileByDefault()
  56. {
  57. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  58. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  59. file_put_contents($sourceFilePath, 'SOURCE FILE');
  60. file_put_contents($targetFilePath, 'TARGET FILE');
  61. // make sure both files have the same modification time
  62. $modificationTime = time() - 1000;
  63. touch($sourceFilePath, $modificationTime);
  64. touch($targetFilePath, $modificationTime);
  65. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  66. $this->assertFileExists($targetFilePath);
  67. $this->assertEquals('TARGET FILE', file_get_contents($targetFilePath));
  68. }
  69. public function testCopyOverridesExistingFileIfForced()
  70. {
  71. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  72. $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
  73. file_put_contents($sourceFilePath, 'SOURCE FILE');
  74. file_put_contents($targetFilePath, 'TARGET FILE');
  75. // make sure both files have the same modification time
  76. $modificationTime = time() - 1000;
  77. touch($sourceFilePath, $modificationTime);
  78. touch($targetFilePath, $modificationTime);
  79. $this->filesystem->copy($sourceFilePath, $targetFilePath, true);
  80. $this->assertFileExists($targetFilePath);
  81. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  82. }
  83. public function testCopyCreatesTargetDirectoryIfItDoesNotExist()
  84. {
  85. $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
  86. $targetFileDirectory = $this->workspace.DIRECTORY_SEPARATOR.'directory';
  87. $targetFilePath = $targetFileDirectory.DIRECTORY_SEPARATOR.'copy_target_file';
  88. file_put_contents($sourceFilePath, 'SOURCE FILE');
  89. $this->filesystem->copy($sourceFilePath, $targetFilePath);
  90. $this->assertTrue(is_dir($targetFileDirectory));
  91. $this->assertFileExists($targetFilePath);
  92. $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
  93. }
  94. public function testMkdirCreatesDirectoriesRecursively()
  95. {
  96. $directory = $this->workspace
  97. .DIRECTORY_SEPARATOR.'directory'
  98. .DIRECTORY_SEPARATOR.'sub_directory';
  99. $this->filesystem->mkdir($directory);
  100. $this->assertTrue(is_dir($directory));
  101. }
  102. public function testMkdirCreatesDirectoriesFromArray()
  103. {
  104. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  105. $directories = array(
  106. $basePath.'1', $basePath.'2', $basePath.'3'
  107. );
  108. $this->filesystem->mkdir($directories);
  109. $this->assertTrue(is_dir($basePath.'1'));
  110. $this->assertTrue(is_dir($basePath.'2'));
  111. $this->assertTrue(is_dir($basePath.'3'));
  112. }
  113. public function testMkdirCreatesDirectoriesFromTraversableObject()
  114. {
  115. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  116. $directories = new \ArrayObject(array(
  117. $basePath.'1', $basePath.'2', $basePath.'3'
  118. ));
  119. $this->filesystem->mkdir($directories);
  120. $this->assertTrue(is_dir($basePath.'1'));
  121. $this->assertTrue(is_dir($basePath.'2'));
  122. $this->assertTrue(is_dir($basePath.'3'));
  123. }
  124. /**
  125. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  126. */
  127. public function testMkdirCreatesDirectoriesFails()
  128. {
  129. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  130. $dir = $basePath.'2';
  131. file_put_contents($dir, '');
  132. $this->filesystem->mkdir($dir);
  133. }
  134. public function testTouchCreatesEmptyFile()
  135. {
  136. $file = $this->workspace.DIRECTORY_SEPARATOR.'1';
  137. $this->filesystem->touch($file);
  138. $this->assertFileExists($file);
  139. }
  140. /**
  141. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  142. */
  143. public function testTouchFails()
  144. {
  145. $file = $this->workspace.DIRECTORY_SEPARATOR.'1'.DIRECTORY_SEPARATOR.'2';
  146. $this->filesystem->touch($file);
  147. }
  148. public function testTouchCreatesEmptyFilesFromArray()
  149. {
  150. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  151. $files = array(
  152. $basePath.'1', $basePath.'2', $basePath.'3'
  153. );
  154. $this->filesystem->touch($files);
  155. $this->assertFileExists($basePath.'1');
  156. $this->assertFileExists($basePath.'2');
  157. $this->assertFileExists($basePath.'3');
  158. }
  159. public function testTouchCreatesEmptyFilesFromTraversableObject()
  160. {
  161. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  162. $files = new \ArrayObject(array(
  163. $basePath.'1', $basePath.'2', $basePath.'3'
  164. ));
  165. $this->filesystem->touch($files);
  166. $this->assertFileExists($basePath.'1');
  167. $this->assertFileExists($basePath.'2');
  168. $this->assertFileExists($basePath.'3');
  169. }
  170. public function testRemoveCleansFilesAndDirectoriesIteratively()
  171. {
  172. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  173. mkdir($basePath);
  174. mkdir($basePath.'dir');
  175. touch($basePath.'file');
  176. $this->filesystem->remove($basePath);
  177. $this->assertTrue(!is_dir($basePath));
  178. }
  179. public function testRemoveCleansArrayOfFilesAndDirectories()
  180. {
  181. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  182. mkdir($basePath.'dir');
  183. touch($basePath.'file');
  184. $files = array(
  185. $basePath.'dir', $basePath.'file'
  186. );
  187. $this->filesystem->remove($files);
  188. $this->assertTrue(!is_dir($basePath.'dir'));
  189. $this->assertTrue(!is_file($basePath.'file'));
  190. }
  191. public function testRemoveCleansTraversableObjectOfFilesAndDirectories()
  192. {
  193. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  194. mkdir($basePath.'dir');
  195. touch($basePath.'file');
  196. $files = new \ArrayObject(array(
  197. $basePath.'dir', $basePath.'file'
  198. ));
  199. $this->filesystem->remove($files);
  200. $this->assertTrue(!is_dir($basePath.'dir'));
  201. $this->assertTrue(!is_file($basePath.'file'));
  202. }
  203. public function testRemoveIgnoresNonExistingFiles()
  204. {
  205. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  206. mkdir($basePath.'dir');
  207. $files = array(
  208. $basePath.'dir', $basePath.'file'
  209. );
  210. $this->filesystem->remove($files);
  211. $this->assertTrue(!is_dir($basePath.'dir'));
  212. }
  213. public function testRemoveCleansInvalidLinks()
  214. {
  215. $this->markAsSkippedIfSymlinkIsMissing();
  216. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  217. mkdir($basePath);
  218. mkdir($basePath.'dir');
  219. // create symlink to unexisting file
  220. @symlink($basePath.'file', $basePath.'link');
  221. $this->filesystem->remove($basePath);
  222. $this->assertTrue(!is_dir($basePath));
  223. }
  224. public function testFilesExists()
  225. {
  226. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  227. mkdir($basePath);
  228. touch($basePath.'file1');
  229. mkdir($basePath.'folder');
  230. $this->assertTrue($this->filesystem->exists($basePath.'file1'));
  231. $this->assertTrue($this->filesystem->exists($basePath.'folder'));
  232. }
  233. public function testFilesExistsTraversableObjectOfFilesAndDirectories()
  234. {
  235. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  236. mkdir($basePath.'dir');
  237. touch($basePath.'file');
  238. $files = new \ArrayObject(array(
  239. $basePath.'dir', $basePath.'file'
  240. ));
  241. $this->assertTrue($this->filesystem->exists($files));
  242. }
  243. public function testFilesNotExistsTraversableObjectOfFilesAndDirectories()
  244. {
  245. $basePath = $this->workspace.DIRECTORY_SEPARATOR;
  246. mkdir($basePath.'dir');
  247. touch($basePath.'file');
  248. touch($basePath.'file2');
  249. $files = new \ArrayObject(array(
  250. $basePath.'dir', $basePath.'file', $basePath.'file2'
  251. ));
  252. unlink($basePath.'file');
  253. $this->assertFalse($this->filesystem->exists($files));
  254. }
  255. public function testInvalidFileNotExists()
  256. {
  257. $basePath = $this->workspace.DIRECTORY_SEPARATOR.'directory'.DIRECTORY_SEPARATOR;
  258. $this->assertFalse($this->filesystem->exists($basePath.time()));
  259. }
  260. public function testChmodChangesFileMode()
  261. {
  262. $this->markAsSkippedIfChmodIsMissing();
  263. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  264. mkdir($dir);
  265. $file = $dir.DIRECTORY_SEPARATOR.'file';
  266. touch($file);
  267. $this->filesystem->chmod($file, 0400);
  268. $this->filesystem->chmod($dir, 0753);
  269. $this->assertFilePermissions(753, $dir);
  270. $this->assertFilePermissions(400, $file);
  271. }
  272. public function testChmodWrongMod()
  273. {
  274. $this->markAsSkippedIfChmodIsMissing();
  275. $dir = $this->workspace.DIRECTORY_SEPARATOR.'file';
  276. touch($dir);
  277. $this->filesystem->chmod($dir, 'Wrongmode');
  278. }
  279. public function testChmodRecursive()
  280. {
  281. $this->markAsSkippedIfChmodIsMissing();
  282. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  283. mkdir($dir);
  284. $file = $dir.DIRECTORY_SEPARATOR.'file';
  285. touch($file);
  286. $this->filesystem->chmod($file, 0400, 0000, true);
  287. $this->filesystem->chmod($dir, 0753, 0000, true);
  288. $this->assertFilePermissions(753, $dir);
  289. $this->assertFilePermissions(753, $file);
  290. }
  291. public function testChmodAppliesUmask()
  292. {
  293. $this->markAsSkippedIfChmodIsMissing();
  294. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  295. touch($file);
  296. $this->filesystem->chmod($file, 0770, 0022);
  297. $this->assertFilePermissions(750, $file);
  298. }
  299. public function testChmodChangesModeOfArrayOfFiles()
  300. {
  301. $this->markAsSkippedIfChmodIsMissing();
  302. $directory = $this->workspace.DIRECTORY_SEPARATOR.'directory';
  303. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  304. $files = array($directory, $file);
  305. mkdir($directory);
  306. touch($file);
  307. $this->filesystem->chmod($files, 0753);
  308. $this->assertFilePermissions(753, $file);
  309. $this->assertFilePermissions(753, $directory);
  310. }
  311. public function testChmodChangesModeOfTraversableFileObject()
  312. {
  313. $this->markAsSkippedIfChmodIsMissing();
  314. $directory = $this->workspace.DIRECTORY_SEPARATOR.'directory';
  315. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  316. $files = new \ArrayObject(array($directory, $file));
  317. mkdir($directory);
  318. touch($file);
  319. $this->filesystem->chmod($files, 0753);
  320. $this->assertFilePermissions(753, $file);
  321. $this->assertFilePermissions(753, $directory);
  322. }
  323. public function testChown()
  324. {
  325. $this->markAsSkippedIfPosixIsMissing();
  326. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  327. mkdir($dir);
  328. $this->filesystem->chown($dir, $this->getFileOwner($dir));
  329. }
  330. public function testChownRecursive()
  331. {
  332. $this->markAsSkippedIfPosixIsMissing();
  333. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  334. mkdir($dir);
  335. $file = $dir.DIRECTORY_SEPARATOR.'file';
  336. touch($file);
  337. $this->filesystem->chown($dir, $this->getFileOwner($dir), true);
  338. }
  339. public function testChownSymlink()
  340. {
  341. $this->markAsSkippedIfSymlinkIsMissing();
  342. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  343. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  344. touch($file);
  345. $this->filesystem->symlink($file, $link);
  346. $this->filesystem->chown($link, $this->getFileOwner($link));
  347. }
  348. /**
  349. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  350. */
  351. public function testChownSymlinkFails()
  352. {
  353. $this->markAsSkippedIfSymlinkIsMissing();
  354. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  355. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  356. touch($file);
  357. $this->filesystem->symlink($file, $link);
  358. $this->filesystem->chown($link, 'user'.time().mt_rand(1000, 9999));
  359. }
  360. /**
  361. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  362. */
  363. public function testChownFail()
  364. {
  365. $this->markAsSkippedIfPosixIsMissing();
  366. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  367. mkdir($dir);
  368. $this->filesystem->chown($dir, 'user'.time().mt_rand(1000, 9999));
  369. }
  370. public function testChgrp()
  371. {
  372. $this->markAsSkippedIfPosixIsMissing();
  373. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  374. mkdir($dir);
  375. $this->filesystem->chgrp($dir, $this->getFileGroup($dir));
  376. }
  377. public function testChgrpRecursive()
  378. {
  379. $this->markAsSkippedIfPosixIsMissing();
  380. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  381. mkdir($dir);
  382. $file = $dir.DIRECTORY_SEPARATOR.'file';
  383. touch($file);
  384. $this->filesystem->chgrp($dir, $this->getFileGroup($dir), true);
  385. }
  386. public function testChgrpSymlink()
  387. {
  388. $this->markAsSkippedIfSymlinkIsMissing();
  389. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  390. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  391. touch($file);
  392. $this->filesystem->symlink($file, $link);
  393. $this->filesystem->chgrp($link, $this->getFileGroup($link));
  394. }
  395. /**
  396. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  397. */
  398. public function testChgrpSymlinkFails()
  399. {
  400. $this->markAsSkippedIfSymlinkIsMissing();
  401. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  402. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  403. touch($file);
  404. $this->filesystem->symlink($file, $link);
  405. $this->filesystem->chgrp($link, 'user'.time().mt_rand(1000, 9999));
  406. }
  407. /**
  408. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  409. */
  410. public function testChgrpFail()
  411. {
  412. $this->markAsSkippedIfPosixIsMissing();
  413. $dir = $this->workspace.DIRECTORY_SEPARATOR.'dir';
  414. mkdir($dir);
  415. $this->filesystem->chgrp($dir, 'user'.time().mt_rand(1000, 9999));
  416. }
  417. public function testRename()
  418. {
  419. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  420. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  421. touch($file);
  422. $this->filesystem->rename($file, $newPath);
  423. $this->assertFileNotExists($file);
  424. $this->assertFileExists($newPath);
  425. }
  426. /**
  427. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  428. */
  429. public function testRenameThrowsExceptionIfTargetAlreadyExists()
  430. {
  431. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  432. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  433. touch($file);
  434. touch($newPath);
  435. $this->filesystem->rename($file, $newPath);
  436. }
  437. public function testRenameOverwritesTheTargetIfItAlreadyExists()
  438. {
  439. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  440. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  441. touch($file);
  442. touch($newPath);
  443. $this->filesystem->rename($file, $newPath, true);
  444. $this->assertFileNotExists($file);
  445. $this->assertFileExists($newPath);
  446. }
  447. /**
  448. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  449. */
  450. public function testRenameThrowsExceptionOnError()
  451. {
  452. $file = $this->workspace.DIRECTORY_SEPARATOR.uniqid();
  453. $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
  454. $this->filesystem->rename($file, $newPath);
  455. }
  456. public function testSymlink()
  457. {
  458. $this->markAsSkippedIfSymlinkIsMissing();
  459. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  460. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  461. touch($file);
  462. $this->filesystem->symlink($file, $link);
  463. $this->assertTrue(is_link($link));
  464. $this->assertEquals($file, readlink($link));
  465. }
  466. /**
  467. * @depends testSymlink
  468. */
  469. public function testRemoveSymlink()
  470. {
  471. $this->markAsSkippedIfSymlinkIsMissing();
  472. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  473. $this->filesystem->remove($link);
  474. $this->assertTrue(!is_link($link));
  475. }
  476. public function testSymlinkIsOverwrittenIfPointsToDifferentTarget()
  477. {
  478. $this->markAsSkippedIfSymlinkIsMissing();
  479. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  480. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  481. touch($file);
  482. symlink($this->workspace, $link);
  483. $this->filesystem->symlink($file, $link);
  484. $this->assertTrue(is_link($link));
  485. $this->assertEquals($file, readlink($link));
  486. }
  487. public function testSymlinkIsNotOverwrittenIfAlreadyCreated()
  488. {
  489. $this->markAsSkippedIfSymlinkIsMissing();
  490. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  491. $link = $this->workspace.DIRECTORY_SEPARATOR.'link';
  492. touch($file);
  493. symlink($file, $link);
  494. $this->filesystem->symlink($file, $link);
  495. $this->assertTrue(is_link($link));
  496. $this->assertEquals($file, readlink($link));
  497. }
  498. public function testSymlinkCreatesTargetDirectoryIfItDoesNotExist()
  499. {
  500. $this->markAsSkippedIfSymlinkIsMissing();
  501. $file = $this->workspace.DIRECTORY_SEPARATOR.'file';
  502. $link1 = $this->workspace.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'link';
  503. $link2 = $this->workspace.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'subdir'.DIRECTORY_SEPARATOR.'link';
  504. touch($file);
  505. $this->filesystem->symlink($file, $link1);
  506. $this->filesystem->symlink($file, $link2);
  507. $this->assertTrue(is_link($link1));
  508. $this->assertEquals($file, readlink($link1));
  509. $this->assertTrue(is_link($link2));
  510. $this->assertEquals($file, readlink($link2));
  511. }
  512. /**
  513. * @dataProvider providePathsForMakePathRelative
  514. */
  515. public function testMakePathRelative($endPath, $startPath, $expectedPath)
  516. {
  517. $path = $this->filesystem->makePathRelative($endPath, $startPath);
  518. $this->assertEquals($expectedPath, $path);
  519. }
  520. /**
  521. * @return array
  522. */
  523. public function providePathsForMakePathRelative()
  524. {
  525. $paths = array(
  526. array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component', '../'),
  527. array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component/', '../'),
  528. array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component', '../'),
  529. array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'),
  530. array('var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../'),
  531. array('/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'),
  532. array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'),
  533. array('/aa/bb', '/aa/bb', './'),
  534. array('/aa/bb', '/aa/bb/', './'),
  535. array('/aa/bb/', '/aa/bb', './'),
  536. array('/aa/bb/', '/aa/bb/', './'),
  537. array('/aa/bb/cc', '/aa/bb/cc/dd', '../'),
  538. array('/aa/bb/cc', '/aa/bb/cc/dd/', '../'),
  539. array('/aa/bb/cc/', '/aa/bb/cc/dd', '../'),
  540. array('/aa/bb/cc/', '/aa/bb/cc/dd/', '../'),
  541. array('/aa/bb/cc', '/aa', 'bb/cc/'),
  542. array('/aa/bb/cc', '/aa/', 'bb/cc/'),
  543. array('/aa/bb/cc/', '/aa', 'bb/cc/'),
  544. array('/aa/bb/cc/', '/aa/', 'bb/cc/'),
  545. array('/a/aab/bb', '/a/aa', '../aab/bb/'),
  546. array('/a/aab/bb', '/a/aa/', '../aab/bb/'),
  547. array('/a/aab/bb/', '/a/aa', '../aab/bb/'),
  548. array('/a/aab/bb/', '/a/aa/', '../aab/bb/'),
  549. );
  550. if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
  551. $paths[] = array('c:\var\lib/symfony/src/Symfony/', 'c:/var/lib/symfony/', 'src/Symfony/');
  552. }
  553. return $paths;
  554. }
  555. public function testMirrorCopiesFilesAndDirectoriesRecursively()
  556. {
  557. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  558. $directory = $sourcePath.'directory'.DIRECTORY_SEPARATOR;
  559. $file1 = $directory.'file1';
  560. $file2 = $sourcePath.'file2';
  561. mkdir($sourcePath);
  562. mkdir($directory);
  563. file_put_contents($file1, 'FILE1');
  564. file_put_contents($file2, 'FILE2');
  565. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  566. $this->filesystem->mirror($sourcePath, $targetPath);
  567. $this->assertTrue(is_dir($targetPath));
  568. $this->assertTrue(is_dir($targetPath.'directory'));
  569. $this->assertFileEquals($file1, $targetPath.'directory'.DIRECTORY_SEPARATOR.'file1');
  570. $this->assertFileEquals($file2, $targetPath.'file2');
  571. $this->filesystem->remove($file1);
  572. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => false));
  573. $this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  574. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => true));
  575. $this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  576. file_put_contents($file1, 'FILE1');
  577. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => true));
  578. $this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  579. $this->filesystem->remove($directory);
  580. $this->filesystem->mirror($sourcePath, $targetPath, null, array('delete' => true));
  581. $this->assertFalse($this->filesystem->exists($targetPath.'directory'));
  582. $this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  583. }
  584. public function testMirrorCopiesLinks()
  585. {
  586. $this->markAsSkippedIfSymlinkIsMissing();
  587. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  588. mkdir($sourcePath);
  589. file_put_contents($sourcePath.'file1', 'FILE1');
  590. symlink($sourcePath.'file1', $sourcePath.'link1');
  591. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  592. $this->filesystem->mirror($sourcePath, $targetPath);
  593. $this->assertTrue(is_dir($targetPath));
  594. $this->assertFileEquals($sourcePath.'file1', $targetPath.DIRECTORY_SEPARATOR.'link1');
  595. $this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
  596. }
  597. public function testMirrorCopiesLinkedDirectoryContents()
  598. {
  599. $this->markAsSkippedIfSymlinkIsMissing();
  600. $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
  601. mkdir($sourcePath.'nested/', 0777, true);
  602. file_put_contents($sourcePath.'/nested/file1.txt', 'FILE1');
  603. // Note: We symlink directory, not file
  604. symlink($sourcePath.'nested', $sourcePath.'link1');
  605. $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
  606. $this->filesystem->mirror($sourcePath, $targetPath);
  607. $this->assertTrue(is_dir($targetPath));
  608. $this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.DIRECTORY_SEPARATOR.'link1/file1.txt');
  609. $this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
  610. }
  611. /**
  612. * @dataProvider providePathsForIsAbsolutePath
  613. */
  614. public function testIsAbsolutePath($path, $expectedResult)
  615. {
  616. $result = $this->filesystem->isAbsolutePath($path);
  617. $this->assertEquals($expectedResult, $result);
  618. }
  619. /**
  620. * @return array
  621. */
  622. public function providePathsForIsAbsolutePath()
  623. {
  624. return array(
  625. array('/var/lib', true),
  626. array('c:\\\\var\\lib', true),
  627. array('\\var\\lib', true),
  628. array('var/lib', false),
  629. array('../var/lib', false),
  630. array('', false),
  631. array(null, false)
  632. );
  633. }
  634. public function testDumpFile()
  635. {
  636. $filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
  637. $this->filesystem->dumpFile($filename, 'bar', 0753);
  638. $this->assertFileExists($filename);
  639. $this->assertSame('bar', file_get_contents($filename));
  640. // skip mode check on windows
  641. if (!defined('PHP_WINDOWS_VERSION_MAJOR')) {
  642. $this->assertFilePermissions(753, $filename);
  643. }
  644. }
  645. public function testDumpFileOverwritesAnExistingFile()
  646. {
  647. $filename = $this->workspace.DIRECTORY_SEPARATOR.'foo.txt';
  648. file_put_contents($filename, 'FOO BAR');
  649. $this->filesystem->dumpFile($filename, 'bar');
  650. $this->assertFileExists($filename);
  651. $this->assertSame('bar', file_get_contents($filename));
  652. }
  653. }