From 512b476152e2d54a80572f83eb648662da213a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Sun, 7 Apr 2019 12:44:45 +0200 Subject: [PATCH] Add the Path class --- src/Symfony/Component/Filesystem/CHANGELOG.md | 6 + .../Filesystem/Exception/RuntimeException.php | 19 + src/Symfony/Component/Filesystem/Path.php | 819 +++++++++++++ .../Component/Filesystem/Tests/PathTest.php | 1055 +++++++++++++++++ .../Component/Filesystem/composer.json | 1 + 5 files changed, 1900 insertions(+) create mode 100644 src/Symfony/Component/Filesystem/Exception/RuntimeException.php create mode 100644 src/Symfony/Component/Filesystem/Path.php create mode 100644 src/Symfony/Component/Filesystem/Tests/PathTest.php diff --git a/src/Symfony/Component/Filesystem/CHANGELOG.md b/src/Symfony/Component/Filesystem/CHANGELOG.md index 4a0755bfe0a83..b04d4d7889044 100644 --- a/src/Symfony/Component/Filesystem/CHANGELOG.md +++ b/src/Symfony/Component/Filesystem/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +5.4.0 +----- + +* Add `Path` class + + 5.0.0 ----- diff --git a/src/Symfony/Component/Filesystem/Exception/RuntimeException.php b/src/Symfony/Component/Filesystem/Exception/RuntimeException.php new file mode 100644 index 0000000000000..a7512dca73e12 --- /dev/null +++ b/src/Symfony/Component/Filesystem/Exception/RuntimeException.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Exception; + +/** + * @author Théo Fidry + */ +class RuntimeException extends \RuntimeException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Filesystem/Path.php b/src/Symfony/Component/Filesystem/Path.php new file mode 100644 index 0000000000000..187632be8cc0a --- /dev/null +++ b/src/Symfony/Component/Filesystem/Path.php @@ -0,0 +1,819 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem; + +use Symfony\Component\Filesystem\Exception\InvalidArgumentException; +use Symfony\Component\Filesystem\Exception\RuntimeException; + +/** + * Contains utility methods for handling path strings. + * + * The methods in this class are able to deal with both UNIX and Windows paths + * with both forward and backward slashes. All methods return normalized parts + * containing only forward slashes and no excess "." and ".." segments. + * + * @author Bernhard Schussek + * @author Thomas Schulz + * @author Théo Fidry + */ +final class Path +{ + /** + * The number of buffer entries that triggers a cleanup operation. + */ + private const CLEANUP_THRESHOLD = 1250; + + /** + * The buffer size after the cleanup operation. + */ + private const CLEANUP_SIZE = 1000; + + /** + * Buffers input/output of {@link canonicalize()}. + * + * @var array + */ + private static $buffer = []; + + /** + * @var int + */ + private static $bufferSize = 0; + + /** + * Canonicalizes the given path. + * + * During normalization, all slashes are replaced by forward slashes ("/"). + * Furthermore, all "." and ".." segments are removed as far as possible. + * ".." segments at the beginning of relative paths are not removed. + * + * ```php + * echo Path::canonicalize("\symfony\puli\..\css\style.css"); + * // => /symfony/css/style.css + * + * echo Path::canonicalize("../css/./style.css"); + * // => ../css/style.css + * ``` + * + * This method is able to deal with both UNIX and Windows paths. + */ + public static function canonicalize(string $path): string + { + if ('' === $path) { + return ''; + } + + // This method is called by many other methods in this class. Buffer + // the canonicalized paths to make up for the severe performance + // decrease. + if (isset(self::$buffer[$path])) { + return self::$buffer[$path]; + } + + // Replace "~" with user's home directory. + if ('~' === $path[0]) { + $path = self::getHomeDirectory().mb_substr($path, 1); + } + + $path = self::normalize($path); + + [$root, $pathWithoutRoot] = self::split($path); + + $canonicalParts = self::findCanonicalParts($root, $pathWithoutRoot); + + // Add the root directory again + self::$buffer[$path] = $canonicalPath = $root.implode('/', $canonicalParts); + ++self::$bufferSize; + + // Clean up regularly to prevent memory leaks + if (self::$bufferSize > self::CLEANUP_THRESHOLD) { + self::$buffer = \array_slice(self::$buffer, -self::CLEANUP_SIZE, null, true); + self::$bufferSize = self::CLEANUP_SIZE; + } + + return $canonicalPath; + } + + /** + * Normalizes the given path. + * + * During normalization, all slashes are replaced by forward slashes ("/"). + * Contrary to {@link canonicalize()}, this method does not remove invalid + * or dot path segments. Consequently, it is much more efficient and should + * be used whenever the given path is known to be a valid, absolute system + * path. + * + * This method is able to deal with both UNIX and Windows paths. + */ + public static function normalize(string $path): string + { + return str_replace('\\', '/', $path); + } + + /** + * Returns the directory part of the path. + * + * This method is similar to PHP's dirname(), but handles various cases + * where dirname() returns a weird result: + * + * - dirname() does not accept backslashes on UNIX + * - dirname("C:/symfony") returns "C:", not "C:/" + * - dirname("C:/") returns ".", not "C:/" + * - dirname("C:") returns ".", not "C:/" + * - dirname("symfony") returns ".", not "" + * - dirname() does not canonicalize the result + * + * This method fixes these shortcomings and behaves like dirname() + * otherwise. + * + * The result is a canonical path. + * + * @return string The canonical directory part. Returns the root directory + * if the root directory is passed. Returns an empty string + * if a relative path is passed that contains no slashes. + * Returns an empty string if an empty string is passed. + */ + public static function getDirectory(string $path): string + { + if ('' === $path) { + return ''; + } + + $path = self::canonicalize($path); + + // Maintain scheme + if (false !== ($schemeSeparatorPosition = mb_strpos($path, '://'))) { + $scheme = mb_substr($path, 0, $schemeSeparatorPosition + 3); + $path = mb_substr($path, $schemeSeparatorPosition + 3); + } else { + $scheme = ''; + } + + if (false === ($dirSeparatorPosition = strrpos($path, '/'))) { + return ''; + } + + // Directory equals root directory "/" + if (0 === $dirSeparatorPosition) { + return $scheme.'/'; + } + + // Directory equals Windows root "C:/" + if (2 === $dirSeparatorPosition && ctype_alpha($path[0]) && ':' === $path[1]) { + return $scheme.mb_substr($path, 0, 3); + } + + return $scheme.mb_substr($path, 0, $dirSeparatorPosition); + } + + /** + * Returns canonical path of the user's home directory. + * + * Supported operating systems: + * + * - UNIX + * - Windows8 and upper + * + * If your operation system or environment isn't supported, an exception is thrown. + * + * The result is a canonical path. + * + * @throws RuntimeException If your operation system or environment isn't supported + */ + public static function getHomeDirectory(): string + { + // For UNIX support + if (getenv('HOME')) { + return self::canonicalize(getenv('HOME')); + } + + // For >= Windows8 support + if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) { + return self::canonicalize(getenv('HOMEDRIVE').getenv('HOMEPATH')); + } + + throw new RuntimeException("Cannot find the home directory path: Your environment or operation system isn't supported."); + } + + /** + * Returns the root directory of a path. + * + * The result is a canonical path. + * + * @return string The canonical root directory. Returns an empty string if + * the given path is relative or empty. + */ + public static function getRoot(string $path): string + { + if ('' === $path) { + return ''; + } + + // Maintain scheme + if (false !== ($schemeSeparatorPosition = strpos($path, '://'))) { + $scheme = substr($path, 0, $schemeSeparatorPosition + 3); + $path = substr($path, $schemeSeparatorPosition + 3); + } else { + $scheme = ''; + } + + $firstCharacter = $path[0]; + + // UNIX root "/" or "\" (Windows style) + if ('/' === $firstCharacter || '\\' === $firstCharacter) { + return $scheme.'/'; + } + + $length = mb_strlen($path); + + // Windows root + if ($length > 1 && ':' === $path[1] && ctype_alpha($firstCharacter)) { + // Special case: "C:" + if (2 === $length) { + return $scheme.$path.'/'; + } + + // Normal case: "C:/ or "C:\" + if ('/' === $path[2] || '\\' === $path[2]) { + return $scheme.$firstCharacter.$path[1].'/'; + } + } + + return ''; + } + + /** + * Returns the file name without the extension from a file path. + * + * @param string|null $extension if specified, only that extension is cut + * off (may contain leading dot) + */ + public static function getFilenameWithoutExtension(string $path, string $extension = null) + { + if ('' === $path) { + return ''; + } + + if (null !== $extension) { + // remove extension and trailing dot + return rtrim(basename($path, $extension), '.'); + } + + return pathinfo($path, \PATHINFO_FILENAME); + } + + /** + * Returns the extension from a file path (without leading dot). + * + * @param bool $forceLowerCase forces the extension to be lower-case + */ + public static function getExtension(string $path, bool $forceLowerCase = false): string + { + if ('' === $path) { + return ''; + } + + $extension = pathinfo($path, \PATHINFO_EXTENSION); + + if ($forceLowerCase) { + $extension = self::toLower($extension); + } + + return $extension; + } + + /** + * Returns whether the path has an (or the specified) extension. + * + * @param string $path the path string + * @param string|string[]|null $extensions if null or not provided, checks if + * an extension exists, otherwise + * checks for the specified extension + * or array of extensions (with or + * without leading dot) + * @param bool $ignoreCase whether to ignore case-sensitivity + */ + public static function hasExtension(string $path, $extensions = null, bool $ignoreCase = false): bool + { + if ('' === $path) { + return false; + } + + $actualExtension = self::getExtension($path, $ignoreCase); + + // Only check if path has any extension + if ([] === $extensions || null === $extensions) { + return '' !== $actualExtension; + } + + if (\is_string($extensions)) { + $extensions = [$extensions]; + } + + foreach ($extensions as $key => $extension) { + if ($ignoreCase) { + $extension = self::toLower($extension); + } + + // remove leading '.' in extensions array + $extensions[$key] = ltrim($extension, '.'); + } + + return \in_array($actualExtension, $extensions, true); + } + + /** + * Changes the extension of a path string. + * + * @param string $path The path string with filename.ext to change. + * @param string $extension new extension (with or without leading dot) + * + * @return string the path string with new file extension + */ + public static function changeExtension(string $path, string $extension): string + { + if ('' === $path) { + return ''; + } + + $actualExtension = self::getExtension($path); + $extension = ltrim($extension, '.'); + + // No extension for paths + if ('/' === mb_substr($path, -1)) { + return $path; + } + + // No actual extension in path + if (empty($actualExtension)) { + return $path.('.' === mb_substr($path, -1) ? '' : '.').$extension; + } + + return mb_substr($path, 0, -mb_strlen($actualExtension)).$extension; + } + + public static function isAbsolute(string $path): bool + { + if ('' === $path) { + return false; + } + + // Strip scheme + if (false !== ($schemeSeparatorPosition = mb_strpos($path, '://'))) { + $path = mb_substr($path, $schemeSeparatorPosition + 3); + } + + $firstCharacter = $path[0]; + + // UNIX root "/" or "\" (Windows style) + if ('/' === $firstCharacter || '\\' === $firstCharacter) { + return true; + } + + // Windows root + if (mb_strlen($path) > 1 && ctype_alpha($firstCharacter) && ':' === $path[1]) { + // Special case: "C:" + if (2 === mb_strlen($path)) { + return true; + } + + // Normal case: "C:/ or "C:\" + if ('/' === $path[2] || '\\' === $path[2]) { + return true; + } + } + + return false; + } + + public static function isRelative(string $path): bool + { + return !self::isAbsolute($path); + } + + /** + * Turns a relative path into an absolute path in canonical form. + * + * Usually, the relative path is appended to the given base path. Dot + * segments ("." and "..") are removed/collapsed and all slashes turned + * into forward slashes. + * + * ```php + * echo Path::makeAbsolute("../style.css", "/symfony/puli/css"); + * // => /symfony/puli/style.css + * ``` + * + * If an absolute path is passed, that path is returned unless its root + * directory is different than the one of the base path. In that case, an + * exception is thrown. + * + * ```php + * Path::makeAbsolute("/style.css", "/symfony/puli/css"); + * // => /style.css + * + * Path::makeAbsolute("C:/style.css", "C:/symfony/puli/css"); + * // => C:/style.css + * + * Path::makeAbsolute("C:/style.css", "/symfony/puli/css"); + * // InvalidArgumentException + * ``` + * + * If the base path is not an absolute path, an exception is thrown. + * + * The result is a canonical path. + * + * @param string $basePath an absolute base path + * + * @throws InvalidArgumentException if the base path is not absolute or if + * the given path is an absolute path with + * a different root than the base path + */ + public static function makeAbsolute(string $path, string $basePath): string + { + if ('' === $basePath) { + throw new InvalidArgumentException(sprintf('The base path must be a non-empty string. Got: "%s".', $basePath)); + } + + if (!self::isAbsolute($basePath)) { + throw new InvalidArgumentException(sprintf('The base path "%s" is not an absolute path.', $basePath)); + } + + if (self::isAbsolute($path)) { + return self::canonicalize($path); + } + + if (false !== ($schemeSeparatorPosition = mb_strpos($basePath, '://'))) { + $scheme = mb_substr($basePath, 0, $schemeSeparatorPosition + 3); + $basePath = mb_substr($basePath, $schemeSeparatorPosition + 3); + } else { + $scheme = ''; + } + + return $scheme.self::canonicalize(rtrim($basePath, '/\\').'/'.$path); + } + + /** + * Turns a path into a relative path. + * + * The relative path is created relative to the given base path: + * + * ```php + * echo Path::makeRelative("/symfony/style.css", "/symfony/puli"); + * // => ../style.css + * ``` + * + * If a relative path is passed and the base path is absolute, the relative + * path is returned unchanged: + * + * ```php + * Path::makeRelative("style.css", "/symfony/puli/css"); + * // => style.css + * ``` + * + * If both paths are relative, the relative path is created with the + * assumption that both paths are relative to the same directory: + * + * ```php + * Path::makeRelative("style.css", "symfony/puli/css"); + * // => ../../../style.css + * ``` + * + * If both paths are absolute, their root directory must be the same, + * otherwise an exception is thrown: + * + * ```php + * Path::makeRelative("C:/symfony/style.css", "/symfony/puli"); + * // InvalidArgumentException + * ``` + * + * If the passed path is absolute, but the base path is not, an exception + * is thrown as well: + * + * ```php + * Path::makeRelative("/symfony/style.css", "symfony/puli"); + * // InvalidArgumentException + * ``` + * + * If the base path is not an absolute path, an exception is thrown. + * + * The result is a canonical path. + * + * @throws InvalidArgumentException if the base path is not absolute or if + * the given path has a different root + * than the base path + */ + public static function makeRelative(string $path, string $basePath): string + { + $path = self::canonicalize($path); + $basePath = self::canonicalize($basePath); + + [$root, $relativePath] = self::split($path); + [$baseRoot, $relativeBasePath] = self::split($basePath); + + // If the base path is given as absolute path and the path is already + // relative, consider it to be relative to the given absolute path + // already + if ('' === $root && '' !== $baseRoot) { + // If base path is already in its root + if ('' === $relativeBasePath) { + $relativePath = ltrim($relativePath, './\\'); + } + + return $relativePath; + } + + // If the passed path is absolute, but the base path is not, we + // cannot generate a relative path + if ('' !== $root && '' === $baseRoot) { + throw new InvalidArgumentException(sprintf('The absolute path "%s" cannot be made relative to the relative path "%s". You should provide an absolute base path instead.', $path, $basePath)); + } + + // Fail if the roots of the two paths are different + if ($baseRoot && $root !== $baseRoot) { + throw new InvalidArgumentException(sprintf('The path "%s" cannot be made relative to "%s", because they have different roots ("%s" and "%s").', $path, $basePath, $root, $baseRoot)); + } + + if ('' === $relativeBasePath) { + return $relativePath; + } + + // Build a "../../" prefix with as many "../" parts as necessary + $parts = explode('/', $relativePath); + $baseParts = explode('/', $relativeBasePath); + $dotDotPrefix = ''; + + // Once we found a non-matching part in the prefix, we need to add + // "../" parts for all remaining parts + $match = true; + + foreach ($baseParts as $index => $basePart) { + if ($match && isset($parts[$index]) && $basePart === $parts[$index]) { + unset($parts[$index]); + + continue; + } + + $match = false; + $dotDotPrefix .= '../'; + } + + return rtrim($dotDotPrefix.implode('/', $parts), '/'); + } + + /** + * Returns whether the given path is on the local filesystem. + */ + public static function isLocal(string $path): bool + { + return '' !== $path && false === mb_strpos($path, '://'); + } + + /** + * Returns the longest common base path in canonical form of a set of paths or + * `null` if the paths are on different Windows partitions. + * + * Dot segments ("." and "..") are removed/collapsed and all slashes turned + * into forward slashes. + * + * ```php + * $basePath = Path::getLongestCommonBasePath([ + * '/symfony/css/style.css', + * '/symfony/css/..' + * ]); + * // => /symfony + * ``` + * + * The root is returned if no common base path can be found: + * + * ```php + * $basePath = Path::getLongestCommonBasePath([ + * '/symfony/css/style.css', + * '/puli/css/..' + * ]); + * // => / + * ``` + * + * If the paths are located on different Windows partitions, `null` is + * returned. + * + * ```php + * $basePath = Path::getLongestCommonBasePath([ + * 'C:/symfony/css/style.css', + * 'D:/symfony/css/..' + * ]); + * // => null + * ``` + */ + public static function getLongestCommonBasePath(string ...$paths): ?string + { + [$bpRoot, $basePath] = self::split(self::canonicalize(reset($paths))); + + for (next($paths); null !== key($paths) && '' !== $basePath; next($paths)) { + [$root, $path] = self::split(self::canonicalize(current($paths))); + + // If we deal with different roots (e.g. C:/ vs. D:/), it's time + // to quit + if ($root !== $bpRoot) { + return null; + } + + // Make the base path shorter until it fits into path + while (true) { + if ('.' === $basePath) { + // No more base paths + $basePath = ''; + + // next path + continue 2; + } + + // Prevent false positives for common prefixes + // see isBasePath() + if (0 === mb_strpos($path.'/', $basePath.'/')) { + // next path + continue 2; + } + + $basePath = \dirname($basePath); + } + } + + return $bpRoot.$basePath; + } + + /** + * Joins two or more path strings into a canonical path. + */ + public static function join(string ...$paths): string + { + $finalPath = null; + $wasScheme = false; + + foreach ($paths as $path) { + if ('' === $path) { + continue; + } + + if (null === $finalPath) { + // For first part we keep slashes, like '/top', 'C:\' or 'phar://' + $finalPath = $path; + $wasScheme = (false !== mb_strpos($path, '://')); + continue; + } + + // Only add slash if previous part didn't end with '/' or '\' + if (!\in_array(mb_substr($finalPath, -1), ['/', '\\'])) { + $finalPath .= '/'; + } + + // If first part included a scheme like 'phar://' we allow \current part to start with '/', otherwise trim + $finalPath .= $wasScheme ? $path : ltrim($path, '/'); + $wasScheme = false; + } + + if (null === $finalPath) { + return ''; + } + + return self::canonicalize($finalPath); + } + + /** + * Returns whether a path is a base path of another path. + * + * Dot segments ("." and "..") are removed/collapsed and all slashes turned + * into forward slashes. + * + * ```php + * Path::isBasePath('/symfony', '/symfony/css'); + * // => true + * + * Path::isBasePath('/symfony', '/symfony'); + * // => true + * + * Path::isBasePath('/symfony', '/symfony/..'); + * // => false + * + * Path::isBasePath('/symfony', '/puli'); + * // => false + * ``` + */ + public static function isBasePath(string $basePath, string $ofPath): bool + { + $basePath = self::canonicalize($basePath); + $ofPath = self::canonicalize($ofPath); + + // Append slashes to prevent false positives when two paths have + // a common prefix, for example /base/foo and /base/foobar. + // Don't append a slash for the root "/", because then that root + // won't be discovered as common prefix ("//" is not a prefix of + // "/foobar/"). + return 0 === mb_strpos($ofPath.'/', rtrim($basePath, '/').'/'); + } + + /** + * @return non-empty-string[] + */ + private static function findCanonicalParts(string $root, string $pathWithoutRoot): array + { + $parts = explode('/', $pathWithoutRoot); + + $canonicalParts = []; + + // Collapse "." and "..", if possible + foreach ($parts as $part) { + if ('.' === $part || '' === $part) { + continue; + } + + // Collapse ".." with the previous part, if one exists + // Don't collapse ".." if the previous part is also ".." + if ('..' === $part && \count($canonicalParts) > 0 && '..' !== $canonicalParts[\count($canonicalParts) - 1]) { + array_pop($canonicalParts); + + continue; + } + + // Only add ".." prefixes for relative paths + if ('..' !== $part || '' === $root) { + $canonicalParts[] = $part; + } + } + + return $canonicalParts; + } + + /** + * Splits a canonical path into its root directory and the remainder. + * + * If the path has no root directory, an empty root directory will be + * returned. + * + * If the root directory is a Windows style partition, the resulting root + * will always contain a trailing slash. + * + * list ($root, $path) = Path::split("C:/symfony") + * // => ["C:/", "symfony"] + * + * list ($root, $path) = Path::split("C:") + * // => ["C:/", ""] + * + * @return array{string, string} an array with the root directory and the remaining relative path + */ + private static function split(string $path): array + { + if ('' === $path) { + return ['', '']; + } + + // Remember scheme as part of the root, if any + if (false !== ($schemeSeparatorPosition = mb_strpos($path, '://'))) { + $root = mb_substr($path, 0, $schemeSeparatorPosition + 3); + $path = mb_substr($path, $schemeSeparatorPosition + 3); + } else { + $root = ''; + } + + $length = mb_strlen($path); + + // Remove and remember root directory + if (0 === mb_strpos($path, '/')) { + $root .= '/'; + $path = $length > 1 ? mb_substr($path, 1) : ''; + } elseif ($length > 1 && ctype_alpha($path[0]) && ':' === $path[1]) { + if (2 === $length) { + // Windows special case: "C:" + $root .= $path.'/'; + $path = ''; + } elseif ('/' === $path[2]) { + // Windows normal case: "C:/".. + $root .= mb_substr($path, 0, 3); + $path = $length > 3 ? mb_substr($path, 3) : ''; + } + } + + return [$root, $path]; + } + + private static function toLower(string $string): string + { + if (false !== $encoding = mb_detect_encoding($string)) { + return mb_strtolower($string, $encoding); + } + + return strtolower($string, $encoding); + } + + private function __construct() + { + } +} diff --git a/src/Symfony/Component/Filesystem/Tests/PathTest.php b/src/Symfony/Component/Filesystem/Tests/PathTest.php new file mode 100644 index 0000000000000..006cdb346ca98 --- /dev/null +++ b/src/Symfony/Component/Filesystem/Tests/PathTest.php @@ -0,0 +1,1055 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Filesystem\Tests; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Filesystem\Path; + +/** + * @author Bernhard Schussek + * @author Thomas Schulz + * @author Théo Fidry + */ +class PathTest extends TestCase +{ + protected $storedEnv = []; + + protected function setUp(): void + { + $this->storedEnv['HOME'] = getenv('HOME'); + $this->storedEnv['HOMEDRIVE'] = getenv('HOMEDRIVE'); + $this->storedEnv['HOMEPATH'] = getenv('HOMEPATH'); + + putenv('HOME=/home/webmozart'); + putenv('HOMEDRIVE='); + putenv('HOMEPATH='); + } + + protected function tearDown(): void + { + putenv('HOME='.$this->storedEnv['HOME']); + putenv('HOMEDRIVE='.$this->storedEnv['HOMEDRIVE']); + putenv('HOMEPATH='.$this->storedEnv['HOMEPATH']); + } + + public function provideCanonicalizationTests(): \Generator + { + // relative paths (forward slash) + yield ['css/./style.css', 'css/style.css']; + yield ['css/../style.css', 'style.css']; + yield ['css/./../style.css', 'style.css']; + yield ['css/.././style.css', 'style.css']; + yield ['css/../../style.css', '../style.css']; + yield ['./css/style.css', 'css/style.css']; + yield ['../css/style.css', '../css/style.css']; + yield ['./../css/style.css', '../css/style.css']; + yield ['.././css/style.css', '../css/style.css']; + yield ['../../css/style.css', '../../css/style.css']; + yield ['', '']; + yield ['.', '']; + yield ['..', '..']; + yield ['./..', '..']; + yield ['../.', '..']; + yield ['../..', '../..']; + + // relative paths (backslash) + yield ['css\\.\\style.css', 'css/style.css']; + yield ['css\\..\\style.css', 'style.css']; + yield ['css\\.\\..\\style.css', 'style.css']; + yield ['css\\..\\.\\style.css', 'style.css']; + yield ['css\\..\\..\\style.css', '../style.css']; + yield ['.\\css\\style.css', 'css/style.css']; + yield ['..\\css\\style.css', '../css/style.css']; + yield ['.\\..\\css\\style.css', '../css/style.css']; + yield ['..\\.\\css\\style.css', '../css/style.css']; + yield ['..\\..\\css\\style.css', '../../css/style.css']; + + // absolute paths (forward slash, UNIX) + yield ['/css/style.css', '/css/style.css']; + yield ['/css/./style.css', '/css/style.css']; + yield ['/css/../style.css', '/style.css']; + yield ['/css/./../style.css', '/style.css']; + yield ['/css/.././style.css', '/style.css']; + yield ['/./css/style.css', '/css/style.css']; + yield ['/../css/style.css', '/css/style.css']; + yield ['/./../css/style.css', '/css/style.css']; + yield ['/.././css/style.css', '/css/style.css']; + yield ['/../../css/style.css', '/css/style.css']; + + // absolute paths (backslash, UNIX) + yield ['\\css\\style.css', '/css/style.css']; + yield ['\\css\\.\\style.css', '/css/style.css']; + yield ['\\css\\..\\style.css', '/style.css']; + yield ['\\css\\.\\..\\style.css', '/style.css']; + yield ['\\css\\..\\.\\style.css', '/style.css']; + yield ['\\.\\css\\style.css', '/css/style.css']; + yield ['\\..\\css\\style.css', '/css/style.css']; + yield ['\\.\\..\\css\\style.css', '/css/style.css']; + yield ['\\..\\.\\css\\style.css', '/css/style.css']; + yield ['\\..\\..\\css\\style.css', '/css/style.css']; + + // absolute paths (forward slash, Windows) + yield ['C:/css/style.css', 'C:/css/style.css']; + yield ['C:/css/./style.css', 'C:/css/style.css']; + yield ['C:/css/../style.css', 'C:/style.css']; + yield ['C:/css/./../style.css', 'C:/style.css']; + yield ['C:/css/.././style.css', 'C:/style.css']; + yield ['C:/./css/style.css', 'C:/css/style.css']; + yield ['C:/../css/style.css', 'C:/css/style.css']; + yield ['C:/./../css/style.css', 'C:/css/style.css']; + yield ['C:/.././css/style.css', 'C:/css/style.css']; + yield ['C:/../../css/style.css', 'C:/css/style.css']; + + // absolute paths (backslash, Windows) + yield ['C:\\css\\style.css', 'C:/css/style.css']; + yield ['C:\\css\\.\\style.css', 'C:/css/style.css']; + yield ['C:\\css\\..\\style.css', 'C:/style.css']; + yield ['C:\\css\\.\\..\\style.css', 'C:/style.css']; + yield ['C:\\css\\..\\.\\style.css', 'C:/style.css']; + yield ['C:\\.\\css\\style.css', 'C:/css/style.css']; + yield ['C:\\..\\css\\style.css', 'C:/css/style.css']; + yield ['C:\\.\\..\\css\\style.css', 'C:/css/style.css']; + yield ['C:\\..\\.\\css\\style.css', 'C:/css/style.css']; + yield ['C:\\..\\..\\css\\style.css', 'C:/css/style.css']; + + // Windows special case + yield ['C:', 'C:/']; + + // Don't change malformed path + yield ['C:css/style.css', 'C:css/style.css']; + + // absolute paths (stream, UNIX) + yield ['phar:///css/style.css', 'phar:///css/style.css']; + yield ['phar:///css/./style.css', 'phar:///css/style.css']; + yield ['phar:///css/../style.css', 'phar:///style.css']; + yield ['phar:///css/./../style.css', 'phar:///style.css']; + yield ['phar:///css/.././style.css', 'phar:///style.css']; + yield ['phar:///./css/style.css', 'phar:///css/style.css']; + yield ['phar:///../css/style.css', 'phar:///css/style.css']; + yield ['phar:///./../css/style.css', 'phar:///css/style.css']; + yield ['phar:///.././css/style.css', 'phar:///css/style.css']; + yield ['phar:///../../css/style.css', 'phar:///css/style.css']; + + // absolute paths (stream, Windows) + yield ['phar://C:/css/style.css', 'phar://C:/css/style.css']; + yield ['phar://C:/css/./style.css', 'phar://C:/css/style.css']; + yield ['phar://C:/css/../style.css', 'phar://C:/style.css']; + yield ['phar://C:/css/./../style.css', 'phar://C:/style.css']; + yield ['phar://C:/css/.././style.css', 'phar://C:/style.css']; + yield ['phar://C:/./css/style.css', 'phar://C:/css/style.css']; + yield ['phar://C:/../css/style.css', 'phar://C:/css/style.css']; + yield ['phar://C:/./../css/style.css', 'phar://C:/css/style.css']; + yield ['phar://C:/.././css/style.css', 'phar://C:/css/style.css']; + yield ['phar://C:/../../css/style.css', 'phar://C:/css/style.css']; + + // paths with "~" UNIX + yield ['~/css/style.css', '/home/webmozart/css/style.css']; + yield ['~/css/./style.css', '/home/webmozart/css/style.css']; + yield ['~/css/../style.css', '/home/webmozart/style.css']; + yield ['~/css/./../style.css', '/home/webmozart/style.css']; + yield ['~/css/.././style.css', '/home/webmozart/style.css']; + yield ['~/./css/style.css', '/home/webmozart/css/style.css']; + yield ['~/../css/style.css', '/home/css/style.css']; + yield ['~/./../css/style.css', '/home/css/style.css']; + yield ['~/.././css/style.css', '/home/css/style.css']; + yield ['~/../../css/style.css', '/css/style.css']; + } + + /** + * @dataProvider provideCanonicalizationTests + */ + public function testCanonicalize(string $path, string $canonicalized) + { + $this->assertSame($canonicalized, Path::canonicalize($path)); + } + + public function provideGetDirectoryTests(): \Generator + { + yield ['/webmozart/symfony/style.css', '/webmozart/symfony']; + yield ['/webmozart/symfony', '/webmozart']; + yield ['/webmozart', '/']; + yield ['/', '/']; + yield ['', '']; + + yield ['\\webmozart\\symfony\\style.css', '/webmozart/symfony']; + yield ['\\webmozart\\symfony', '/webmozart']; + yield ['\\webmozart', '/']; + yield ['\\', '/']; + + yield ['C:/webmozart/symfony/style.css', 'C:/webmozart/symfony']; + yield ['C:/webmozart/symfony', 'C:/webmozart']; + yield ['C:/webmozart', 'C:/']; + yield ['C:/', 'C:/']; + yield ['C:', 'C:/']; + + yield ['C:\\webmozart\\symfony\\style.css', 'C:/webmozart/symfony']; + yield ['C:\\webmozart\\symfony', 'C:/webmozart']; + yield ['C:\\webmozart', 'C:/']; + yield ['C:\\', 'C:/']; + + yield ['phar:///webmozart/symfony/style.css', 'phar:///webmozart/symfony']; + yield ['phar:///webmozart/symfony', 'phar:///webmozart']; + yield ['phar:///webmozart', 'phar:///']; + yield ['phar:///', 'phar:///']; + + yield ['phar://C:/webmozart/symfony/style.css', 'phar://C:/webmozart/symfony']; + yield ['phar://C:/webmozart/symfony', 'phar://C:/webmozart']; + yield ['phar://C:/webmozart', 'phar://C:/']; + yield ['phar://C:/', 'phar://C:/']; + + yield ['webmozart/symfony/style.css', 'webmozart/symfony']; + yield ['webmozart/symfony', 'webmozart']; + yield ['webmozart', '']; + + yield ['webmozart\\symfony\\style.css', 'webmozart/symfony']; + yield ['webmozart\\symfony', 'webmozart']; + yield ['webmozart', '']; + + yield ['/webmozart/./symfony/style.css', '/webmozart/symfony']; + yield ['/webmozart/../symfony/style.css', '/symfony']; + yield ['/webmozart/./../symfony/style.css', '/symfony']; + yield ['/webmozart/.././symfony/style.css', '/symfony']; + yield ['/webmozart/../../symfony/style.css', '/symfony']; + yield ['/.', '/']; + yield ['/..', '/']; + + yield ['C:webmozart', '']; + } + + /** + * @dataProvider provideGetDirectoryTests + */ + public function testGetDirectory(string $path, string $directory) + { + $this->assertSame($directory, Path::getDirectory($path)); + } + + public function provideGetFilenameWithoutExtensionTests(): \Generator + { + yield ['/webmozart/symfony/style.css.twig', null, 'style.css']; + yield ['/webmozart/symfony/style.css.', null, 'style.css']; + yield ['/webmozart/symfony/style.css', null, 'style']; + yield ['/webmozart/symfony/.style.css', null, '.style']; + yield ['/webmozart/symfony/', null, 'symfony']; + yield ['/webmozart/symfony', null, 'symfony']; + yield ['/', null, '']; + yield ['', null, '']; + + yield ['/webmozart/symfony/style.css', 'css', 'style']; + yield ['/webmozart/symfony/style.css', '.css', 'style']; + yield ['/webmozart/symfony/style.css', 'twig', 'style.css']; + yield ['/webmozart/symfony/style.css', '.twig', 'style.css']; + yield ['/webmozart/symfony/style.css', '', 'style.css']; + yield ['/webmozart/symfony/style.css.', '', 'style.css']; + yield ['/webmozart/symfony/style.css.', '.', 'style.css']; + yield ['/webmozart/symfony/style.css.', '.css', 'style.css']; + yield ['/webmozart/symfony/.style.css', 'css', '.style']; + yield ['/webmozart/symfony/.style.css', '.css', '.style']; + } + + /** + * @dataProvider provideGetFilenameWithoutExtensionTests + */ + public function testGetFilenameWithoutExtension(string $path, ?string $extension, string $filename) + { + $this->assertSame($filename, Path::getFilenameWithoutExtension($path, $extension)); + } + + public function provideGetExtensionTests(): \Generator + { + yield ['/webmozart/symfony/style.css.twig', false, 'twig']; + yield ['/webmozart/symfony/style.css', false, 'css']; + yield ['/webmozart/symfony/style.css.', false, '']; + yield ['/webmozart/symfony/', false, '']; + yield ['/webmozart/symfony', false, '']; + yield ['/', false, '']; + yield ['', false, '']; + + yield ['/webmozart/symfony/style.CSS', false, 'CSS']; + yield ['/webmozart/symfony/style.CSS', true, 'css']; + yield ['/webmozart/symfony/style.ÄÖÜ', false, 'ÄÖÜ']; + + yield ['/webmozart/symfony/style.ÄÖÜ', true, 'äöü']; + } + + /** + * @dataProvider provideGetExtensionTests + */ + public function testGetExtension(string $path, bool $forceLowerCase, string $extension) + { + $this->assertSame($extension, Path::getExtension($path, $forceLowerCase)); + } + + public function provideHasExtensionTests(): \Generator + { + yield [true, '/webmozart/symfony/style.css.twig', null, false]; + yield [true, '/webmozart/symfony/style.css', null, false]; + yield [false, '/webmozart/symfony/style.css.', null, false]; + yield [false, '/webmozart/symfony/', null, false]; + yield [false, '/webmozart/symfony', null, false]; + yield [false, '/', null, false]; + yield [false, '', null, false]; + + yield [true, '/webmozart/symfony/style.css.twig', 'twig', false]; + yield [false, '/webmozart/symfony/style.css.twig', 'css', false]; + yield [true, '/webmozart/symfony/style.css', 'css', false]; + yield [true, '/webmozart/symfony/style.css', '.css', false]; + yield [true, '/webmozart/symfony/style.css.', '', false]; + yield [false, '/webmozart/symfony/', 'ext', false]; + yield [false, '/webmozart/symfony', 'ext', false]; + yield [false, '/', 'ext', false]; + yield [false, '', 'ext', false]; + + yield [false, '/webmozart/symfony/style.css', 'CSS', false]; + yield [true, '/webmozart/symfony/style.css', 'CSS', true]; + yield [false, '/webmozart/symfony/style.CSS', 'css', false]; + yield [true, '/webmozart/symfony/style.CSS', 'css', true]; + yield [true, '/webmozart/symfony/style.ÄÖÜ', 'ÄÖÜ', false]; + + yield [true, '/webmozart/symfony/style.css', ['ext', 'css'], false]; + yield [true, '/webmozart/symfony/style.css', ['.ext', '.css'], false]; + yield [true, '/webmozart/symfony/style.css.', ['ext', ''], false]; + yield [false, '/webmozart/symfony/style.css', ['foo', 'bar', ''], false]; + yield [false, '/webmozart/symfony/style.css', ['.foo', '.bar', ''], false]; + + // This can only be tested, when mbstring is installed + yield [true, '/webmozart/symfony/style.ÄÖÜ', 'äöü', true]; + yield [true, '/webmozart/symfony/style.ÄÖÜ', ['äöü'], true]; + } + + /** + * @dataProvider provideHasExtensionTests + * + * @param string|string[]|null $extension + */ + public function testHasExtension(bool $hasExtension, string $path, $extension, bool $ignoreCase) + { + $this->assertSame($hasExtension, Path::hasExtension($path, $extension, $ignoreCase)); + } + + public function provideChangeExtensionTests(): \Generator + { + yield ['/webmozart/symfony/style.css.twig', 'html', '/webmozart/symfony/style.css.html']; + yield ['/webmozart/symfony/style.css', 'sass', '/webmozart/symfony/style.sass']; + yield ['/webmozart/symfony/style.css', '.sass', '/webmozart/symfony/style.sass']; + yield ['/webmozart/symfony/style.css', '', '/webmozart/symfony/style.']; + yield ['/webmozart/symfony/style.css.', 'twig', '/webmozart/symfony/style.css.twig']; + yield ['/webmozart/symfony/style.css.', '', '/webmozart/symfony/style.css.']; + yield ['/webmozart/symfony/style.css', 'äöü', '/webmozart/symfony/style.äöü']; + yield ['/webmozart/symfony/style.äöü', 'css', '/webmozart/symfony/style.css']; + yield ['/webmozart/symfony/', 'css', '/webmozart/symfony/']; + yield ['/webmozart/symfony', 'css', '/webmozart/symfony.css']; + yield ['/', 'css', '/']; + yield ['', 'css', '']; + } + + /** + * @dataProvider provideChangeExtensionTests + */ + public function testChangeExtension(string $path, string $extension, string $pathExpected) + { + $this->assertSame($pathExpected, Path::changeExtension($path, $extension)); + } + + public function provideIsAbsolutePathTests(): \Generator + { + yield ['/css/style.css', true]; + yield ['/', true]; + yield ['css/style.css', false]; + yield ['', false]; + + yield ['\\css\\style.css', true]; + yield ['\\', true]; + yield ['css\\style.css', false]; + + yield ['C:/css/style.css', true]; + yield ['D:/', true]; + + yield ['E:\\css\\style.css', true]; + yield ['F:\\', true]; + + yield ['phar:///css/style.css', true]; + yield ['phar:///', true]; + + // Windows special case + yield ['C:', true]; + + // Not considered absolute + yield ['C:css/style.css', false]; + } + + /** + * @dataProvider provideIsAbsolutePathTests + */ + public function testIsAbsolute(string $path, bool $isAbsolute) + { + $this->assertSame($isAbsolute, Path::isAbsolute($path)); + } + + /** + * @dataProvider provideIsAbsolutePathTests + */ + public function testIsRelative(string $path, bool $isAbsolute) + { + $this->assertSame(!$isAbsolute, Path::isRelative($path)); + } + + public function provideGetRootTests(): \Generator + { + yield ['/css/style.css', '/']; + yield ['/', '/']; + yield ['css/style.css', '']; + yield ['', '']; + + yield ['\\css\\style.css', '/']; + yield ['\\', '/']; + yield ['css\\style.css', '']; + + yield ['C:/css/style.css', 'C:/']; + yield ['C:/', 'C:/']; + yield ['C:', 'C:/']; + + yield ['D:\\css\\style.css', 'D:/']; + yield ['D:\\', 'D:/']; + + yield ['phar:///css/style.css', 'phar:///']; + yield ['phar:///', 'phar:///']; + + yield ['phar://C:/css/style.css', 'phar://C:/']; + yield ['phar://C:/', 'phar://C:/']; + yield ['phar://C:', 'phar://C:/']; + } + + /** + * @dataProvider provideGetRootTests + */ + public function testGetRoot(string $path, string $root) + { + $this->assertSame($root, Path::getRoot($path)); + } + + public function providePathTests(): \Generator + { + // relative to absolute path + yield ['css/style.css', '/webmozart/symfony', '/webmozart/symfony/css/style.css']; + yield ['../css/style.css', '/webmozart/symfony', '/webmozart/css/style.css']; + yield ['../../css/style.css', '/webmozart/symfony', '/css/style.css']; + + // relative to root + yield ['css/style.css', '/', '/css/style.css']; + yield ['css/style.css', 'C:', 'C:/css/style.css']; + yield ['css/style.css', 'C:/', 'C:/css/style.css']; + + // same sub directories in different base directories + yield ['../../symfony/css/style.css', '/webmozart/css', '/symfony/css/style.css']; + + yield ['', '/webmozart/symfony', '/webmozart/symfony']; + yield ['..', '/webmozart/symfony', '/webmozart']; + } + + public function provideMakeAbsoluteTests(): \Generator + { + foreach ($this->providePathTests() as $set) { + yield $set; + } + + // collapse dots + yield ['css/./style.css', '/webmozart/symfony', '/webmozart/symfony/css/style.css']; + yield ['css/../style.css', '/webmozart/symfony', '/webmozart/symfony/style.css']; + yield ['css/./../style.css', '/webmozart/symfony', '/webmozart/symfony/style.css']; + yield ['css/.././style.css', '/webmozart/symfony', '/webmozart/symfony/style.css']; + yield ['./css/style.css', '/webmozart/symfony', '/webmozart/symfony/css/style.css']; + + yield ['css\\.\\style.css', '\\webmozart\\symfony', '/webmozart/symfony/css/style.css']; + yield ['css\\..\\style.css', '\\webmozart\\symfony', '/webmozart/symfony/style.css']; + yield ['css\\.\\..\\style.css', '\\webmozart\\symfony', '/webmozart/symfony/style.css']; + yield ['css\\..\\.\\style.css', '\\webmozart\\symfony', '/webmozart/symfony/style.css']; + yield ['.\\css\\style.css', '\\webmozart\\symfony', '/webmozart/symfony/css/style.css']; + + // collapse dots on root + yield ['./css/style.css', '/', '/css/style.css']; + yield ['../css/style.css', '/', '/css/style.css']; + yield ['../css/./style.css', '/', '/css/style.css']; + yield ['../css/../style.css', '/', '/style.css']; + yield ['../css/./../style.css', '/', '/style.css']; + yield ['../css/.././style.css', '/', '/style.css']; + + yield ['.\\css\\style.css', '\\', '/css/style.css']; + yield ['..\\css\\style.css', '\\', '/css/style.css']; + yield ['..\\css\\.\\style.css', '\\', '/css/style.css']; + yield ['..\\css\\..\\style.css', '\\', '/style.css']; + yield ['..\\css\\.\\..\\style.css', '\\', '/style.css']; + yield ['..\\css\\..\\.\\style.css', '\\', '/style.css']; + + yield ['./css/style.css', 'C:/', 'C:/css/style.css']; + yield ['../css/style.css', 'C:/', 'C:/css/style.css']; + yield ['../css/./style.css', 'C:/', 'C:/css/style.css']; + yield ['../css/../style.css', 'C:/', 'C:/style.css']; + yield ['../css/./../style.css', 'C:/', 'C:/style.css']; + yield ['../css/.././style.css', 'C:/', 'C:/style.css']; + + yield ['.\\css\\style.css', 'C:\\', 'C:/css/style.css']; + yield ['..\\css\\style.css', 'C:\\', 'C:/css/style.css']; + yield ['..\\css\\.\\style.css', 'C:\\', 'C:/css/style.css']; + yield ['..\\css\\..\\style.css', 'C:\\', 'C:/style.css']; + yield ['..\\css\\.\\..\\style.css', 'C:\\', 'C:/style.css']; + yield ['..\\css\\..\\.\\style.css', 'C:\\', 'C:/style.css']; + + yield ['./css/style.css', 'phar:///', 'phar:///css/style.css']; + yield ['../css/style.css', 'phar:///', 'phar:///css/style.css']; + yield ['../css/./style.css', 'phar:///', 'phar:///css/style.css']; + yield ['../css/../style.css', 'phar:///', 'phar:///style.css']; + yield ['../css/./../style.css', 'phar:///', 'phar:///style.css']; + yield ['../css/.././style.css', 'phar:///', 'phar:///style.css']; + + yield ['./css/style.css', 'phar://C:/', 'phar://C:/css/style.css']; + yield ['../css/style.css', 'phar://C:/', 'phar://C:/css/style.css']; + yield ['../css/./style.css', 'phar://C:/', 'phar://C:/css/style.css']; + yield ['../css/../style.css', 'phar://C:/', 'phar://C:/style.css']; + yield ['../css/./../style.css', 'phar://C:/', 'phar://C:/style.css']; + yield ['../css/.././style.css', 'phar://C:/', 'phar://C:/style.css']; + + // absolute paths + yield ['/css/style.css', '/webmozart/symfony', '/css/style.css']; + yield ['\\css\\style.css', '/webmozart/symfony', '/css/style.css']; + yield ['C:/css/style.css', 'C:/webmozart/symfony', 'C:/css/style.css']; + yield ['D:\\css\\style.css', 'D:/webmozart/symfony', 'D:/css/style.css']; + } + + /** + * @dataProvider provideMakeAbsoluteTests + */ + public function testMakeAbsolute(string $relativePath, string $basePath, string $absolutePath) + { + $this->assertSame($absolutePath, Path::makeAbsolute($relativePath, $basePath)); + } + + public function testMakeAbsoluteFailsIfBasePathNotAbsolute() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The base path "webmozart/symfony" is not an absolute path.'); + + Path::makeAbsolute('css/style.css', 'webmozart/symfony'); + } + + public function testMakeAbsoluteFailsIfBasePathEmpty() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The base path must be a non-empty string. Got: ""'); + + Path::makeAbsolute('css/style.css', ''); + } + + public function provideAbsolutePathsWithDifferentRoots(): \Generator + { + yield ['C:/css/style.css', '/webmozart/symfony']; + yield ['C:/css/style.css', '\\webmozart\\symfony']; + yield ['C:\\css\\style.css', '/webmozart/symfony']; + yield ['C:\\css\\style.css', '\\webmozart\\symfony']; + + yield ['/css/style.css', 'C:/webmozart/symfony']; + yield ['/css/style.css', 'C:\\webmozart\\symfony']; + yield ['\\css\\style.css', 'C:/webmozart/symfony']; + yield ['\\css\\style.css', 'C:\\webmozart\\symfony']; + + yield ['D:/css/style.css', 'C:/webmozart/symfony']; + yield ['D:/css/style.css', 'C:\\webmozart\\symfony']; + yield ['D:\\css\\style.css', 'C:/webmozart/symfony']; + yield ['D:\\css\\style.css', 'C:\\webmozart\\symfony']; + + yield ['phar:///css/style.css', '/webmozart/symfony']; + yield ['/css/style.css', 'phar:///webmozart/symfony']; + + yield ['phar://C:/css/style.css', 'C:/webmozart/symfony']; + yield ['phar://C:/css/style.css', 'C:\\webmozart\\symfony']; + yield ['phar://C:\\css\\style.css', 'C:/webmozart/symfony']; + yield ['phar://C:\\css\\style.css', 'C:\\webmozart\\symfony']; + } + + /** + * @dataProvider provideAbsolutePathsWithDifferentRoots + */ + public function testMakeAbsoluteDoesNotFailIfDifferentRoot(string $basePath, string $absolutePath) + { + // If a path in partition D: is passed, but $basePath is in partition + // C:, the path should be returned unchanged + $this->assertSame(Path::canonicalize($absolutePath), Path::makeAbsolute($absolutePath, $basePath)); + } + + public function provideMakeRelativeTests(): \Generator + { + foreach ($this->providePathTests() as $set) { + yield [$set[2], $set[1], $set[0]]; + } + + yield ['/webmozart/symfony/./css/style.css', '/webmozart/symfony', 'css/style.css']; + yield ['/webmozart/symfony/../css/style.css', '/webmozart/symfony', '../css/style.css']; + yield ['/webmozart/symfony/.././css/style.css', '/webmozart/symfony', '../css/style.css']; + yield ['/webmozart/symfony/./../css/style.css', '/webmozart/symfony', '../css/style.css']; + yield ['/webmozart/symfony/../../css/style.css', '/webmozart/symfony', '../../css/style.css']; + yield ['/webmozart/symfony/css/style.css', '/webmozart/./symfony', 'css/style.css']; + yield ['/webmozart/symfony/css/style.css', '/webmozart/../symfony', '../webmozart/symfony/css/style.css']; + yield ['/webmozart/symfony/css/style.css', '/webmozart/./../symfony', '../webmozart/symfony/css/style.css']; + yield ['/webmozart/symfony/css/style.css', '/webmozart/.././symfony', '../webmozart/symfony/css/style.css']; + yield ['/webmozart/symfony/css/style.css', '/webmozart/../../symfony', '../webmozart/symfony/css/style.css']; + + // first argument shorter than second + yield ['/css', '/webmozart/symfony', '../../css']; + + // second argument shorter than first + yield ['/webmozart/symfony', '/css', '../webmozart/symfony']; + + yield ['\\webmozart\\symfony\\css\\style.css', '\\webmozart\\symfony', 'css/style.css']; + yield ['\\webmozart\\css\\style.css', '\\webmozart\\symfony', '../css/style.css']; + yield ['\\css\\style.css', '\\webmozart\\symfony', '../../css/style.css']; + + yield ['C:/webmozart/symfony/css/style.css', 'C:/webmozart/symfony', 'css/style.css']; + yield ['C:/webmozart/css/style.css', 'C:/webmozart/symfony', '../css/style.css']; + yield ['C:/css/style.css', 'C:/webmozart/symfony', '../../css/style.css']; + + yield ['C:\\webmozart\\symfony\\css\\style.css', 'C:\\webmozart\\symfony', 'css/style.css']; + yield ['C:\\webmozart\\css\\style.css', 'C:\\webmozart\\symfony', '../css/style.css']; + yield ['C:\\css\\style.css', 'C:\\webmozart\\symfony', '../../css/style.css']; + + yield ['phar:///webmozart/symfony/css/style.css', 'phar:///webmozart/symfony', 'css/style.css']; + yield ['phar:///webmozart/css/style.css', 'phar:///webmozart/symfony', '../css/style.css']; + yield ['phar:///css/style.css', 'phar:///webmozart/symfony', '../../css/style.css']; + + yield ['phar://C:/webmozart/symfony/css/style.css', 'phar://C:/webmozart/symfony', 'css/style.css']; + yield ['phar://C:/webmozart/css/style.css', 'phar://C:/webmozart/symfony', '../css/style.css']; + yield ['phar://C:/css/style.css', 'phar://C:/webmozart/symfony', '../../css/style.css']; + + // already relative + already in root basepath + yield ['../style.css', '/', 'style.css']; + yield ['./style.css', '/', 'style.css']; + yield ['../../style.css', '/', 'style.css']; + yield ['..\\style.css', 'C:\\', 'style.css']; + yield ['.\\style.css', 'C:\\', 'style.css']; + yield ['..\\..\\style.css', 'C:\\', 'style.css']; + yield ['../style.css', 'C:/', 'style.css']; + yield ['./style.css', 'C:/', 'style.css']; + yield ['../../style.css', 'C:/', 'style.css']; + yield ['..\\style.css', '\\', 'style.css']; + yield ['.\\style.css', '\\', 'style.css']; + yield ['..\\..\\style.css', '\\', 'style.css']; + yield ['../style.css', 'phar:///', 'style.css']; + yield ['./style.css', 'phar:///', 'style.css']; + yield ['../../style.css', 'phar:///', 'style.css']; + yield ['..\\style.css', 'phar://C:\\', 'style.css']; + yield ['.\\style.css', 'phar://C:\\', 'style.css']; + yield ['..\\..\\style.css', 'phar://C:\\', 'style.css']; + + yield ['css/../style.css', '/', 'style.css']; + yield ['css/./style.css', '/', 'css/style.css']; + yield ['css\\..\\style.css', 'C:\\', 'style.css']; + yield ['css\\.\\style.css', 'C:\\', 'css/style.css']; + yield ['css/../style.css', 'C:/', 'style.css']; + yield ['css/./style.css', 'C:/', 'css/style.css']; + yield ['css\\..\\style.css', '\\', 'style.css']; + yield ['css\\.\\style.css', '\\', 'css/style.css']; + yield ['css/../style.css', 'phar:///', 'style.css']; + yield ['css/./style.css', 'phar:///', 'css/style.css']; + yield ['css\\..\\style.css', 'phar://C:\\', 'style.css']; + yield ['css\\.\\style.css', 'phar://C:\\', 'css/style.css']; + + // already relative + yield ['css/style.css', '/webmozart/symfony', 'css/style.css']; + yield ['css\\style.css', '\\webmozart\\symfony', 'css/style.css']; + + // both relative + yield ['css/style.css', 'webmozart/symfony', '../../css/style.css']; + yield ['css\\style.css', 'webmozart\\symfony', '../../css/style.css']; + + // relative to empty + yield ['css/style.css', '', 'css/style.css']; + yield ['css\\style.css', '', 'css/style.css']; + + // different slashes in path and base path + yield ['/webmozart/symfony/css/style.css', '\\webmozart\\symfony', 'css/style.css']; + yield ['\\webmozart\\symfony\\css\\style.css', '/webmozart/symfony', 'css/style.css']; + } + + /** + * @dataProvider provideMakeRelativeTests + */ + public function testMakeRelative(string $absolutePath, string $basePath, string $relativePath) + { + $this->assertSame($relativePath, Path::makeRelative($absolutePath, $basePath)); + } + + public function testMakeRelativeFailsIfAbsolutePathAndBasePathNotAbsolute() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('The absolute path "/webmozart/symfony/css/style.css" cannot be made relative to the relative path "webmozart/symfony". You should provide an absolute base path instead.'); + + Path::makeRelative('/webmozart/symfony/css/style.css', 'webmozart/symfony'); + } + + public function testMakeRelativeFailsIfAbsolutePathAndBasePathEmpty() + { + $this->expectExceptionMessage('The absolute path "/webmozart/symfony/css/style.css" cannot be made relative to the relative path "". You should provide an absolute base path instead.'); + + Path::makeRelative('/webmozart/symfony/css/style.css', ''); + } + + /** + * @dataProvider provideAbsolutePathsWithDifferentRoots + */ + public function testMakeRelativeFailsIfDifferentRoot(string $absolutePath, string $basePath) + { + $this->expectException(\InvalidArgumentException::class); + + Path::makeRelative($absolutePath, $basePath); + } + + public function provideIsLocalTests(): \Generator + { + yield ['/bg.png', true]; + yield ['bg.png', true]; + yield ['http://example.com/bg.png', false]; + yield ['http://example.com', false]; + yield ['', false]; + } + + /** + * @dataProvider provideIsLocalTests + */ + public function testIsLocal(string $path, bool $isLocal) + { + $this->assertSame($isLocal, Path::isLocal($path)); + } + + public function provideGetLongestCommonBasePathTests(): \Generator + { + // same paths + yield [['/base/path', '/base/path'], '/base/path']; + yield [['C:/base/path', 'C:/base/path'], 'C:/base/path']; + yield [['C:\\base\\path', 'C:\\base\\path'], 'C:/base/path']; + yield [['C:/base/path', 'C:\\base\\path'], 'C:/base/path']; + yield [['phar:///base/path', 'phar:///base/path'], 'phar:///base/path']; + yield [['phar://C:/base/path', 'phar://C:/base/path'], 'phar://C:/base/path']; + + // trailing slash + yield [['/base/path/', '/base/path'], '/base/path']; + yield [['C:/base/path/', 'C:/base/path'], 'C:/base/path']; + yield [['C:\\base\\path\\', 'C:\\base\\path'], 'C:/base/path']; + yield [['C:/base/path/', 'C:\\base\\path'], 'C:/base/path']; + yield [['phar:///base/path/', 'phar:///base/path'], 'phar:///base/path']; + yield [['phar://C:/base/path/', 'phar://C:/base/path'], 'phar://C:/base/path']; + + yield [['/base/path', '/base/path/'], '/base/path']; + yield [['C:/base/path', 'C:/base/path/'], 'C:/base/path']; + yield [['C:\\base\\path', 'C:\\base\\path\\'], 'C:/base/path']; + yield [['C:/base/path', 'C:\\base\\path\\'], 'C:/base/path']; + yield [['phar:///base/path', 'phar:///base/path/'], 'phar:///base/path']; + yield [['phar://C:/base/path', 'phar://C:/base/path/'], 'phar://C:/base/path']; + + // first in second + yield [['/base/path/sub', '/base/path'], '/base/path']; + yield [['C:/base/path/sub', 'C:/base/path'], 'C:/base/path']; + yield [['C:\\base\\path\\sub', 'C:\\base\\path'], 'C:/base/path']; + yield [['C:/base/path/sub', 'C:\\base\\path'], 'C:/base/path']; + yield [['phar:///base/path/sub', 'phar:///base/path'], 'phar:///base/path']; + yield [['phar://C:/base/path/sub', 'phar://C:/base/path'], 'phar://C:/base/path']; + + // second in first + yield [['/base/path', '/base/path/sub'], '/base/path']; + yield [['C:/base/path', 'C:/base/path/sub'], 'C:/base/path']; + yield [['C:\\base\\path', 'C:\\base\\path\\sub'], 'C:/base/path']; + yield [['C:/base/path', 'C:\\base\\path\\sub'], 'C:/base/path']; + yield [['phar:///base/path', 'phar:///base/path/sub'], 'phar:///base/path']; + yield [['phar://C:/base/path', 'phar://C:/base/path/sub'], 'phar://C:/base/path']; + + // first is prefix + yield [['/base/path/di', '/base/path/dir'], '/base/path']; + yield [['C:/base/path/di', 'C:/base/path/dir'], 'C:/base/path']; + yield [['C:\\base\\path\\di', 'C:\\base\\path\\dir'], 'C:/base/path']; + yield [['C:/base/path/di', 'C:\\base\\path\\dir'], 'C:/base/path']; + yield [['phar:///base/path/di', 'phar:///base/path/dir'], 'phar:///base/path']; + yield [['phar://C:/base/path/di', 'phar://C:/base/path/dir'], 'phar://C:/base/path']; + + // second is prefix + yield [['/base/path/dir', '/base/path/di'], '/base/path']; + yield [['C:/base/path/dir', 'C:/base/path/di'], 'C:/base/path']; + yield [['C:\\base\\path\\dir', 'C:\\base\\path\\di'], 'C:/base/path']; + yield [['C:/base/path/dir', 'C:\\base\\path\\di'], 'C:/base/path']; + yield [['phar:///base/path/dir', 'phar:///base/path/di'], 'phar:///base/path']; + yield [['phar://C:/base/path/dir', 'phar://C:/base/path/di'], 'phar://C:/base/path']; + + // root is common base path + yield [['/first', '/second'], '/']; + yield [['C:/first', 'C:/second'], 'C:/']; + yield [['C:\\first', 'C:\\second'], 'C:/']; + yield [['C:/first', 'C:\\second'], 'C:/']; + yield [['phar:///first', 'phar:///second'], 'phar:///']; + yield [['phar://C:/first', 'phar://C:/second'], 'phar://C:/']; + + // windows vs unix + yield [['/base/path', 'C:/base/path'], null]; + yield [['C:/base/path', '/base/path'], null]; + yield [['/base/path', 'C:\\base\\path'], null]; + yield [['phar:///base/path', 'phar://C:/base/path'], null]; + + // different partitions + yield [['C:/base/path', 'D:/base/path'], null]; + yield [['C:/base/path', 'D:\\base\\path'], null]; + yield [['C:\\base\\path', 'D:\\base\\path'], null]; + yield [['phar://C:/base/path', 'phar://D:/base/path'], null]; + + // three paths + yield [['/base/path/foo', '/base/path', '/base/path/bar'], '/base/path']; + yield [['C:/base/path/foo', 'C:/base/path', 'C:/base/path/bar'], 'C:/base/path']; + yield [['C:\\base\\path\\foo', 'C:\\base\\path', 'C:\\base\\path\\bar'], 'C:/base/path']; + yield [['C:/base/path//foo', 'C:/base/path', 'C:\\base\\path\\bar'], 'C:/base/path']; + yield [['phar:///base/path/foo', 'phar:///base/path', 'phar:///base/path/bar'], 'phar:///base/path']; + yield [['phar://C:/base/path/foo', 'phar://C:/base/path', 'phar://C:/base/path/bar'], 'phar://C:/base/path']; + + // three paths with root + yield [['/base/path/foo', '/', '/base/path/bar'], '/']; + yield [['C:/base/path/foo', 'C:/', 'C:/base/path/bar'], 'C:/']; + yield [['C:\\base\\path\\foo', 'C:\\', 'C:\\base\\path\\bar'], 'C:/']; + yield [['C:/base/path//foo', 'C:/', 'C:\\base\\path\\bar'], 'C:/']; + yield [['phar:///base/path/foo', 'phar:///', 'phar:///base/path/bar'], 'phar:///']; + yield [['phar://C:/base/path/foo', 'phar://C:/', 'phar://C:/base/path/bar'], 'phar://C:/']; + + // three paths, different roots + yield [['/base/path/foo', 'C:/base/path', '/base/path/bar'], null]; + yield [['/base/path/foo', 'C:\\base\\path', '/base/path/bar'], null]; + yield [['C:/base/path/foo', 'D:/base/path', 'C:/base/path/bar'], null]; + yield [['C:\\base\\path\\foo', 'D:\\base\\path', 'C:\\base\\path\\bar'], null]; + yield [['C:/base/path//foo', 'D:/base/path', 'C:\\base\\path\\bar'], null]; + yield [['phar:///base/path/foo', 'phar://C:/base/path', 'phar:///base/path/bar'], null]; + yield [['phar://C:/base/path/foo', 'phar://D:/base/path', 'phar://C:/base/path/bar'], null]; + + // only one path + yield [['/base/path'], '/base/path']; + yield [['C:/base/path'], 'C:/base/path']; + yield [['C:\\base\\path'], 'C:/base/path']; + yield [['phar:///base/path'], 'phar:///base/path']; + yield [['phar://C:/base/path'], 'phar://C:/base/path']; + } + + /** + * @dataProvider provideGetLongestCommonBasePathTests + * + * @param string[] $paths + */ + public function testGetLongestCommonBasePath(array $paths, ?string $basePath) + { + $this->assertSame($basePath, Path::getLongestCommonBasePath(...$paths)); + } + + public function provideIsBasePathTests(): \Generator + { + // same paths + yield ['/base/path', '/base/path', true]; + yield ['C:/base/path', 'C:/base/path', true]; + yield ['C:\\base\\path', 'C:\\base\\path', true]; + yield ['C:/base/path', 'C:\\base\\path', true]; + yield ['phar:///base/path', 'phar:///base/path', true]; + yield ['phar://C:/base/path', 'phar://C:/base/path', true]; + + // trailing slash + yield ['/base/path/', '/base/path', true]; + yield ['C:/base/path/', 'C:/base/path', true]; + yield ['C:\\base\\path\\', 'C:\\base\\path', true]; + yield ['C:/base/path/', 'C:\\base\\path', true]; + yield ['phar:///base/path/', 'phar:///base/path', true]; + yield ['phar://C:/base/path/', 'phar://C:/base/path', true]; + + yield ['/base/path', '/base/path/', true]; + yield ['C:/base/path', 'C:/base/path/', true]; + yield ['C:\\base\\path', 'C:\\base\\path\\', true]; + yield ['C:/base/path', 'C:\\base\\path\\', true]; + yield ['phar:///base/path', 'phar:///base/path/', true]; + yield ['phar://C:/base/path', 'phar://C:/base/path/', true]; + + // first in second + yield ['/base/path/sub', '/base/path', false]; + yield ['C:/base/path/sub', 'C:/base/path', false]; + yield ['C:\\base\\path\\sub', 'C:\\base\\path', false]; + yield ['C:/base/path/sub', 'C:\\base\\path', false]; + yield ['phar:///base/path/sub', 'phar:///base/path', false]; + yield ['phar://C:/base/path/sub', 'phar://C:/base/path', false]; + + // second in first + yield ['/base/path', '/base/path/sub', true]; + yield ['C:/base/path', 'C:/base/path/sub', true]; + yield ['C:\\base\\path', 'C:\\base\\path\\sub', true]; + yield ['C:/base/path', 'C:\\base\\path\\sub', true]; + yield ['phar:///base/path', 'phar:///base/path/sub', true]; + yield ['phar://C:/base/path', 'phar://C:/base/path/sub', true]; + + // first is prefix + yield ['/base/path/di', '/base/path/dir', false]; + yield ['C:/base/path/di', 'C:/base/path/dir', false]; + yield ['C:\\base\\path\\di', 'C:\\base\\path\\dir', false]; + yield ['C:/base/path/di', 'C:\\base\\path\\dir', false]; + yield ['phar:///base/path/di', 'phar:///base/path/dir', false]; + yield ['phar://C:/base/path/di', 'phar://C:/base/path/dir', false]; + + // second is prefix + yield ['/base/path/dir', '/base/path/di', false]; + yield ['C:/base/path/dir', 'C:/base/path/di', false]; + yield ['C:\\base\\path\\dir', 'C:\\base\\path\\di', false]; + yield ['C:/base/path/dir', 'C:\\base\\path\\di', false]; + yield ['phar:///base/path/dir', 'phar:///base/path/di', false]; + yield ['phar://C:/base/path/dir', 'phar://C:/base/path/di', false]; + + // root + yield ['/', '/second', true]; + yield ['C:/', 'C:/second', true]; + yield ['C:', 'C:/second', true]; + yield ['C:\\', 'C:\\second', true]; + yield ['C:/', 'C:\\second', true]; + yield ['phar:///', 'phar:///second', true]; + yield ['phar://C:/', 'phar://C:/second', true]; + + // windows vs unix + yield ['/base/path', 'C:/base/path', false]; + yield ['C:/base/path', '/base/path', false]; + yield ['/base/path', 'C:\\base\\path', false]; + yield ['/base/path', 'phar:///base/path', false]; + yield ['phar:///base/path', 'phar://C:/base/path', false]; + + // different partitions + yield ['C:/base/path', 'D:/base/path', false]; + yield ['C:/base/path', 'D:\\base\\path', false]; + yield ['C:\\base\\path', 'D:\\base\\path', false]; + yield ['C:/base/path', 'phar://C:/base/path', false]; + yield ['phar://C:/base/path', 'phar://D:/base/path', false]; + } + + /** + * @dataProvider provideIsBasePathTests + */ + public function testIsBasePath(string $path, string $ofPath, bool $result) + { + $this->assertSame($result, Path::isBasePath($path, $ofPath)); + } + + public function provideJoinTests(): \Generator + { + yield [['', ''], '']; + yield [['/path/to/test', ''], '/path/to/test']; + yield [['/path/to//test', ''], '/path/to/test']; + yield [['', '/path/to/test'], '/path/to/test']; + yield [['', '/path/to//test'], '/path/to/test']; + + yield [['/path/to/test', 'subdir'], '/path/to/test/subdir']; + yield [['/path/to/test/', 'subdir'], '/path/to/test/subdir']; + yield [['/path/to/test', '/subdir'], '/path/to/test/subdir']; + yield [['/path/to/test/', '/subdir'], '/path/to/test/subdir']; + yield [['/path/to/test', './subdir'], '/path/to/test/subdir']; + yield [['/path/to/test/', './subdir'], '/path/to/test/subdir']; + yield [['/path/to/test/', '../parentdir'], '/path/to/parentdir']; + yield [['/path/to/test', '../parentdir'], '/path/to/parentdir']; + yield [['path/to/test/', '/subdir'], 'path/to/test/subdir']; + yield [['path/to/test', '/subdir'], 'path/to/test/subdir']; + yield [['../path/to/test', '/subdir'], '../path/to/test/subdir']; + yield [['path', '../../subdir'], '../subdir']; + yield [['/path', '../../subdir'], '/subdir']; + yield [['../path', '../../subdir'], '../../subdir']; + + yield [['/path/to/test', 'subdir', ''], '/path/to/test/subdir']; + yield [['/path/to/test', '/subdir', ''], '/path/to/test/subdir']; + yield [['/path/to/test/', 'subdir', ''], '/path/to/test/subdir']; + yield [['/path/to/test/', '/subdir', ''], '/path/to/test/subdir']; + + yield [['/path', ''], '/path']; + yield [['/path', 'to', '/test', ''], '/path/to/test']; + yield [['/path', '', '/test', ''], '/path/test']; + yield [['path', 'to', 'test', ''], 'path/to/test']; + yield [[], '']; + + yield [['base/path', 'to/test'], 'base/path/to/test']; + + yield [['C:\\path\\to\\test', 'subdir'], 'C:/path/to/test/subdir']; + yield [['C:\\path\\to\\test\\', 'subdir'], 'C:/path/to/test/subdir']; + yield [['C:\\path\\to\\test', '/subdir'], 'C:/path/to/test/subdir']; + yield [['C:\\path\\to\\test\\', '/subdir'], 'C:/path/to/test/subdir']; + + yield [['/', 'subdir'], '/subdir']; + yield [['/', '/subdir'], '/subdir']; + yield [['C:/', 'subdir'], 'C:/subdir']; + yield [['C:/', '/subdir'], 'C:/subdir']; + yield [['C:\\', 'subdir'], 'C:/subdir']; + yield [['C:\\', '/subdir'], 'C:/subdir']; + yield [['C:', 'subdir'], 'C:/subdir']; + yield [['C:', '/subdir'], 'C:/subdir']; + + yield [['phar://', '/path/to/test'], 'phar:///path/to/test']; + yield [['phar:///', '/path/to/test'], 'phar:///path/to/test']; + yield [['phar:///path/to/test', 'subdir'], 'phar:///path/to/test/subdir']; + yield [['phar:///path/to/test', 'subdir/'], 'phar:///path/to/test/subdir']; + yield [['phar:///path/to/test', '/subdir'], 'phar:///path/to/test/subdir']; + yield [['phar:///path/to/test/', 'subdir'], 'phar:///path/to/test/subdir']; + yield [['phar:///path/to/test/', '/subdir'], 'phar:///path/to/test/subdir']; + + yield [['phar://', 'C:/path/to/test'], 'phar://C:/path/to/test']; + yield [['phar://', 'C:\\path\\to\\test'], 'phar://C:/path/to/test']; + yield [['phar://C:/path/to/test', 'subdir'], 'phar://C:/path/to/test/subdir']; + yield [['phar://C:/path/to/test', 'subdir/'], 'phar://C:/path/to/test/subdir']; + yield [['phar://C:/path/to/test', '/subdir'], 'phar://C:/path/to/test/subdir']; + yield [['phar://C:/path/to/test/', 'subdir'], 'phar://C:/path/to/test/subdir']; + yield [['phar://C:/path/to/test/', '/subdir'], 'phar://C:/path/to/test/subdir']; + yield [['phar://C:', 'path/to/test'], 'phar://C:/path/to/test']; + yield [['phar://C:', '/path/to/test'], 'phar://C:/path/to/test']; + yield [['phar://C:/', 'path/to/test'], 'phar://C:/path/to/test']; + yield [['phar://C:/', '/path/to/test'], 'phar://C:/path/to/test']; + } + + /** + * @dataProvider provideJoinTests + */ + public function testJoin(array $paths, $result) + { + $this->assertSame($result, Path::join(...$paths)); + } + + public function testJoinVarArgs() + { + $this->assertSame('/path', Path::join('/path')); + $this->assertSame('/path/to', Path::join('/path', 'to')); + $this->assertSame('/path/to/test', Path::join('/path', 'to', '/test')); + $this->assertSame('/path/to/test/subdir', Path::join('/path', 'to', '/test', 'subdir/')); + } + + public function testGetHomeDirectoryFailsIfNotSupportedOperationSystem() + { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Your environment or operation system isn\'t supported'); + + putenv('HOME='); + + Path::getHomeDirectory(); + } + + public function testGetHomeDirectoryForUnix() + { + $this->assertEquals('/home/webmozart', Path::getHomeDirectory()); + } + + public function testGetHomeDirectoryForWindows() + { + putenv('HOME='); + putenv('HOMEDRIVE=C:'); + putenv('HOMEPATH=/users/webmozart'); + + $this->assertEquals('C:/users/webmozart', Path::getHomeDirectory()); + } + + public function testNormalize() + { + $this->assertSame('C:/Foo/Bar/test', Path::normalize('C:\\Foo\\Bar/test')); + } +} diff --git a/src/Symfony/Component/Filesystem/composer.json b/src/Symfony/Component/Filesystem/composer.json index a6c17a1e5fba9..e756104cd5fa4 100644 --- a/src/Symfony/Component/Filesystem/composer.json +++ b/src/Symfony/Component/Filesystem/composer.json @@ -18,6 +18,7 @@ "require": { "php": ">=7.2.5", "symfony/polyfill-ctype": "~1.8", + "symfony/polyfill-mbstring": "~1.8", "symfony/polyfill-php80": "^1.16" }, "autoload": {