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

Add ResponseIsUnprocessable assertion #40894

Closed
garak opened this issue Apr 21, 2021 · 3 comments · Fixed by #43671
Closed

Add ResponseIsUnprocessable assertion #40894

garak opened this issue Apr 21, 2021 · 3 comments · Fixed by #43671

Comments

@garak
Copy link
Contributor

garak commented Apr 21, 2021

Description
Now that we got an automatic way to get 422 response code when a form is invalid (see #40799), I think it could be nice to ease testing it.
I'm proposing to add a new method to BrowserKitAssertionsTrait:

<?php

namespace Symfony\Bundle\FrameworkBundle\Test;

trait BrowserKitAssertionsTrait
{
    public static function assertResponseIsUnprocessable(string $message = ''): void
    {
        self::assertThatForResponse(new ResponseConstraint\ResponseIsUnprocessable(), $message);
    }
    // ...
}

with relative constraint

<?php

namespace Symfony\Component\HttpFoundation\Test\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\HttpFoundation\Response;

final class ResponseIsUnprocessable extends Constraint
{
    /**
     * {@inheritdoc}
     */
    public function toString(): string
    {
        return 'is unprocessable';
    }

    /**
     * @param Response $response
     *
     * {@inheritdoc}
     */
    protected function matches($response): bool
    {
        return Response::HTTP_UNPROCESSABLE_ENTITY === $response->getStatusCode();
    }

    /**
     * @param Response $response
     *
     * {@inheritdoc}
     */
    protected function failureDescription($response): string
    {
        return 'the Response '.$this->toString();
    }

    /**
     * @param Response $response
     *
     * {@inheritdoc}
     */
    protected function additionalFailureDescription($response): string
    {
        return (string) $response;
    }
}

Example

Instead of calling self::assertResponseStatusCodeSame(422) or self::assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY) each time that you want to test for a submitted invali form, you could simply call self::assertResponseIsUnprocessable()

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@garak
Copy link
Contributor Author

garak commented Oct 22, 2021

Thank you for this suggestion. There has not been a lot of activity here for a while. Would you still like to see this feature?

I do.

@carsonbot carsonbot removed the Stalled label Oct 22, 2021
@fabpot
Copy link
Member

fabpot commented Oct 23, 2021

Please, go ahead and create a PR.

@fabpot fabpot closed this as completed Oct 26, 2021
fabpot added a commit that referenced this issue Oct 26, 2021
This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

add ResponseIsUnprocessable

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #40894
| License       | MIT
| Doc PR        | none

Commits
-------

61df7f1 add ResponseIsUnprocessable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants