diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index a6b9940976f7..6cf3e9b75b33 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -305,6 +305,27 @@ public function assertRedirectToSignedRoute($name = null, $parameters = [], $abs return $this; } + /** + * Assert whether the response is redirecting to a given controller action. + * + * @param string|array $name + * @param array $parameters + * @return $this + */ + public function assertRedirectToAction($name, $parameters = []) + { + $uri = action($name, $parameters); + + PHPUnit::withResponse($this)->assertTrue( + $this->isRedirect(), + $this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()), + ); + + $this->assertLocation($uri); + + return $this; + } + /** * Asserts that the response contains the given header and equals the optional value. * diff --git a/tests/Testing/AssertRedirectToActionTest.php b/tests/Testing/AssertRedirectToActionTest.php new file mode 100644 index 000000000000..3dfb6905667f --- /dev/null +++ b/tests/Testing/AssertRedirectToActionTest.php @@ -0,0 +1,75 @@ +router = $this->app->make(Registrar::class); + + $this->router->get('controller/index', [TestActionController::class, 'index']); + $this->router->get('controller/show/{id}', [TestActionController::class, 'show']); + + $this->router->get('redirect-to-index', function () { + return new RedirectResponse($this->urlGenerator->action([TestActionController::class, 'index'])); + }); + + $this->router->get('redirect-to-show', function () { + return new RedirectResponse($this->urlGenerator->action([TestActionController::class, 'show'], ['id' => 123])); + }); + + $this->urlGenerator = $this->app->make(UrlGenerator::class); + } + + public function testAssertRedirectToActionWithoutParameters() + { + $this->get('redirect-to-index') + ->assertRedirectToAction([TestActionController::class, 'index']); + } + + public function testAssertRedirectToActionWithParameters() + { + $this->get('redirect-to-show') + ->assertRedirectToAction([TestActionController::class, 'show'], ['id' => 123]); + } + + protected function tearDown(): void + { + parent::tearDown(); + + Facade::setFacadeApplication(null); + } +} + +class TestActionController extends Controller +{ + public function index() + { + return 'ok'; + } + + public function show($id) + { + return "id: $id"; + } +}