Skip to content

Handle "constraints" option in form unit testing#4213

Closed
sarcher wants to merge 2 commits intosymfony:2.5from
sarcher:patch-1
Closed

Handle "constraints" option in form unit testing#4213
sarcher wants to merge 2 commits intosymfony:2.5from
sarcher:patch-1

Conversation

@sarcher
Copy link
Copy Markdown
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.
Copy link
Copy Markdown
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
Copy Markdown
Contributor

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

Thanks!

@stof
Copy link
Copy Markdown
Member

stof commented Sep 16, 2014

@weaverryan yep, it is 2.3+

@weaverryan
Copy link
Copy Markdown
Contributor

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