Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[12.x] Add assertRedirectToAction method to test redirection to controller actions #55788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions 21 src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
75 changes: 75 additions & 0 deletions 75 tests/Testing/AssertRedirectToActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Illuminate\Tests\Testing;

use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Controller;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\Facade;
use Orchestra\Testbench\TestCase;

class AssertRedirectToActionTest extends TestCase
{
/**
* @var \Illuminate\Contracts\Routing\Registrar
*/
private $router;

/**
* @var \Illuminate\Routing\UrlGenerator
*/
public $urlGenerator;

protected function setUp(): void
{
parent::setUp();

$this->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";
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.