@@ -157,12 +157,31 @@ before creating the parent form using the ``PreloadedExtension`` class::
157
157
be getting errors that are not related to the form you are currently
158
158
testing but to its children.
159
159
160
+ Forms Using Validation
161
+ ------------------------------------
162
+
163
+ If your forms uses the ``invalid_message `` or ``constraints `` option for validation, you need to
164
+ register the validation extension which provides this options.
165
+ Luckily Symfony provides a custom test class which does this for you.
166
+ In order to have this option registered, your test needs to extend from the
167
+ :class: `Symfony\\ Component\\ Form\\ Tests\\ Extension\\ Validator\\ Type\\ TypeTestCase `
168
+ class::
169
+
170
+ // tests/AppBundle/Form/Type/TestedTypeTests.php
171
+ namespace Tests\AppBundle\Form\Type;
172
+
173
+ use Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase;
174
+
175
+ class TestedTypeTest extends TypeTestCase
176
+ {
177
+ // ...
178
+ }
179
+
160
180
Adding Custom Extensions
161
181
------------------------
162
182
163
183
It often happens that you use some options that are added by
164
- :doc: `form extensions </form/create_form_type_extension >`. One of the
165
- cases may be the ``ValidatorExtension `` with its ``invalid_message `` option.
184
+ :doc: `form extensions </form/create_form_type_extension >`.
166
185
The ``TypeTestCase `` only loads the core form extension, which means an
167
186
:class: `Symfony\\ Component\\ OptionsResolver\\ Exception\\ InvalidOptionsException `
168
187
will be raised if you try to test a class that depends on other extensions.
@@ -173,37 +192,32 @@ allows you to return a list of extensions to register::
173
192
namespace AppBundle\Tests\Form\Type;
174
193
175
194
use AppBundle\Form\Type\TestedType;
176
- use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
177
195
use Symfony\Component\Form\Form;
178
196
use Symfony\Component\Form\Forms;
179
197
use Symfony\Component\Form\FormBuilder;
180
198
use Symfony\Component\Form\Test\TypeTestCase;
181
- use Symfony\Component\Validator\ConstraintViolationList;
182
199
use Symfony\Component\Validator\Mapping\ClassMetadata;
183
- use Symfony\Component\Validator\Validator\ValidatorInterface;
184
200
185
201
class TestedTypeTest extends TypeTestCase
186
202
{
187
203
protected function getExtensions()
188
204
{
189
- $validator = $this->createMock(ValidatorInterface::class);
190
205
// use getMock() on PHPUnit 5.3 or below
191
206
// $validator = $this->getMock(ValidatorInterface::class);
192
- $validator
193
- ->method('validate')
194
- ->will($this->returnValue(new ConstraintViolationList()));
195
207
$validator
196
208
->method('getMetadataFor')
197
209
->will($this->returnValue(new ClassMetadata(Form::class)));
198
-
199
210
return array(
200
- new ValidatorExtension($validator ),
211
+ new MyFormExtension( ),
201
212
);
202
213
}
203
214
204
215
// ... your tests
205
216
}
206
217
218
+ It is also possible to load custom form types, form type extensions or type guessers using the
219
+ ``getTypedExtensions ``, ``getTypes `` and ``getTypeGuessers `` methods.
220
+
207
221
Testing against Different Sets of Data
208
222
--------------------------------------
209
223
0 commit comments