Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/Api/Concerns/MakesUrlAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Browser/Webpage/AssertUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down