From 8251e79c69909aacdcf83fc104172bdaf3a13c00 Mon Sep 17 00:00:00 2001 From: Shane Archer Date: Mon, 8 Sep 2014 14:44:58 -0700 Subject: [PATCH 1/2] 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`: ```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. --- cookbook/form/unit_testing.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index 7a9100fda29..e3595367fdb 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -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())); $this->factory = Forms::createFormFactoryBuilder() ->addExtensions($this->getExtensions()) ->addTypeExtension( new FormTypeValidatorExtension( - $this->getMock('Symfony\Component\Validator\ValidatorInterface') + $validator ) ) ->addTypeGuesser( From 3746f07c015882fc5935d903c5c1054df3b5428c Mon Sep 17 00:00:00 2001 From: Shane Archer Date: Mon, 8 Sep 2014 16:43:06 -0700 Subject: [PATCH 2/2] Fixed return value Replaced `array()` with `new ConstraintViolationList()`. --- cookbook/form/unit_testing.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cookbook/form/unit_testing.rst b/cookbook/form/unit_testing.rst index e3595367fdb..944ed25b372 100644 --- a/cookbook/form/unit_testing.rst +++ b/cookbook/form/unit_testing.rst @@ -177,6 +177,7 @@ 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 { @@ -185,7 +186,7 @@ on other extensions. You need add those extensions to the factory object:: parent::setUp(); $validator = $this->getMock('\Symfony\Component\Validator\ValidatorInterface'); - $validator->method('validate')->will($this->returnValue(array())); + $validator->method('validate')->will($this->returnValue(new ConstraintViolationList())); $this->factory = Forms::createFormFactoryBuilder() ->addExtensions($this->getExtensions())