@@ -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,35 @@ 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()
82
+ ;
74
83
75
- foreach (array_keys($formData) as $key) {
76
- $this->assertArrayHasKey($key, $children);
77
- }
84
+ $this->assertArrayHasKey('custom_var', $view->vars);
85
+ $this->assertSame('expected value', $view->vars['custom_var']);
78
86
}
79
87
}
80
88
@@ -84,7 +92,7 @@ First you verify if the ``FormType`` compiles. This includes basic class
84
92
inheritance, the ``buildForm() `` function and options resolution. This should
85
93
be the first test you write::
86
94
87
- $form = $this->factory->create(TestedType::class, $objectToCompare );
95
+ $form = $this->factory->create(TestedType::class, $formData );
88
96
89
97
This test checks that none of your data transformers used by the form
90
98
failed. The :method: `Symfony\\ Component\\ Form\\ FormInterface::isSynchronized `
@@ -97,30 +105,38 @@ method is only set to ``false`` if a data transformer throws an exception::
97
105
98
106
Don't test the validation: it is applied by a listener that is not
99
107
active in the test case and it relies on validation configuration.
100
- Instead, unit test your custom constraints directly.
108
+ Instead, unit test your custom constraints directly or read how
109
+ to :ref: `add custom extensions <form_unit_testing-adding_custom_extensions >`
110
+ in the last section of this page.
101
111
102
- Next, verify the submission and mapping of the form. The test below
103
- checks if all the fields are correctly specified::
112
+ Next, verify the submission and mapping of the form. The test below checks if
113
+ all the fields are correctly specified::
104
114
105
- $this->assertEquals($object , $objectToCompare );
115
+ $this->assertEquals($expected , $formData );
106
116
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 ::
117
+ Finally, check the creation of the ``FormView ``. You can check that a custom
118
+ variable exists and will be available in your form themes ::
109
119
110
- $view = $form->createView();
111
- $children = $view->children;
112
-
113
- foreach (array_keys($formData) as $key) {
114
- $this->assertArrayHasKey($key, $children);
115
- }
120
+ $this->assertArrayHasKey('custom_var', $view->vars);
121
+ $this->assertSame('expected value', $view->vars['custom_var']);
116
122
117
123
.. tip ::
118
124
119
125
Use :ref: `PHPUnit data providers <testing-data-providers >` to test multiple
120
126
form conditions using the same test code.
121
127
122
- Testings Types from the Service Container
123
- -----------------------------------------
128
+ .. caution ::
129
+
130
+ When your type relies on the ``EntityType ``, you should register the
131
+ :class: `Symfony\\ Bridge\\ Doctrine\\ Form\\ DoctrineOrmExtension `, which will
132
+ need to mock the ``ManagerRegistry ``.
133
+
134
+ However, If you cannot use a mock to write your test, you should extend
135
+ the ``KernelTestCase `` instead and use the ``form.factory `` service to
136
+ create the form.
137
+
138
+ Testings Types Registered as Services
139
+ -------------------------------------
124
140
125
141
Your form may be used as a service, as it depends on other services (e.g. the
126
142
Doctrine entity manager). In these cases, using the above code won't work, as
@@ -165,14 +181,18 @@ make sure the ``FormRegistry`` uses the created instance::
165
181
166
182
public function testSubmitValidData()
167
183
{
184
+ // ...
185
+
168
186
// Instead of creating a new instance, the one created in
169
187
// getExtensions() will be used.
170
- $form = $this->factory->create(TestedType::class);
188
+ $form = $this->factory->create(TestedType::class, $formData );
171
189
172
190
// ... your test
173
191
}
174
192
}
175
193
194
+ .. _form_unit_testing-adding_custom_extensions :
195
+
176
196
Adding Custom Extensions
177
197
------------------------
178
198
@@ -211,6 +231,13 @@ allows you to return a list of extensions to register::
211
231
// ... your tests
212
232
}
213
233
234
+ .. note ::
235
+
236
+ By Default only the
237
+ :class: `Symfony\\ Component\\ Form\\ Extension\\ Core\\ CoreExtension ` is
238
+ registered in tests. You can find other extensions from the Form component
239
+ in the ``Symfony\Component\Form\Extension `` namespace.
240
+
214
241
It is also possible to load custom form types, form type extensions or type
215
242
guessers using the :method: `Symfony\\ Component\\ Form\\ Test\\ FormIntegrationTestCase::getTypes `,
216
243
:method: `Symfony\\ Component\\ Form\\ Test\\ FormIntegrationTestCase::getTypeExtensions `
0 commit comments