Skip to content

[9.x] Add assertRedirectToRoute to TestResponse#44926

Merged
taylorotwell merged 2 commits into
laravel:9.xfrom
zaherg:9.x
Nov 14, 2022
Merged

[9.x] Add assertRedirectToRoute to TestResponse#44926
taylorotwell merged 2 commits into
laravel:9.xfrom
zaherg:9.x

Conversation

@zaherg

@zaherg zaherg commented Nov 13, 2022

Copy link
Copy Markdown
Contributor

Illuminate\Testing\TestResponse has several helpers to assert the response has a redirect of various types. There is assertRedirect(string $uri = null), assertRedirectContains(string $uri), and assertRedirectToSignedRoute(string $name = null, array $parameters = []).

This PR add a missing assertion assertRedirectToRoute, which in theory does not differ much from assertRedirectToSignedRoute.

Used as the following:

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

and in case of passing parameters we use it as the following:

$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',
    ]);

@zaherg zaherg changed the title [9.x] Adding assertRedirectToRoute to TestResponse [9.x] Add assertRedirectToRoute to TestResponse Nov 13, 2022
@taylorotwell taylorotwell merged commit 0247079 into laravel:9.x Nov 14, 2022
@gms8994

gms8994 commented Nov 15, 2022

Copy link
Copy Markdown
Contributor

The assertRedirectToRoute function suggests that it can take a null value, but passing in a null value throws an error:

See this test case for PoC:

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

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

@zaherg

zaherg commented Nov 16, 2022

Copy link
Copy Markdown
Contributor Author

I'll push new code in a few min.

thanks @gms8994 for catching this, I added your test to the code to make sure everything is working as it should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants