Skip to content

Commit 9866967

Browse files
xurshudyanXurshudyantaylorotwell
authored
[12.x] Add assertRedirectToAction method to test redirection to controller actions (#55788)
* Add assertRedirectToAction method * Add test * Update TestResponse.php --------- Co-authored-by: Xurshudyan <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent a841a51 commit 9866967

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/Illuminate/Testing/TestResponse.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,27 @@ public function assertRedirectToSignedRoute($name = null, $parameters = [], $abs
305305
return $this;
306306
}
307307

308+
/**
309+
* Assert whether the response is redirecting to a given controller action.
310+
*
311+
* @param string|array $name
312+
* @param array $parameters
313+
* @return $this
314+
*/
315+
public function assertRedirectToAction($name, $parameters = [])
316+
{
317+
$uri = action($name, $parameters);
318+
319+
PHPUnit::withResponse($this)->assertTrue(
320+
$this->isRedirect(),
321+
$this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()),
322+
);
323+
324+
$this->assertLocation($uri);
325+
326+
return $this;
327+
}
328+
308329
/**
309330
* Asserts that the response contains the given header and equals the optional value.
310331
*
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Testing;
4+
5+
use Illuminate\Contracts\Routing\Registrar;
6+
use Illuminate\Http\RedirectResponse;
7+
use Illuminate\Routing\Controller;
8+
use Illuminate\Routing\UrlGenerator;
9+
use Illuminate\Support\Facades\Facade;
10+
use Orchestra\Testbench\TestCase;
11+
12+
class AssertRedirectToActionTest extends TestCase
13+
{
14+
/**
15+
* @var \Illuminate\Contracts\Routing\Registrar
16+
*/
17+
private $router;
18+
19+
/**
20+
* @var \Illuminate\Routing\UrlGenerator
21+
*/
22+
public $urlGenerator;
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
$this->router = $this->app->make(Registrar::class);
29+
30+
$this->router->get('controller/index', [TestActionController::class, 'index']);
31+
$this->router->get('controller/show/{id}', [TestActionController::class, 'show']);
32+
33+
$this->router->get('redirect-to-index', function () {
34+
return new RedirectResponse($this->urlGenerator->action([TestActionController::class, 'index']));
35+
});
36+
37+
$this->router->get('redirect-to-show', function () {
38+
return new RedirectResponse($this->urlGenerator->action([TestActionController::class, 'show'], ['id' => 123]));
39+
});
40+
41+
$this->urlGenerator = $this->app->make(UrlGenerator::class);
42+
}
43+
44+
public function testAssertRedirectToActionWithoutParameters()
45+
{
46+
$this->get('redirect-to-index')
47+
->assertRedirectToAction([TestActionController::class, 'index']);
48+
}
49+
50+
public function testAssertRedirectToActionWithParameters()
51+
{
52+
$this->get('redirect-to-show')
53+
->assertRedirectToAction([TestActionController::class, 'show'], ['id' => 123]);
54+
}
55+
56+
protected function tearDown(): void
57+
{
58+
parent::tearDown();
59+
60+
Facade::setFacadeApplication(null);
61+
}
62+
}
63+
64+
class TestActionController extends Controller
65+
{
66+
public function index()
67+
{
68+
return 'ok';
69+
}
70+
71+
public function show($id)
72+
{
73+
return "id: $id";
74+
}
75+
}

0 commit comments

Comments
 (0)