1
0
Fork 0

Added more tests for Filesystem util and some fixes

pull/142/head
Jordi Boggiano 2011-12-04 22:40:30 +01:00
parent 304ce3b375
commit 927be089ba
2 changed files with 15 additions and 16 deletions

View File

@ -61,7 +61,7 @@ class Filesystem
$from = rtrim(strtr($from, '\\', '/'), '/'); $from = rtrim(strtr($from, '\\', '/'), '/');
$to = rtrim(strtr($to, '\\', '/'), '/'); $to = rtrim(strtr($to, '\\', '/'), '/');
$commonPath = strtr(dirname($to), '\\', '/'); $commonPath = $to;
while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) { while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
$commonPath = strtr(dirname($commonPath), '\\', '/'); $commonPath = strtr(dirname($commonPath), '\\', '/');
} }
@ -70,14 +70,10 @@ class Filesystem
return $to; return $to;
} }
if (strpos($from, $to) === 0) {
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
return str_repeat('../', $sourcePathDepth);
}
$commonPath = rtrim($commonPath, '/') . '/'; $commonPath = rtrim($commonPath, '/') . '/';
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
$commonPathCode = str_repeat('../', $sourcePathDepth); $commonPathCode = str_repeat('../', $sourcePathDepth);
return $commonPathCode . substr($to, strlen($commonPath)); return ($commonPathCode . substr($to, strlen($commonPath))) ?: './';
} }
/** /**
@ -100,26 +96,23 @@ class Filesystem
$from = strtr($from, '\\', '/'); $from = strtr($from, '\\', '/');
$to = strtr($to, '\\', '/'); $to = strtr($to, '\\', '/');
$commonPath = dirname($to); $commonPath = $to;
while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/$}i', $commonPath)) { while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) {
$commonPath = strtr(dirname($commonPath), '\\', '/'); $commonPath = strtr(dirname($commonPath), '\\', '/');
} }
if (0 !== strpos($from, $commonPath) || '/' === $commonPath) { if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) {
return var_export($to, true); return var_export($to, true);
} }
$commonPath = rtrim($commonPath, '/') . '/'; $commonPath = rtrim($commonPath, '/') . '/';
if (strpos($to, $from) === 0) { if (strpos($to, $from.'/') === 0) {
return '__DIR__ . '.var_export(substr($to, strlen($from)), true); 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; $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories;
$commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth); $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth);
return $commonPathCode . '.' . var_export('/' . substr($to, strlen($commonPath)), true); $relTarget = substr($to, strlen($commonPath));
return $commonPathCode . (strlen($relTarget) ? '.' . var_export('/' . $relTarget, true) : '');
} }
/** /**

View File

@ -49,6 +49,8 @@ class FilesystemTest extends TestCase
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/test', 'C:\Temp', true, "dirname(__DIR__)"),
array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"), array('C:/Temp', 'C:\Temp\test', true, "__DIR__ . '/test'"),
array('/tmp/test', '/tmp', true, "dirname(__DIR__)"),
array('/tmp', '/tmp/test', true, "__DIR__ . '/test'"),
); );
} }
@ -73,7 +75,11 @@ class FilesystemTest extends TestCase
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), 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:\\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/test', 'C:\Temp', "./"),
array('/tmp/test', '/tmp', "./"),
array('C:/Temp/test/sub', 'C:\Temp', "../"),
array('/tmp/test/sub', '/tmp', "../"),
array('/tmp', '/tmp/test', "test"),
array('C:/Temp', 'C:\Temp\test', "test"), array('C:/Temp', 'C:\Temp\test', "test"),
); );
} }