diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d3e9bd..c2ecbb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Fixed + +- Some relative paths were parsed as absolute ones + ## 5.2.0 - 2026-03-17 ### Added diff --git a/src/Path.php b/src/Path.php index 2a36d0f..66975e3 100644 --- a/src/Path.php +++ b/src/Path.php @@ -35,6 +35,14 @@ final public static function of(string $value): self return new RelativePath($value); } + // For some paths the parsing removes chars leading to the parsing + // thinking the path is absolute when the specified one is relative. + // But since we don't use the parsed string, to avoid using encoded + // strings, then the relative path is returned as an absolute one. + if ($value[0] !== '/') { + return new RelativePath($value); + } + return new AbsolutePath($value); } diff --git a/tests/PathTest.php b/tests/PathTest.php index 5719cfc..02e4347 100644 --- a/tests/PathTest.php +++ b/tests/PathTest.php @@ -8,11 +8,17 @@ AbsolutePath, RelativePath, }; -use Innmind\BlackBox\PHPUnit\Framework\TestCase; +use Fixtures\Innmind\Url\Path as FPath; +use Innmind\BlackBox\{ + PHPUnit\BlackBox, + PHPUnit\Framework\TestCase, +}; use PHPUnit\Framework\Attributes\DataProvider; class PathTest extends TestCase { + use BlackBox; + public function testInterface() { $p = Path::of('/foo/bar/'); @@ -79,6 +85,19 @@ public function testNamedConstructorsReturnsAppropriateSubType() $this->assertInstanceOf(RelativePath::class, Path::of('../somewhere')); } + public function testAbsolutePathsAlwaysStartWithASlash(): BlackBox\Proof + { + return $this + ->forAll(FPath::any()) + ->prove(function($path) { + if ($path->absolute()) { + $this->assertStringStartsWith('/', $path->toString()); + } else { + $this->assertStringStartsNotWith('/', $path->toString()); + } + }); + } + public static function resolutions(): array { return [