Browse files

bug #4213 Handle "constraints" option in form unit testing (sarcher)

This PR was submitted for the 2.5 branch but it was merged into the 2.3 branch instead (closes #4213).


Handle "constraints" option in form unit testing

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.


3746f07 Fixed return value
8251e79 Handle "constraints" option in form unit testing
  • Loading branch information...
weaverryan committed Sep 16, 2014
2 parents 9b4b36f + 0b2c979 commit 2319d6a391ee16f772ac34b5c4336680a26f8992
Showing with 5 additions and 1 deletion.
  1. +5 −1 cookbook/form/unit_testing.rst
@@ -177,18 +177,22 @@ on other extensions. You need add those extensions to the factory object::
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Extension\Validator\Type\FormTypeValidatorExtension;
use Symfony\Component\Validator\ConstraintViolationList;
class TestedTypeTest extends TypeTestCase
protected function setUp()
$validator = $this->getMock('\Symfony\Component\Validator\ValidatorInterface');
$validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));
$this->factory = Forms::createFormFactoryBuilder()
new FormTypeValidatorExtension(

0 comments on commit 2319d6a

Please sign in to comment.