@@ -161,7 +161,8 @@ Adding Custom Extensions
161
161
------------------------
162
162
163
163
It 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.
165
166
The ``TypeTestCase `` only loads the core form extension, which means an
166
167
:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ InvalidOptionsException `
167
168
will 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::
172
173
namespace AppBundle\Tests\Form\Type;
173
174
174
175
use AppBundle\Form\Type\TestedType;
176
+ use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
175
177
use Symfony\Component\Form\Form;
176
178
use Symfony\Component\Form\Forms;
177
179
use Symfony\Component\Form\FormBuilder;
178
180
use Symfony\Component\Form\Test\TypeTestCase;
181
+ use Symfony\Component\Validator\ConstraintViolationList;
179
182
use Symfony\Component\Validator\Mapping\ClassMetadata;
183
+ use Symfony\Component\Validator\Validator\ValidatorInterface;
180
184
181
185
class TestedTypeTest extends TypeTestCase
182
186
{
183
187
protected function getExtensions()
184
188
{
189
+ $validator = $this->createMock(ValidatorInterface::class);
185
190
// use getMock() on PHPUnit 5.3 or below
186
191
// $validator = $this->getMock(ValidatorInterface::class);
192
+ $validator
193
+ ->method('validate')
194
+ ->will($this->returnValue(new ConstraintViolationList()));
187
195
$validator
188
196
->method('getMetadataFor')
189
197
->will($this->returnValue(new ClassMetadata(Form::class)));
190
198
return array(
191
- new MyFormExtension( ),
199
+ new ValidatorExtension($validator ),
192
200
);
193
201
}
194
202
0 commit comments