Skip to content

Commit 773a12d

Browse files
u01jmg3shawnhooper
andauthored
New Assertions: assertPathEndsWith and assertPathContains (#1088) (#1100)
* Add Assertion: assertPathEndsWith * Add Assertion: assertPathContains * Added return types Co-authored-by: Shawn Hooper <[email protected]>
1 parent b39ef34 commit 773a12d

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Concerns/MakesUrlAssertions.php

+36
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,42 @@ public function assertPathBeginsWith($path)
168168
return $this;
169169
}
170170

171+
/**
172+
* Assert that the current URL path ends with the given path.
173+
*
174+
* @param string $path
175+
* @return $this
176+
*/
177+
public function assertPathEndsWith($path)
178+
{
179+
$actualPath = parse_url($this->driver->getCurrentURL(), PHP_URL_PATH) ?? '';
180+
181+
PHPUnit::assertStringEndsWith(
182+
$path, $actualPath,
183+
"Actual path [{$actualPath}] does not end with expected path [{$path}]."
184+
);
185+
186+
return $this;
187+
}
188+
189+
/**
190+
* Assert that the current URL path contains the given path.
191+
*
192+
* @param string $path
193+
* @return $this
194+
*/
195+
public function assertPathContains($path)
196+
{
197+
$actualPath = parse_url($this->driver->getCurrentURL(), PHP_URL_PATH) ?? '';
198+
199+
PHPUnit::assertStringContainsString(
200+
$path, $actualPath,
201+
"Actual path [{$actualPath}] does not contain the expected string [{$path}]."
202+
);
203+
204+
return $this;
205+
}
206+
171207
/**
172208
* Assert that the current path matches the given path.
173209
*

tests/Unit/MakesUrlAssertionsTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,46 @@ public function test_assert_path_begins_with()
219219
}
220220
}
221221

222+
public function test_assert_path_ends_with(): void
223+
{
224+
$driver = m::mock(stdClass::class);
225+
$driver->shouldReceive('getCurrentURL')->andReturn(
226+
'http://www.google.com/test/ending'
227+
);
228+
$browser = new Browser($driver);
229+
230+
$browser->assertPathEndsWith('ending');
231+
232+
try {
233+
$browser->assertPathEndsWith('/not-the-ending-expected');
234+
} catch (ExpectationFailedException $e) {
235+
$this->assertStringContainsString(
236+
'Actual path [/test/ending] does not end with expected path [/not-the-ending-expected].',
237+
$e->getMessage()
238+
);
239+
}
240+
}
241+
242+
public function test_assert_path_contains(): void
243+
{
244+
$driver = m::mock(stdClass::class);
245+
$driver->shouldReceive('getCurrentURL')->andReturn(
246+
'http://www.google.com/admin/test/1/details'
247+
);
248+
$browser = new Browser($driver);
249+
250+
$browser->assertPathContains('/test/1/');
251+
252+
try {
253+
$browser->assertPathContains('/test/2/');
254+
} catch (ExpectationFailedException $e) {
255+
$this->assertStringContainsString(
256+
'Actual path [/admin/test/1/details] does not contain the expected string [/test/2/].',
257+
$e->getMessage()
258+
);
259+
}
260+
}
261+
222262
public function test_assert_path_is()
223263
{
224264
$driver = m::mock(stdClass::class);

0 commit comments

Comments
 (0)