Skip to content

Commit 4807fee

Browse files
[11.x] Backport TestResponse::assertRedirectBack (#55780)
* Backport `TestResponse::assertRedirectBack` * Backport tests too
1 parent 6c3e369 commit 4807fee

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
@@ -214,6 +214,23 @@ public function assertRedirectContains($uri)
214214
return $this;
215215
}
216216

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

tests/Testing/TestResponseTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,6 +2631,27 @@ public function testAssertRedirect()
26312631
$response->assertRedirect();
26322632
}
26332633

2634+
public function testAssertRedirectBack()
2635+
{
2636+
app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));
2637+
2638+
$store->setPreviousUrl('https://url.com');
2639+
2640+
app('url')->setSessionResolver(fn () => app('session.store'));
2641+
2642+
$response = TestResponse::fromBaseResponse(
2643+
(new Response('', 302))->withHeaders(['Location' => 'https://url.com'])
2644+
);
2645+
2646+
$response->assertRedirectBack();
2647+
2648+
$this->expectException(ExpectationFailedException::class);
2649+
2650+
$store->setPreviousUrl('https://url.net');
2651+
2652+
$response->assertRedirectBack();
2653+
}
2654+
26342655
public function testGetDecryptedCookie()
26352656
{
26362657
$response = TestResponse::fromBaseResponse(

0 commit comments

Comments
 (0)