Skip to content

Commit 0247079

Browse files
zahergtaylorotwell
andauthored
[9.x] Add assertRedirectToRoute to TestResponse (#44926)
* adding assertRedirectToRoute to TestResponse * Update TestResponse.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5a36ee9 commit 0247079

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,33 @@ public function assertRedirectContains($uri)
252252
return $this;
253253
}
254254

255+
/**
256+
* Assert whether the response is redirecting to a given route.
257+
*
258+
* @param string|null $name
259+
* @param mixed $parameters
260+
* @return $this
261+
*/
262+
public function assertRedirectToRoute($name = null, $parameters = [])
263+
{
264+
if (! is_null($name)) {
265+
$uri = route($name, $parameters);
266+
}
267+
268+
PHPUnit::assertTrue(
269+
$this->isRedirect(),
270+
$this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()),
271+
);
272+
273+
$request = Request::create($this->headers->get('Location'));
274+
275+
PHPUnit::assertEquals(
276+
app('url')->to($uri), $request->fullUrl()
277+
);
278+
279+
return $this;
280+
}
281+
255282
/**
256283
* Assert whether the response is redirecting to a given signed route.
257284
*
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Testing;
4+
5+
use Illuminate\Contracts\Routing\Registrar;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Routing\UrlGenerator;
8+
use Illuminate\Support\Facades\Facade;
9+
use Orchestra\Testbench\TestCase;
10+
11+
class AssertRedirectToRouteTest extends TestCase
12+
{
13+
/**
14+
* @var \Illuminate\Contracts\Routing\Registrar
15+
*/
16+
private $router;
17+
18+
/**
19+
* @var \Illuminate\Routing\UrlGenerator
20+
*/
21+
private $urlGenerator;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
$this->router = $this->app->make(Registrar::class);
28+
29+
$this->router
30+
->get('named-route')
31+
->name('named-route');
32+
33+
$this->router
34+
->get('named-route-with-param/{param}')
35+
->name('named-route-with-param');
36+
37+
$this->urlGenerator = $this->app->make(UrlGenerator::class);
38+
}
39+
40+
public function testAssertRedirectToRouteWithRouteName()
41+
{
42+
$this->router->get('test-route', function () {
43+
return new RedirectResponse($this->urlGenerator->route('named-route'));
44+
});
45+
46+
$this->get('test-route')
47+
->assertRedirectToRoute('named-route');
48+
}
49+
50+
public function testAssertRedirectToRouteWithRouteNameAndParams()
51+
{
52+
$this->router->get('test-route', function () {
53+
return new RedirectResponse($this->urlGenerator->route('named-route-with-param', 'hello'));
54+
});
55+
56+
$this->router->get('test-route-with-extra-param', function () {
57+
return new RedirectResponse($this->urlGenerator->route('named-route-with-param', [
58+
'param' => 'foo',
59+
'extra' => 'another',
60+
]));
61+
});
62+
63+
$this->get('test-route')
64+
->assertRedirectToRoute('named-route-with-param', 'hello');
65+
66+
$this->get('test-route-with-extra-param')
67+
->assertRedirectToRoute('named-route-with-param', [
68+
'param' => 'foo',
69+
'extra' => 'another',
70+
]);
71+
}
72+
73+
protected function tearDown(): void
74+
{
75+
parent::tearDown();
76+
77+
Facade::setFacadeApplication(null);
78+
}
79+
}

0 commit comments

Comments
 (0)