From d3febdafd142d5464dbeb79f4f235ca97436abdb Mon Sep 17 00:00:00 2001 From: Xurshudyan Date: Tue, 20 May 2025 10:35:35 +0400 Subject: [PATCH 1/3] Add assertRedirectToAction method --- src/Illuminate/Testing/TestResponse.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index a6b9940976f7..78f11aba6abd 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -266,6 +266,27 @@ public function assertRedirectToRoute($name, $parameters = []) 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; + } + /** * Assert whether the response is redirecting to a given signed route. * From 630c52420a7a832c77e4746b16a7d377850528c0 Mon Sep 17 00:00:00 2001 From: Xurshudyan Date: Tue, 20 May 2025 10:53:51 +0400 Subject: [PATCH 2/3] Add test --- tests/Testing/AssertRedirectToActionTest.php | 75 ++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/Testing/AssertRedirectToActionTest.php 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"; + } +} From fa4ac5d1334f4282d5bcccba7430373710db41b4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 20 May 2025 10:04:39 -0500 Subject: [PATCH 3/3] Update TestResponse.php --- src/Illuminate/Testing/TestResponse.php | 42 ++++++++++++------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/Illuminate/Testing/TestResponse.php b/src/Illuminate/Testing/TestResponse.php index 78f11aba6abd..6cf3e9b75b33 100644 --- a/src/Illuminate/Testing/TestResponse.php +++ b/src/Illuminate/Testing/TestResponse.php @@ -266,27 +266,6 @@ public function assertRedirectToRoute($name, $parameters = []) 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; - } - /** * Assert whether the response is redirecting to a given signed route. * @@ -326,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. *