Added tests and fixed some incorrect behaviors in Filesystem

main
Jordi Boggiano 13 years ago
parent bc88d86983
commit 4517a2e51e

@ -58,8 +58,8 @@ class Filesystem
if (dirname($from) === dirname($to)) {
return './'.basename($to);
}
$from = strtr($from, '\\', '/');
$to = strtr($to, '\\', '/');
$from = rtrim(strtr($from, '\\', '/'), '/');
$to = rtrim(strtr($to, '\\', '/'), '/');
$commonPath = dirname($to);
while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/$}i', $commonPath)) {
@ -70,6 +70,10 @@ class Filesystem
return $to;
}
if (strpos($from, $to) === 0) {
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
return str_repeat('../', $sourcePathDepth);
}
$commonPath = rtrim($commonPath, '/') . '/';
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
$commonPathCode = str_repeat('../', $sourcePathDepth);
@ -81,16 +85,17 @@ class Filesystem
*
* @param string $from
* @param string $to
* @param Boolean $directories if true, the source/target are considered to be directories
* @return string
*/
public function findShortestPathCode($from, $to)
public function findShortestPathCode($from, $to, $directories = false)
{
if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
throw new \InvalidArgumentException('from and to must be absolute paths');
}
if ($from === $to) {
return '__FILE__';
return $directories ? '__DIR__' : '__FILE__';
}
$from = strtr($from, '\\', '/');
$to = strtr($to, '\\', '/');
@ -105,7 +110,14 @@ class Filesystem
}
$commonPath = rtrim($commonPath, '/') . '/';
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
if (strpos($to, $from) === 0) {
return '__DIR__ . '.var_export(substr($to, strlen($from)), true);
}
if (strpos($from, $to) === 0) {
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') - 1 + $directories;
return str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
}
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories;
$commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
return $commonPathCode . '.' . var_export('/' . substr($to, strlen($commonPath)), true);
}

@ -20,24 +20,35 @@ class FilesystemTest extends TestCase
/**
* @dataProvider providePathCouplesAsCode
*/
public function testFindShortestPathCode($a, $b, $expected)
public function testFindShortestPathCode($a, $b, $directory, $expected)
{
$fs = new Filesystem;
$this->assertEquals($expected, $fs->findShortestPathCode($a, $b));
$this->assertEquals($expected, $fs->findShortestPathCode($a, $b, $directory));
}
public function providePathCouplesAsCode()
{
return array(
array('/foo/bar', '/foo/bar', "__FILE__"),
array('/foo/bar', '/foo/baz', "__DIR__.'/baz'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/bar/bin/run', "'/bar/bin/run'"),
array('c:/bin/run', 'c:/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', "'d:/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "'d:/vendor/acme/bin/run'"),
array('/foo/bar', '/foo/bar', false, "__FILE__"),
array('/foo/bar', '/foo/baz', false, "__DIR__.'/baz'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/bar/bin/run', false, "'/bar/bin/run'"),
array('c:/bin/run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', false, "dirname(__DIR__).'/vendor/acme/bin/run'"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', false, "'d:/vendor/acme/bin/run'"),
array('/foo/bar', '/foo/bar', true, "__DIR__"),
array('/foo/bar', '/foo/baz', true, "dirname(__DIR__).'/baz'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('/foo/bin/run', '/bar/bin/run', true, "'/bar/bin/run'"),
array('c:/bin/run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', true, "dirname(dirname(__DIR__)).'/vendor/acme/bin/run'"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', true, "'d:/vendor/acme/bin/run'"),
array('C:/Temp/test', 'C:\Temp', true, "dirname(__DIR__)"),
array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
);
}
@ -62,6 +73,8 @@ class FilesystemTest extends TestCase
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
array('c:\\bin\\run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
array('C:/Temp/test', 'C:\Temp', "../"),
array('C:/Temp', 'C:\Temp\test', "test"),
);
}
}

Loading…
Cancel
Save