Skip to content

Commit

Permalink
New Assertions: assertPathEndsWith and assertPathContains (#1088) (#1100
Browse files Browse the repository at this point in the history
)

* Add Assertion: assertPathEndsWith

* Add Assertion: assertPathContains

* Added return types

Co-authored-by: Shawn Hooper <[email protected]>
  • Loading branch information
u01jmg3 and shawnhooper authored Apr 16, 2024
1 parent b39ef34 commit 773a12d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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

0 comments on commit 773a12d

Please sign in to comment.