diff --git a/src/Concerns/MakesUrlAssertions.php b/src/Concerns/MakesUrlAssertions.php index dba124859..f221caa03 100644 --- a/src/Concerns/MakesUrlAssertions.php +++ b/src/Concerns/MakesUrlAssertions.php @@ -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. * diff --git a/tests/Unit/MakesUrlAssertionsTest.php b/tests/Unit/MakesUrlAssertionsTest.php index 2726123c2..90273b8de 100644 --- a/tests/Unit/MakesUrlAssertionsTest.php +++ b/tests/Unit/MakesUrlAssertionsTest.php @@ -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);