Skip to content

Commit 906d55a

Browse files
committed
Update Testing Form Types article for 2.8 refactorings
1 parent 53febf0 commit 906d55a

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

Diff for: cookbook/form/unit_testing.rst

+39-40
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ The simplest ``TypeTestCase`` implementation looks like the following::
5252
'test2' => 'test2',
5353
);
5454

55-
$type = TestedType::class;
56-
$form = $this->factory->create($type);
55+
$form = $this->factory->create(TestedType::class);
5756

5857
$object = TestObject::fromArray($formData);
5958

@@ -78,8 +77,7 @@ First you verify if the ``FormType`` compiles. This includes basic class
7877
inheritance, the ``buildForm`` function and options resolution. This should
7978
be the first test you write::
8079

81-
$type = TestedType::class;
82-
$form = $this->factory->create($type);
80+
$form = $this->factory->create(TestedType::class);
8381

8482
This test checks that none of your data transformers used by the form
8583
failed. The :method:`Symfony\\Component\\Form\\FormInterface::isSynchronized`
@@ -109,55 +107,55 @@ widgets you want to display are available in the children property::
109107
$this->assertArrayHasKey($key, $children);
110108
}
111109

112-
Adding a Type your Form Depends on
113-
----------------------------------
110+
Testings Types from the Service Container
111+
-----------------------------------------
114112

115-
Your form may depend on other types that are defined as services. It
116-
might look like this::
113+
Your form may be used as a service, as it depends on other services (e.g. the
114+
Doctrine entity manager). In these cases, using the above code won't work, as
115+
the Form component just instantiates the form type without passing any
116+
arguments to the constructor.
117117

118-
// src/AppBundle/Form/Type/TestedType.php
119-
120-
// ... the buildForm method
121-
$builder->add('app_test_child_type');
122-
123-
To create your form correctly, you need to make the type available to the
124-
form factory in your test. The easiest way is to register it manually
125-
before creating the parent form using the ``PreloadedExtension`` class::
118+
To solve this, you have to mock the injected dependencies, instantiate your own
119+
form type and use the :class:`Symfony\\Component\\Form\\PreloadedExtension` to
120+
make sure the ``FormRegistry`` uses the created instance::
126121

127122
// src/AppBundle/Tests/Form/Type/TestedTypeTests.php
128123
namespace AppBundle\Tests\Form\Type;
129124

130-
use AppBundle\Form\Type\TestedType;
131-
use AppBundle\Model\TestObject;
132-
use Symfony\Component\Form\Test\TypeTestCase;
133125
use Symfony\Component\Form\PreloadedExtension;
126+
// ...
134127

135128
class TestedTypeTest extends TypeTestCase
136129
{
130+
private $entityManager;
131+
132+
protected function setUp()
133+
{
134+
// mock any dependencies
135+
$this->entityManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
136+
}
137+
137138
protected function getExtensions()
138139
{
139-
$childType = TestChildType::class;
140+
// create a type instance with the mocked dependencies
141+
$type = new TestedType($this->entityManager);
140142

141-
return array(new PreloadedExtension(array(
142-
$childType->getName() => $childType,
143-
), array()));
143+
return array(
144+
// register the type instances with the PreloadedExtension
145+
new PreloadedExtension(array($type), array()),
146+
);
144147
}
145148

146149
public function testSubmitValidData()
147150
{
148-
$type = TestedType::class;
149-
$form = $this->factory->create($type);
151+
// Instead of creating a new instance, the one created in
152+
// getExtensions() will be used.
153+
$form = $this->factory->create(TestedType::class);
150154

151155
// ... your test
152156
}
153157
}
154158

155-
.. caution::
156-
157-
Make sure the child type you add is well tested. Otherwise you may
158-
be getting errors that are not related to the form you are currently
159-
testing but to its children.
160-
161159
Adding Custom Extensions
162160
------------------------
163161

@@ -173,23 +171,25 @@ allows you to return a list of extensions to register::
173171
// src/AppBundle/Tests/Form/Type/TestedTypeTests.php
174172
namespace AppBundle\Tests\Form\Type;
175173

176-
use AppBundle\Form\Type\TestedType;
177-
use AppBundle\Model\TestObject;
174+
// ...
178175
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
179-
use Symfony\Component\Form\Forms;
180-
use Symfony\Component\Form\FormBuilder;
181-
use Symfony\Component\Form\Test\TypeTestCase;
182176
use Symfony\Component\Validator\ConstraintViolationList;
183177

184178
class TestedTypeTest extends TypeTestCase
185179
{
180+
private $validator;
181+
186182
protected function getExtensions()
187183
{
188-
$validator = $this->getMock('\Symfony\Component\Validator\Validator\ValidatorInterface');
189-
$validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));
184+
$this->validator = $this->getMock(
185+
'Symfony\Component\Validator\Validator\ValidatorInterface'
186+
);
187+
$this->validator
188+
->method('validate')
189+
->will($this->returnValue(new ConstraintViolationList()));
190190

191191
return array(
192-
new ValidatorExtension($validator),
192+
new ValidatorExtension($this->validator),
193193
);
194194
}
195195

@@ -211,7 +211,6 @@ a good opportunity to use them::
211211

212212
class TestedTypeTest extends TypeTestCase
213213
{
214-
215214
/**
216215
* @dataProvider getValidTestData
217216
*/

0 commit comments

Comments
 (0)