Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertPathEndsWith() and assertPathContains() #1100

Merged
merged 1 commit into from
Apr 16, 2024
Merged
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
36 changes: 36 additions & 0 deletions src/Concerns/MakesUrlAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,42 @@ public function assertPathBeginsWith($path)
return $this;
}

/**
* Assert that the current URL path ends with the given path.
*
* @param string $path
* @return $this
*/
public function assertPathEndsWith($path)
{
$actualPath = parse_url($this->driver->getCurrentURL(), PHP_URL_PATH) ?? '';

PHPUnit::assertStringEndsWith(
$path, $actualPath,
"Actual path [{$actualPath}] does not end with expected path [{$path}]."
);

return $this;
}

/**
* Assert that the current URL path contains the given path.
*
* @param string $path
* @return $this
*/
public function assertPathContains($path)
{
$actualPath = parse_url($this->driver->getCurrentURL(), PHP_URL_PATH) ?? '';

PHPUnit::assertStringContainsString(
$path, $actualPath,
"Actual path [{$actualPath}] does not contain the expected string [{$path}]."
);

return $this;
}

/**
* Assert that the current path matches the given path.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/MakesUrlAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,46 @@ public function test_assert_path_begins_with()
}
}

public function test_assert_path_ends_with(): void
{
$driver = m::mock(stdClass::class);
$driver->shouldReceive('getCurrentURL')->andReturn(
'http://www.google.com/test/ending'
);
$browser = new Browser($driver);

$browser->assertPathEndsWith('ending');

try {
$browser->assertPathEndsWith('/not-the-ending-expected');
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString(
'Actual path [/test/ending] does not end with expected path [/not-the-ending-expected].',
$e->getMessage()
);
}
}

public function test_assert_path_contains(): void
{
$driver = m::mock(stdClass::class);
$driver->shouldReceive('getCurrentURL')->andReturn(
'http://www.google.com/admin/test/1/details'
);
$browser = new Browser($driver);

$browser->assertPathContains('/test/1/');

try {
$browser->assertPathContains('/test/2/');
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString(
'Actual path [/admin/test/1/details] does not contain the expected string [/test/2/].',
$e->getMessage()
);
}
}

public function test_assert_path_is()
{
$driver = m::mock(stdClass::class);
Expand Down