FilesystemTest.php 37 KB

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