Skip to content
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

[9.x] Add assertRedirectToRoute to TestResponse #44926

Merged
merged 2 commits into from
Nov 14, 2022
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
27 changes: 27 additions & 0 deletions src/Illuminate/Testing/TestResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ public function assertRedirectContains($uri)
return $this;
}

/**
* Assert whether the response is redirecting to a given route.
*
* @param string|null $name
* @param mixed $parameters
* @return $this
*/
public function assertRedirectToRoute($name = null, $parameters = [])
{
if (! is_null($name)) {
$uri = route($name, $parameters);
}

PHPUnit::assertTrue(
$this->isRedirect(),
$this->statusMessageWithDetails('201, 301, 302, 303, 307, 308', $this->getStatusCode()),
);

$request = Request::create($this->headers->get('Location'));

PHPUnit::assertEquals(
app('url')->to($uri), $request->fullUrl()
);

return $this;
}

/**
* Assert whether the response is redirecting to a given signed route.
*
Expand Down
79 changes: 79 additions & 0 deletions tests/Testing/AssertRedirectToRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Illuminate\Tests\Testing;

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

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

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

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

$this->router = $this->app->make(Registrar::class);

$this->router
->get('named-route')
->name('named-route');

$this->router
->get('named-route-with-param/{param}')
->name('named-route-with-param');

$this->urlGenerator = $this->app->make(UrlGenerator::class);
}

public function testAssertRedirectToRouteWithRouteName()
{
$this->router->get('test-route', function () {
return new RedirectResponse($this->urlGenerator->route('named-route'));
});

$this->get('test-route')
->assertRedirectToRoute('named-route');
}

public function testAssertRedirectToRouteWithRouteNameAndParams()
{
$this->router->get('test-route', function () {
return new RedirectResponse($this->urlGenerator->route('named-route-with-param', 'hello'));
});

$this->router->get('test-route-with-extra-param', function () {
return new RedirectResponse($this->urlGenerator->route('named-route-with-param', [
'param' => 'foo',
'extra' => 'another',
]));
});

$this->get('test-route')
->assertRedirectToRoute('named-route-with-param', 'hello');

$this->get('test-route-with-extra-param')
->assertRedirectToRoute('named-route-with-param', [
'param' => 'foo',
'extra' => 'another',
]);
}

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

Facade::setFacadeApplication(null);
}
}