Skip to content
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 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 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";
}
}