PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Atomic Projects/vendor/composer/composer/tests/Composer/Test/Util/FilesystemTest.php

https://gitlab.com/imamul68e/137619_PHP31
PHP | 314 lines | 234 code | 38 blank | 42 comment | 8 complexity | eca4ba1024f9ddb51e4e5e53a17eaba3 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\Filesystem;
  13. use Composer\TestCase;
  14. class FilesystemTest extends TestCase
  15. {
  16. /**
  17. * @var Filesystem
  18. */
  19. private $fs;
  20. /**
  21. * @var string
  22. */
  23. private $workingDir;
  24. /**
  25. * @var string
  26. */
  27. private $testFile;
  28. public function setUp()
  29. {
  30. $this->fs = new Filesystem;
  31. $this->workingDir = $this->getUniqueTmpDirectory();
  32. $this->testFile = $this->getUniqueTmpDirectory() . '/composer_test_file';
  33. }
  34. public function tearDown()
  35. {
  36. if (is_dir($this->workingDir)) {
  37. $this->fs->removeDirectory($this->workingDir);
  38. }
  39. if (is_file($this->testFile)) {
  40. $this->fs->removeDirectory(dirname($this->testFile));
  41. }
  42. }
  43. /**
  44. * @dataProvider providePathCouplesAsCode
  45. */
  46. public function testFindShortestPathCode($a, $b, $directory, $expected, $static = false)
  47. {
  48. $fs = new Filesystem;
  49. $this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory, $static));
  50. }
  51. public function providePathCouplesAsCode()
  52. {
  53. return array(
  54. array('/foo/bar', '/foo/bar', false, "__FILE__"),
  55. array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"),
  56. array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  57. array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"),
  58. array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  59. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
  60. array('c:/bin/run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
  61. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
  62. array('/foo/bar', '/foo/bar', true, "__DIR__"),
  63. array('/foo/bar/', '/foo/bar', true, "__DIR__"),
  64. array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"),
  65. array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  66. array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"),
  67. array('/bin/run', '/bin/run', true, "__DIR__"),
  68. array('c:/bin/run', 'c:\\bin/run', true, "__DIR__"),
  69. array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  70. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
  71. array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  72. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
  73. array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"),
  74. array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
  75. array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
  76. array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
  77. array('C:/Temp', 'c:\Temp\test', true, "__DIR__ . '/test'"),
  78. array('/tmp/test/./', '/tmp/test/', true, '__DIR__'),
  79. array('/tmp/test/../vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
  80. array('/tmp/test/.././vendor', '/tmp/test', true, "dirname(__DIR__).'/test'"),
  81. array('C:/Temp', 'c:\Temp\..\..\test', true, "dirname(__DIR__).'/test'"),
  82. array('C:/Temp/../..', 'd:\Temp\..\..\test', true, "'d:/test'"),
  83. array('/foo/bar', '/foo/bar_vendor', true, "dirname(__DIR__).'/bar_vendor'"),
  84. array('/foo/bar_vendor', '/foo/bar', true, "dirname(__DIR__).'/bar'"),
  85. array('/foo/bar_vendor', '/foo/bar/src', true, "dirname(__DIR__).'/bar/src'"),
  86. array('/foo/bar_vendor/src2', '/foo/bar/src/lib', true, "dirname(dirname(__DIR__)).'/bar/src/lib'"),
  87. // static use case
  88. array('/tmp/test/../vendor', '/tmp/test', true, "__DIR__ . '/..'.'/test'", true),
  89. array('/tmp/test/.././vendor', '/tmp/test', true, "__DIR__ . '/..'.'/test'", true),
  90. array('C:/Temp', 'c:\Temp\..\..\test', true, "__DIR__ . '/..'.'/test'", true),
  91. array('C:/Temp/../..', 'd:\Temp\..\..\test', true, "'d:/test'", true),
  92. array('/foo/bar', '/foo/bar_vendor', true, "__DIR__ . '/..'.'/bar_vendor'", true),
  93. array('/foo/bar_vendor', '/foo/bar', true, "__DIR__ . '/..'.'/bar'", true),
  94. array('/foo/bar_vendor', '/foo/bar/src', true, "__DIR__ . '/..'.'/bar/src'", true),
  95. array('/foo/bar_vendor/src2', '/foo/bar/src/lib', true, "__DIR__ . '/../..'.'/bar/src/lib'", true),
  96. );
  97. }
  98. /**
  99. * @dataProvider providePathCouples
  100. */
  101. public function testFindShortestPath($a, $b, $expected, $directory = false)
  102. {
  103. $fs = new Filesystem;
  104. $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
  105. }
  106. public function providePathCouples()
  107. {
  108. return array(
  109. array('/foo/bar', '/foo/bar', "./bar"),
  110. array('/foo/bar', '/foo/baz', "./baz"),
  111. array('/foo/bar/', '/foo/baz', "./baz"),
  112. array('/foo/bar', '/foo/bar', "./", true),
  113. array('/foo/bar', '/foo/baz', "../baz", true),
  114. array('/foo/bar/', '/foo/baz', "../baz", true),
  115. array('C:/foo/bar/', 'c:/foo/baz', "../baz", true),
  116. array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  117. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
  118. array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
  119. array('c:/foo/bin/run', 'd:/bar/bin/run', "d:/bar/bin/run", true),
  120. array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  121. array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
  122. array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  123. array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
  124. array('C:/Temp/test', 'C:\Temp', "./"),
  125. array('/tmp/test', '/tmp', "./"),
  126. array('C:/Temp/test/sub', 'C:\Temp', "../"),
  127. array('/tmp/test/sub', '/tmp', "../"),
  128. array('/tmp/test/sub', '/tmp', "../../", true),
  129. array('c:/tmp/test/sub', 'c:/tmp', "../../", true),
  130. array('/tmp', '/tmp/test', "test"),
  131. array('C:/Temp', 'C:\Temp\test', "test"),
  132. array('C:/Temp', 'c:\Temp\test', "test"),
  133. array('/tmp/test/./', '/tmp/test', './', true),
  134. array('/tmp/test/../vendor', '/tmp/test', '../test', true),
  135. array('/tmp/test/.././vendor', '/tmp/test', '../test', true),
  136. array('C:/Temp', 'c:\Temp\..\..\test', "../test", true),
  137. array('C:/Temp/../..', 'c:\Temp\..\..\test', "./test", true),
  138. array('C:/Temp/../..', 'D:\Temp\..\..\test', "d:/test", true),
  139. array('/tmp', '/tmp/../../test', '/test', true),
  140. array('/foo/bar', '/foo/bar_vendor', '../bar_vendor', true),
  141. array('/foo/bar_vendor', '/foo/bar', '../bar', true),
  142. array('/foo/bar_vendor', '/foo/bar/src', '../bar/src', true),
  143. array('/foo/bar_vendor/src2', '/foo/bar/src/lib', '../../bar/src/lib', true),
  144. array('C:/', 'C:/foo/bar/', "foo/bar", true),
  145. );
  146. }
  147. /**
  148. * @group GH-1339
  149. */
  150. public function testRemoveDirectoryPhp()
  151. {
  152. @mkdir($this->workingDir . "/level1/level2", 0777, true);
  153. file_put_contents($this->workingDir . "/level1/level2/hello.txt", "hello world");
  154. $fs = new Filesystem;
  155. $this->assertTrue($fs->removeDirectoryPhp($this->workingDir));
  156. $this->assertFalse(file_exists($this->workingDir . "/level1/level2/hello.txt"));
  157. }
  158. public function testFileSize()
  159. {
  160. file_put_contents($this->testFile, 'Hello');
  161. $fs = new Filesystem;
  162. $this->assertGreaterThanOrEqual(5, $fs->size($this->testFile));
  163. }
  164. public function testDirectorySize()
  165. {
  166. @mkdir($this->workingDir, 0777, true);
  167. file_put_contents($this->workingDir."/file1.txt", 'Hello');
  168. file_put_contents($this->workingDir."/file2.txt", 'World');
  169. $fs = new Filesystem;
  170. $this->assertGreaterThanOrEqual(10, $fs->size($this->workingDir));
  171. }
  172. /**
  173. * @dataProvider provideNormalizedPaths
  174. */
  175. public function testNormalizePath($expected, $actual)
  176. {
  177. $fs = new Filesystem;
  178. $this->assertEquals($expected, $fs->normalizePath($actual));
  179. }
  180. public function provideNormalizedPaths()
  181. {
  182. return array(
  183. array('../foo', '../foo'),
  184. array('c:/foo/bar', 'c:/foo//bar'),
  185. array('C:/foo/bar', 'C:/foo/./bar'),
  186. array('C:/foo/bar', 'C://foo//bar'),
  187. array('C:/foo/bar', 'C:///foo//bar'),
  188. array('C:/bar', 'C:/foo/../bar'),
  189. array('/bar', '/foo/../bar/'),
  190. array('phar://c:/Foo', 'phar://c:/Foo/Bar/..'),
  191. array('phar://c:/Foo', 'phar://c:///Foo/Bar/..'),
  192. array('phar://c:/', 'phar://c:/Foo/Bar/../../../..'),
  193. array('/', '/Foo/Bar/../../../..'),
  194. array('/', '/'),
  195. array('/', '//'),
  196. array('/', '///'),
  197. array('/Foo', '///Foo'),
  198. array('c:/', 'c:\\'),
  199. array('../src', 'Foo/Bar/../../../src'),
  200. array('c:../b', 'c:.\\..\\a\\..\\b'),
  201. array('phar://c:../Foo', 'phar://c:../Foo'),
  202. );
  203. }
  204. /**
  205. * @link https://github.com/composer/composer/issues/3157
  206. * @requires function symlink
  207. */
  208. public function testUnlinkSymlinkedDirectory()
  209. {
  210. $basepath = $this->workingDir;
  211. $symlinked = $basepath . "/linked";
  212. @mkdir($basepath . "/real", 0777, true);
  213. touch($basepath . "/real/FILE");
  214. $result = @symlink($basepath . "/real", $symlinked);
  215. if (!$result) {
  216. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  217. }
  218. if (!is_dir($symlinked)) {
  219. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  220. }
  221. $fs = new Filesystem();
  222. $result = $fs->unlink($symlinked);
  223. $this->assertTrue($result);
  224. $this->assertFalse(file_exists($symlinked));
  225. }
  226. /**
  227. * @link https://github.com/composer/composer/issues/3144
  228. * @requires function symlink
  229. */
  230. public function testRemoveSymlinkedDirectoryWithTrailingSlash()
  231. {
  232. @mkdir($this->workingDir . "/real", 0777, true);
  233. touch($this->workingDir . "/real/FILE");
  234. $symlinked = $this->workingDir . "/linked";
  235. $symlinkedTrailingSlash = $symlinked . "/";
  236. $result = @symlink($this->workingDir . "/real", $symlinked);
  237. if (!$result) {
  238. $this->markTestSkipped('Symbolic links for directories not supported on this platform');
  239. }
  240. if (!is_dir($symlinked)) {
  241. $this->fail('Precondition assertion failed (is_dir is false on symbolic link to directory).');
  242. }
  243. if (!is_dir($symlinkedTrailingSlash)) {
  244. $this->fail('Precondition assertion failed (is_dir false w trailing slash).');
  245. }
  246. $fs = new Filesystem();
  247. $result = $fs->removeDirectory($symlinkedTrailingSlash);
  248. $this->assertTrue($result);
  249. $this->assertFalse(file_exists($symlinkedTrailingSlash));
  250. $this->assertFalse(file_exists($symlinked));
  251. }
  252. public function testJunctions()
  253. {
  254. @mkdir($this->workingDir . '/real/nesting/testing', 0777, true);
  255. $fs = new Filesystem();
  256. // Non-Windows systems do not support this and will return false on all tests, and an exception on creation
  257. if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
  258. $this->assertFalse($fs->isJunction($this->workingDir));
  259. $this->assertFalse($fs->removeJunction($this->workingDir));
  260. $this->setExpectedException('LogicException', 'not available on non-Windows platform');
  261. }
  262. $target = $this->workingDir . '/real/../real/nesting';
  263. $junction = $this->workingDir . '/junction';
  264. // Create and detect junction
  265. $fs->junction($target, $junction);
  266. $this->assertTrue($fs->isJunction($junction));
  267. $this->assertFalse($fs->isJunction($target));
  268. $this->assertTrue($fs->isJunction($target . '/../../junction'));
  269. $this->assertFalse($fs->isJunction($junction . '/../real'));
  270. $this->assertTrue($fs->isJunction($junction . '/../junction'));
  271. // Remove junction
  272. $this->assertTrue(is_dir($junction));
  273. $this->assertTrue($fs->removeJunction($junction));
  274. $this->assertFalse(is_dir($junction));
  275. }
  276. }