Skip to content

Commit 6d825ed

Browse files
Add assertRedirectBack assertion method (#55635)
1 parent ae91534 commit 6d825ed

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ public function assertRedirectContains($uri)
213213
return $this;
214214
}
215215

216+
/**
217+
* Assert whether the response is redirecting back to the previous location.
218+
*
219+
* @return $this
220+
*/
221+
public function assertRedirectBack()
222+
{
223+
PHPUnit::withResponse($this)->assertTrue(
224+
$this->isRedirect(),
225+
$this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()),
226+
);
227+
228+
$this->assertLocation(app('url')->previous());
229+
230+
return $this;
231+
}
232+
216233
/**
217234
* Assert whether the response is redirecting to a given route.
218235
*

tests/Testing/TestResponseTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,27 @@ public function testAssertRedirect()
26522652
$response->assertRedirect();
26532653
}
26542654

2655+
public function testAssertRedirectBack()
2656+
{
2657+
app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));
2658+
2659+
$store->setPreviousUrl('https://url.com');
2660+
2661+
app('url')->setSessionResolver(fn () => app('session.store'));
2662+
2663+
$response = TestResponse::fromBaseResponse(
2664+
(new Response('', 302))->withHeaders(['Location' => 'https://url.com'])
2665+
);
2666+
2667+
$response->assertRedirectBack();
2668+
2669+
$this->expectException(ExpectationFailedException::class);
2670+
2671+
$store->setPreviousUrl('https://url.net');
2672+
2673+
$response->assertRedirectBack();
2674+
}
2675+
26552676
public function testGetDecryptedCookie()
26562677
{
26572678
$response = TestResponse::fromBaseResponse(

0 commit comments

Comments
 (0)