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

Handle "constraints" option in form unit testing #4213

Closed
wants to merge 2 commits into from

Conversation

sarcher
Copy link
Contributor

@sarcher sarcher commented Sep 8, 2014

In the current documentation, although a mocked ValidatorInterface is being passed to the FormTypeValidatorExtension, the actual validate() method in it is returning null. This causes any test against a form type that utilizes the extension's constraints option to fail, because of the following code in Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener:

            // Validate the form in group "Default"
            $violations = $this->validator->validate($form);

            foreach ($violations as $violation) {
                // Allow the "invalid" constraint to be put onto
                // non-synchronized forms
                $allowNonSynchronized = Form::ERR_INVALID === $violation->getCode();

                $this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
            }

Note the foreach loop that is expecting an array; currently the mocked object returns null and any test fails.

Since the documentation uses the ValidatorExtension as a specific example, I think it would be nice for the example code to handle this case, preventing the user from having to dig deeper into the code to discover the problem.

In the current documentation, although a mocked `ValidatorInterface` is being passed to the `FormTypeValidatorExtension`, the actual `validate()` method in it is returning null. This causes any test against a form type that utilizes the extension's `constraints` option to fail, because of the following code in `Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener`:

```php
            // Validate the form in group "Default"
            $violations = $this->validator->validate($form);

            foreach ($violations as $violation) {
                // Allow the "invalid" constraint to be put onto
                // non-synchronized forms
                $allowNonSynchronized = Form::ERR_INVALID === $violation->getCode();

                $this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
            }
```

Note the `foreach` loop that is expecting an array.

Since the documentation uses the `ValidatorExtension` as a specific example, I think it would be nice for the example code to handle this case, preventing the user from having to dig deeper into the code to discover the problem.
@@ -183,12 +183,15 @@ on other extensions. You need add those extensions to the factory object::
protected function setUp()
{
parent::setUp();

$validator = $this->getMock('\Symfony\Component\Validator\ValidatorInterface');
$validator->method('validate')->will($this->returnValue(array()));
Copy link
Member

Choose a reason for hiding this comment

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

it should actually return a ConstraintViolationList

Replaced `array()` with `new ConstraintViolationList()`.
@weaverryan
Copy link
Member

I believe this applies all the way back to version 2.3. Can someone verify?

Thanks!

@stof
Copy link
Member

stof commented Sep 16, 2014

@weaverryan yep, it is 2.3+

@weaverryan
Copy link
Member

Thanks for the report and the fix! Cheers Shane!

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