From cf82d55850033c9ca7414e0078663f5adc44013e Mon Sep 17 00:00:00 2001 From: Aleksei Lebedev <1329824+LastDragon-ru@users.noreply.github.com> Date: Sat, 25 May 2024 10:02:51 +0400 Subject: [PATCH] fix(core): `Path::getRelativePath()` will return correct path when `$root` is file. (cherry picked from commit 9478b446b1852a82f05326e8abed08828b585801) --- packages/core/src/Utils/Path.php | 5 ++--- packages/core/src/Utils/PathTest.php | 2 ++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/core/src/Utils/Path.php b/packages/core/src/Utils/Path.php index 63e76c6d..9535bf4a 100644 --- a/packages/core/src/Utils/Path.php +++ b/packages/core/src/Utils/Path.php @@ -18,9 +18,8 @@ public static function getPath(string $root, string $path): string { } public static function getRelativePath(string $root, string $path): string { - $path = static::isRelative($path) - ? static::join(static::getDirname($root), $path) - : $path; + $root = static::getDirname($root); + $path = static::isRelative($path) ? static::join($root, $path) : $path; $path = SymfonyPath::makeRelative($path, $root); $path = static::normalize($path); diff --git a/packages/core/src/Utils/PathTest.php b/packages/core/src/Utils/PathTest.php index b8264200..1165c9b5 100644 --- a/packages/core/src/Utils/PathTest.php +++ b/packages/core/src/Utils/PathTest.php @@ -5,6 +5,7 @@ use LastDragon_ru\LaraASP\Core\Testing\Package\TestCase; use PHPUnit\Framework\Attributes\CoversClass; +use function basename; use function dirname; use function str_replace; @@ -25,6 +26,7 @@ public function testGetPath(): void { public function testGetRelativePath(): void { self::assertEquals('../to/file', Path::getRelativePath('/any/path', '/any/path/../to/file')); self::assertEquals('to/file', Path::getRelativePath('/absolute/path', 'to/./file')); + self::assertEquals(basename(__FILE__), Path::getRelativePath(__FILE__, __FILE__)); } public function testJoin(): void {