@@ -161,7 +161,8 @@ Adding Custom Extensions
161161------------------------
162162
163163It often happens that you use some options that are added by
164- :doc: `form extensions </form/create_form_type_extension >`.
164+ :doc: `form extensions </form/create_form_type_extension >`. One of the
165+ cases may be the ``ValidatorExtension `` with its ``invalid_message `` option.
165166The ``TypeTestCase `` only loads the core form extension, which means an
166167:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ InvalidOptionsException `
167168will be raised if you try to test a class that depends on other extensions.
@@ -172,23 +173,30 @@ allows you to return a list of extensions to register::
172173 namespace AppBundle\Tests\Form\Type;
173174
174175 use AppBundle\Form\Type\TestedType;
176+ use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
175177 use Symfony\Component\Form\Form;
176178 use Symfony\Component\Form\Forms;
177179 use Symfony\Component\Form\FormBuilder;
178180 use Symfony\Component\Form\Test\TypeTestCase;
181+ use Symfony\Component\Validator\ConstraintViolationList;
179182 use Symfony\Component\Validator\Mapping\ClassMetadata;
183+ use Symfony\Component\Validator\Validator\ValidatorInterface;
180184
181185 class TestedTypeTest extends TypeTestCase
182186 {
183187 protected function getExtensions()
184188 {
189+ $validator = $this->createMock(ValidatorInterface::class);
185190 // use getMock() on PHPUnit 5.3 or below
186191 // $validator = $this->getMock(ValidatorInterface::class);
192+ $validator
193+ ->method('validate')
194+ ->will($this->returnValue(new ConstraintViolationList()));
187195 $validator
188196 ->method('getMetadataFor')
189197 ->will($this->returnValue(new ClassMetadata(Form::class)));
190198 return array(
191- new MyFormExtension( ),
199+ new ValidatorExtension($validator ),
192200 );
193201 }
194202
0 commit comments