@@ -23,9 +23,9 @@ factory but it would be complex. It is better to pass it to FormFactory like it
23
23
is done in a real application. It is simple to bootstrap and you can trust
24
24
the Symfony components enough to use them as a testing base.
25
25
26
- There is already a class that you can benefit from for simple FormTypes
27
- testing: : class: `Symfony\\ Component\\ Form\\ Test\\ TypeTestCase `. It is used to
28
- test the core types and you can use it to test your types too.
26
+ There is already a class that you can benefit from for testing:
27
+ : class: `Symfony\\ Component\\ Form\\ Test\\ TypeTestCase `. It is used to test the
28
+ core types and you can use it to test your types too.
29
29
30
30
.. note ::
31
31
@@ -54,27 +54,34 @@ The simplest ``TypeTestCase`` implementation looks like the following::
54
54
'test2' => 'test2',
55
55
];
56
56
57
- $objectToCompare = new TestObject();
58
- // $objectToCompare will retrieve data from the form submission; pass it as the second argument
59
- $form = $this->factory->create(TestedType::class, $objectToCompare );
57
+ $formData = new TestObject();
58
+ // $formData will retrieve data from the form submission; pass it as the second argument
59
+ $form = $this->factory->create(TestedType::class, $formData );
60
60
61
- $object = new TestObject();
61
+ $expected = new TestObject();
62
62
// ...populate $object properties with the data stored in $formData
63
63
64
64
// submit the data to the form directly
65
65
$form->submit($formData);
66
66
67
+ // This check ensures there are no transformation failures
67
68
$this->assertTrue($form->isSynchronized());
68
69
69
- // check that $objectToCompare was modified as expected when the form was submitted
70
- $this->assertEquals($object, $objectToCompare);
70
+ // check that $formData was modified as expected when the form was submitted
71
+ $this->assertEquals($expected, $formData);
72
+ }
73
+
74
+ public function testCustomFormView()
75
+ {
76
+ $formData = new TestObject();
77
+ // ... prepare the data as you need
71
78
72
- $view = $form->createView();
73
- $children = $view->children;
79
+ // The initial data may be used to compute custom view variables
80
+ $view = $this->factory->create(TestedType::class, $formData)
81
+ ->createView();
74
82
75
- foreach (array_keys($formData) as $key) {
76
- $this->assertArrayHasKey($key, $children);
77
- }
83
+ $this->assertArrayHasKey('custom_var', $view->vars);
84
+ $this->assertSame('expected value', $view->vars['custom_var']);
78
85
}
79
86
}
80
87
@@ -84,7 +91,7 @@ First you verify if the ``FormType`` compiles. This includes basic class
84
91
inheritance, the ``buildForm() `` function and options resolution. This should
85
92
be the first test you write::
86
93
87
- $form = $this->factory->create(TestedType::class, $objectToCompare );
94
+ $form = $this->factory->create(TestedType::class, $formData );
88
95
89
96
This test checks that none of your data transformers used by the form
90
97
failed. The :method: `Symfony\\ Component\\ Form\\ FormInterface::isSynchronized `
@@ -97,30 +104,38 @@ method is only set to ``false`` if a data transformer throws an exception::
97
104
98
105
Don't test the validation: it is applied by a listener that is not
99
106
active in the test case and it relies on validation configuration.
100
- Instead, unit test your custom constraints directly.
107
+ Instead, unit test your custom constraints directly or read how
108
+ to :ref: `add custom extensions <form_unit_testing-adding_custom_extensions >`
109
+ in the last section of this page.
101
110
102
- Next, verify the submission and mapping of the form. The test below
103
- checks if all the fields are correctly specified::
111
+ Next, verify the submission and mapping of the form. The test below checks if
112
+ all the fields are correctly specified::
104
113
105
- $this->assertEquals($object , $objectToCompare );
114
+ $this->assertEquals($expected , $formData );
106
115
107
- Finally, check the creation of the ``FormView ``. You should check if all
108
- widgets you want to display are available in the children property ::
116
+ Finally, check the creation of the ``FormView ``. You can check that a custom
117
+ variable exists and will be available in your form themes ::
109
118
110
- $view = $form->createView();
111
- $children = $view->children;
112
-
113
- foreach (array_keys($formData) as $key) {
114
- $this->assertArrayHasKey($key, $children);
115
- }
119
+ $this->assertArrayHasKey('custom_var', $view->vars);
120
+ $this->assertSame('expected value', $view->vars['custom_var']);
116
121
117
122
.. tip ::
118
123
119
124
Use :ref: `PHPUnit data providers <testing-data-providers >` to test multiple
120
125
form conditions using the same test code.
121
126
122
- Testings Types from the Service Container
123
- -----------------------------------------
127
+ .. caution ::
128
+
129
+ When your type relies on the ``EntityType ``, you should register the
130
+ :class: `Symfony\\ Bridge\\ Doctrine\\ Form\\ DoctrineOrmExtension `, which will
131
+ need to mock the ``ManagerRegistry ``.
132
+
133
+ However, If you cannot use a mock to write your test, you should extend
134
+ the ``KernelTestCase `` instead and use the ``form.factory `` service to
135
+ create the form.
136
+
137
+ Testings Types Registered as Services
138
+ -------------------------------------
124
139
125
140
Your form may be used as a service, as it depends on other services (e.g. the
126
141
Doctrine entity manager). In these cases, using the above code won't work, as
@@ -165,14 +180,18 @@ make sure the ``FormRegistry`` uses the created instance::
165
180
166
181
public function testSubmitValidData()
167
182
{
183
+ // ...
184
+
168
185
// Instead of creating a new instance, the one created in
169
186
// getExtensions() will be used.
170
- $form = $this->factory->create(TestedType::class);
187
+ $form = $this->factory->create(TestedType::class, $formData );
171
188
172
189
// ... your test
173
190
}
174
191
}
175
192
193
+ .. _form_unit_testing-adding_custom_extensions :
194
+
176
195
Adding Custom Extensions
177
196
------------------------
178
197
@@ -211,6 +230,13 @@ allows you to return a list of extensions to register::
211
230
// ... your tests
212
231
}
213
232
233
+ .. note ::
234
+
235
+ By default only the
236
+ :class: `Symfony\\ Component\\ Form\\ Extension\\ Core\\ CoreExtension ` is
237
+ registered in tests. You can find other extensions from the Form component
238
+ in the ``Symfony\Component\Form\Extension `` namespace.
239
+
214
240
It is also possible to load custom form types, form type extensions or type
215
241
guessers using the :method: `Symfony\\ Component\\ Form\\ Test\\ FormIntegrationTestCase::getTypes `,
216
242
:method: `Symfony\\ Component\\ Form\\ Test\\ FormIntegrationTestCase::getTypeExtensions `
0 commit comments