Skip to content

Added a note about redirections to absolute URLs in tests #7521

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

Closed
wants to merge 2 commits into from

Conversation

javiereguiluz
Copy link
Member

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -284,8 +284,9 @@ document::

// Assert that the response is a redirect to /demo/contact
$this->assertTrue(
$client->getResponse()->isRedirect('/demo/contact'),
'response is a redirect to /demo/contact'
$client->getResponse()->isRedirect('/demo/contact')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ease debugging, I learned to favor:

$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
$this->assertSame('/demo/contact', $client->getResponse()->headers->get('location'));

And since it's very verbose and repetitive I've got a ControllerTestTrait which provides assertions like:

public function assertResponseStatusCode(Client $client, $code)
{
    $this->assertSame($code, $client->getResponse()->getStatusCode());
}

public function assertClientIsRedirectedTo(Client $client, $url, $permanent = false)
{
    $this->assertResponseStatusCode($client, $permanent ? Response::HTTP_MOVED_PERMANENTLY : Response::HTTP_FOUND);
    $this->assertSame($url, $client->getResponse()->headers->get('location'));
}

As said in another PR, we need to avoid failure messages like "true is not false" as much as possible to debug faster. Comparing values gives insight immediately.

Maybe it is worth to update the example to advise it, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid that's too much for this PR. Even if I agree with your idea (although I use a different implementation) I'd say that change would need a discussion first. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Javier. Let's keep it as is here.

@xabbuh
Copy link
Member

xabbuh commented Apr 15, 2017

Thank you @javiereguiluz.

xabbuh added a commit that referenced this pull request Apr 15, 2017
… (javiereguiluz)

This PR was squashed before being merged into the 2.7 branch (closes #7521).

Discussion
----------

Added a note about redirections to absolute URLs in tests

This fixes symfony/symfony#21189

Commits
-------

8414fb7 Added a note about redirections to absolute URLs in tests
@xabbuh xabbuh closed this Apr 15, 2017
@javiereguiluz javiereguiluz deleted the phpunit_redirect branch May 24, 2018 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants