Skip to content

Commit 3bd6379

Browse files
committed
Add docs about the validator TypeTestCase class
1 parent 819495c commit 3bd6379

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,30 +192,25 @@ 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\Forms;
178196
use Symfony\Component\Form\FormBuilder;
179197
use Symfony\Component\Form\Test\TypeTestCase;
180-
use Symfony\Component\Validator\ConstraintViolationList;
181-
use Symfony\Component\Validator\Validator\ValidatorInterface;
182198

183199
class TestedTypeTest extends TypeTestCase
184200
{
185201
protected function getExtensions()
186202
{
187-
$validator = $this->createMock(ValidatorInterface::class);
188-
$validator
189-
->method('validate')
190-
->will($this->returnValue(new ConstraintViolationList()));
191-
192203
return array(
193-
new ValidatorExtension($validator),
204+
new MyFormExtension(),
194205
);
195206
}
196207

197208
// ... your tests
198209
}
199210

211+
It is also possible to load custom form types, form type extensions or type guessers using the
212+
``getTypedExtensions``, ``getTypes`` and ``getTypeGuessers`` methods.
213+
200214
Testing against Different Sets of Data
201215
--------------------------------------
202216

0 commit comments

Comments
 (0)