Skip to content

Commit a84aa9f

Browse files
committed
Add docs about the validator TypeTestCase class
1 parent c7af8cf commit a84aa9f

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

form/unit_testing.rst

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,31 @@ make sure the ``FormRegistry`` uses the created instance::
156156
}
157157
}
158158

159+
Forms Using Validation
160+
------------------------------------
161+
162+
If your forms uses the ``invalid_message`` or ``constraints`` option for validation, you need to
163+
register the validation extension which provides this options.
164+
Luckily Symfony provides a custom test class which does this for you.
165+
In order to have this option registered, your test needs to extend from the
166+
:class:`Symfony\\Component\\Form\\Tests\\Extension\\Validator\\Type\\TypeTestCase`
167+
class::
168+
169+
// tests/AppBundle/Form/Type/TestedTypeTests.php
170+
namespace Tests\AppBundle\Form\Type;
171+
172+
use Symfony\Component\Form\Tests\Extension\Validator\Type\TypeTestCase;
173+
174+
class TestedTypeTest extends TypeTestCase
175+
{
176+
// ...
177+
}
178+
159179
Adding Custom Extensions
160180
------------------------
161181

162182
It often happens that you use some options that are added by
163-
:doc:`form extensions </form/create_form_type_extension>`. One of the
164-
cases may be the ``ValidatorExtension`` with its ``invalid_message`` option.
183+
:doc:`form extensions </form/create_form_type_extension>`.
165184
The ``TypeTestCase`` only loads the core form extension, which means an
166185
:class:`Symfony\\Component\\OptionsResolver\\Exception\\InvalidOptionsException`
167186
will be raised if you try to test a class that depends on other extensions.
@@ -173,29 +192,22 @@ allows you to return a list of extensions to register::
173192

174193
// ...
175194
use AppBundle\Form\Type\TestedType;
176-
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
177-
use Symfony\Component\Validator\ConstraintViolationList;
178-
use Symfony\Component\Validator\Validator\ValidatorInterface;
179195

180196
class TestedTypeTest extends TypeTestCase
181197
{
182-
private $validator;
183-
184198
protected function getExtensions()
185199
{
186-
$this->validator = $this->createMock(ValidatorInterface::class);
187-
$this->validator
188-
->method('validate')
189-
->will($this->returnValue(new ConstraintViolationList()));
190-
191200
return array(
192-
new ValidatorExtension($this->validator),
201+
new MyFormExtension(),
193202
);
194203
}
195204

196205
// ... your tests
197206
}
198207

208+
It is also possible to load custom form types, form type extensions or type guessers using the
209+
``getTypedExtensions``, ``getTypes`` and ``getTypeGuessers`` methods.
210+
199211
Testing against Different Sets of Data
200212
--------------------------------------
201213

0 commit comments

Comments
 (0)