From 82652bfd914ed9c6fcbca25c1f991ee3f583bece Mon Sep 17 00:00:00 2001 From: Sabri Taieb Date: Sat, 21 Feb 2026 06:39:45 +0100 Subject: [PATCH] fix: allow assertUrlIs() to accept path-only notation --- src/Api/Concerns/MakesUrlAssertions.php | 31 ++++++++++++++++--------- tests/Browser/Webpage/AssertUrlTest.php | 16 +++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/Api/Concerns/MakesUrlAssertions.php b/src/Api/Concerns/MakesUrlAssertions.php index 564a9d4a..89525812 100644 --- a/src/Api/Concerns/MakesUrlAssertions.php +++ b/src/Api/Concerns/MakesUrlAssertions.php @@ -19,22 +19,31 @@ public function assertUrlIs(string $url): Webpage { $pattern = str_replace('\*', '.*', preg_quote($url, '/')); - /** @var array{ scheme: string, host: string, port?: int, path?: string} $segments */ $segments = parse_url($this->page->url()); - $currentUrl = sprintf( - '%s://%s%s%s', - $segments['scheme'], - $segments['host'], - isset($segments['port']) ? ':'.$segments['port'] : '', - $segments['path'] ?? '' - ); + if (parse_url($url, PHP_URL_SCHEME) !== null) { + $currentUrl = sprintf( + '%s://%s%s%s', + $segments['scheme'], + $segments['host'], + isset($segments['port']) + ? ':'.$segments['port'] + : '', + $segments['path'] ?? '' + ); - $currentUrl = mb_rtrim($currentUrl, '/'); + $currentUrl = mb_rtrim($currentUrl, '/'); - $message = "Actual URL [{$currentUrl}] does not equal expected URL [{$url}]."; + $message = "Actual URL [{$currentUrl}] does not equal expected URL [{$url}]."; - expect($currentUrl)->toMatch('/^'.$pattern.'$/u', $message); + expect($currentUrl)->toMatch('/^'.$pattern.'$/u', $message); + } else { + $currentPath = $segments['path'] ?? ''; + + $message = "Actual path [{$currentPath}] does not equal expected path [{$url}]."; + + expect($currentPath)->toMatch('/^'.$pattern.'$/u', $message); + } return $this; } diff --git a/tests/Browser/Webpage/AssertUrlTest.php b/tests/Browser/Webpage/AssertUrlTest.php index e405699b..82aff809 100644 --- a/tests/Browser/Webpage/AssertUrlTest.php +++ b/tests/Browser/Webpage/AssertUrlTest.php @@ -12,6 +12,22 @@ $page->assertUrlIs(url('/test-url')); }); +it('may assert current URL path using path-only notation', function (): void { + Route::get('/login', fn (): string => 'Login Page'); + + $page = visit('/login'); + + $page->assertUrlIs('/login'); +}); + +it('may fail when asserting URL path using path-only notation but it does not match', function (): void { + Route::get('/login', fn (): string => 'Login Page'); + + $page = visit('/login'); + + $page->assertUrlIs('/wrong-path'); +})->throws(ExpectationFailedException::class); + it('may fail when asserting URL matches but it does not', function (): void { Route::get('/test-url', fn (): string => 'Test URL Page');