@@ -52,8 +52,7 @@ The simplest ``TypeTestCase`` implementation looks like the following::
52
52
'test2' => 'test2',
53
53
);
54
54
55
- $type = TestedType::class;
56
- $form = $this->factory->create($type);
55
+ $form = $this->factory->create(TestedType::class);
57
56
58
57
$object = TestObject::fromArray($formData);
59
58
@@ -78,8 +77,7 @@ First you verify if the ``FormType`` compiles. This includes basic class
78
77
inheritance, the ``buildForm `` function and options resolution. This should
79
78
be the first test you write::
80
79
81
- $type = TestedType::class;
82
- $form = $this->factory->create($type);
80
+ $form = $this->factory->create(TestedType::class);
83
81
84
82
This test checks that none of your data transformers used by the form
85
83
failed. The :method: `Symfony\\ Component\\ Form\\ FormInterface::isSynchronized `
@@ -109,55 +107,55 @@ widgets you want to display are available in the children property::
109
107
$this->assertArrayHasKey($key, $children);
110
108
}
111
109
112
- Adding a Type your Form Depends on
113
- ----------------------------------
110
+ Testings Types from the Service Container
111
+ -----------------------------------------
114
112
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.
117
117
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::
126
121
127
122
// src/AppBundle/Tests/Form/Type/TestedTypeTests.php
128
123
namespace AppBundle\Tests\Form\Type;
129
124
130
- use AppBundle\Form\Type\TestedType;
131
- use AppBundle\Model\TestObject;
132
- use Symfony\Component\Form\Test\TypeTestCase;
133
125
use Symfony\Component\Form\PreloadedExtension;
126
+ // ...
134
127
135
128
class TestedTypeTest extends TypeTestCase
136
129
{
130
+ private $entityManager;
131
+
132
+ protected function setUp()
133
+ {
134
+ // mock any dependencies
135
+ $this->entityManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
136
+ }
137
+
137
138
protected function getExtensions()
138
139
{
139
- $childType = TestChildType::class;
140
+ // create a type instance with the mocked dependencies
141
+ $type = new TestedType($this->entityManager);
140
142
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
+ );
144
147
}
145
148
146
149
public function testSubmitValidData()
147
150
{
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);
150
154
151
155
// ... your test
152
156
}
153
157
}
154
158
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
-
161
159
Adding Custom Extensions
162
160
------------------------
163
161
@@ -173,23 +171,25 @@ allows you to return a list of extensions to register::
173
171
// src/AppBundle/Tests/Form/Type/TestedTypeTests.php
174
172
namespace AppBundle\Tests\Form\Type;
175
173
176
- use AppBundle\Form\Type\TestedType;
177
- use AppBundle\Model\TestObject;
174
+ // ...
178
175
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;
182
176
use Symfony\Component\Validator\ConstraintViolationList;
183
177
184
178
class TestedTypeTest extends TypeTestCase
185
179
{
180
+ private $validator;
181
+
186
182
protected function getExtensions()
187
183
{
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()));
190
190
191
191
return array(
192
- new ValidatorExtension($validator),
192
+ new ValidatorExtension($this-> validator),
193
193
);
194
194
}
195
195
@@ -211,7 +211,6 @@ a good opportunity to use them::
211
211
212
212
class TestedTypeTest extends TypeTestCase
213
213
{
214
-
215
214
/**
216
215
* @dataProvider getValidTestData
217
216
*/
0 commit comments