Skip to content

Commit 3e6dc19

Browse files
committed
Add docs about the validator TypeTestCase class
1 parent 8bf4ed6 commit 3e6dc19

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

form/unit_testing.rst

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,31 @@ before creating the parent form using the ``PreloadedExtension`` class::
157157
be getting errors that are not related to the form you are currently
158158
testing but to its children.
159159

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+
160180
Adding Custom Extensions
161181
------------------------
162182

163183
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>`.
166185
The ``TypeTestCase`` only loads the core form extension, which means an
167186
:class:`Symfony\\Component\\OptionsResolver\\Exception\\InvalidOptionsException`
168187
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::
173192
namespace AppBundle\Tests\Form\Type;
174193

175194
use AppBundle\Form\Type\TestedType;
176-
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
177195
use Symfony\Component\Form\Form;
178196
use Symfony\Component\Form\Forms;
179197
use Symfony\Component\Form\FormBuilder;
180198
use Symfony\Component\Form\Test\TypeTestCase;
181-
use Symfony\Component\Validator\ConstraintViolationList;
182199
use Symfony\Component\Validator\Mapping\ClassMetadata;
183-
use Symfony\Component\Validator\Validator\ValidatorInterface;
184200

185201
class TestedTypeTest extends TypeTestCase
186202
{
187203
protected function getExtensions()
188204
{
189-
$validator = $this->createMock(ValidatorInterface::class);
190205
// use getMock() on PHPUnit 5.3 or below
191206
// $validator = $this->getMock(ValidatorInterface::class);
192-
$validator
193-
->method('validate')
194-
->will($this->returnValue(new ConstraintViolationList()));
195207
$validator
196208
->method('getMetadataFor')
197209
->will($this->returnValue(new ClassMetadata(Form::class)));
198-
199210
return array(
200-
new ValidatorExtension($validator),
211+
new MyFormExtension(),
201212
);
202213
}
203214

204215
// ... your tests
205216
}
206217

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+
207221
Testing against Different Sets of Data
208222
--------------------------------------
209223

0 commit comments

Comments
 (0)