12
12
namespace Symfony \Component \Form \Tests ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \EventDispatcher \EventDispatcher ;
16
+ use Symfony \Component \Form \Extension \Core \DataMapper \PropertyPathMapper ;
17
+ use Symfony \Component \Form \Form ;
18
+ use Symfony \Component \Form \FormBuilder ;
15
19
use Symfony \Component \Form \FormError ;
16
20
use Symfony \Component \Form \FormFactory ;
17
21
use Symfony \Component \Form \Forms ;
@@ -66,140 +70,136 @@ public function methodProvider()
66
70
*/
67
71
public function testSubmitIfNameInRequest ($ method )
68
72
{
69
- $ form = $ this ->getMockForm ('param1 ' , $ method );
73
+ $ form = $ this ->createForm ('param1 ' , $ method );
70
74
71
75
$ this ->setRequestData ($ method , [
72
76
'param1 ' => 'DATA ' ,
73
77
]);
74
78
75
- $ form ->expects ($ this ->once ())
76
- ->method ('submit ' )
77
- ->with ('DATA ' , 'PATCH ' !== $ method );
78
-
79
79
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
80
+
81
+ $ this ->assertTrue ($ form ->isSubmitted ());
82
+ $ this ->assertSame ('DATA ' , $ form ->getData ());
80
83
}
81
84
82
85
/**
83
86
* @dataProvider methodProvider
84
87
*/
85
88
public function testDoNotSubmitIfWrongRequestMethod ($ method )
86
89
{
87
- $ form = $ this ->getMockForm ('param1 ' , $ method );
90
+ $ form = $ this ->createForm ('param1 ' , $ method );
88
91
89
92
$ otherMethod = 'POST ' === $ method ? 'PUT ' : 'POST ' ;
90
93
91
94
$ this ->setRequestData ($ otherMethod , [
92
95
'param1 ' => 'DATA ' ,
93
96
]);
94
97
95
- $ form ->expects ($ this ->never ())
96
- ->method ('submit ' );
97
-
98
98
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
99
+
100
+ $ this ->assertFalse ($ form ->isSubmitted ());
99
101
}
100
102
101
103
/**
102
104
* @dataProvider methodExceptGetProvider
103
105
*/
104
106
public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest ($ method )
105
107
{
106
- $ form = $ this ->getMockForm ('param1 ' , $ method , false );
108
+ $ form = $ this ->createForm ('param1 ' , $ method , false );
107
109
108
110
$ this ->setRequestData ($ method , [
109
111
'paramx ' => [],
110
112
]);
111
113
112
- $ form ->expects ($ this ->never ())
113
- ->method ('submit ' );
114
-
115
114
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
115
+
116
+ $ this ->assertFalse ($ form ->isSubmitted ());
116
117
}
117
118
118
119
/**
119
120
* @dataProvider methodExceptGetProvider
120
121
*/
121
122
public function testDoNotSubmitCompoundFormIfNameNotInRequestAndNotGetRequest ($ method )
122
123
{
123
- $ form = $ this ->getMockForm ('param1 ' , $ method , true );
124
+ $ form = $ this ->createForm ('param1 ' , $ method , true );
124
125
125
126
$ this ->setRequestData ($ method , [
126
127
'paramx ' => [],
127
128
]);
128
129
129
- $ form ->expects ($ this ->never ())
130
- ->method ('submit ' );
131
-
132
130
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
131
+
132
+ $ this ->assertFalse ($ form ->isSubmitted ());
133
133
}
134
134
135
135
public function testDoNotSubmitIfNameNotInRequestAndGetRequest ()
136
136
{
137
- $ form = $ this ->getMockForm ('param1 ' , 'GET ' );
137
+ $ form = $ this ->createForm ('param1 ' , 'GET ' );
138
138
139
139
$ this ->setRequestData ('GET ' , [
140
140
'paramx ' => [],
141
141
]);
142
142
143
- $ form ->expects ($ this ->never ())
144
- ->method ('submit ' );
145
-
146
143
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
144
+
145
+ $ this ->assertFalse ($ form ->isSubmitted ());
147
146
}
148
147
149
148
/**
150
149
* @dataProvider methodProvider
151
150
*/
152
151
public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest ($ method )
153
152
{
154
- $ form = $ this ->getMockForm ('' , $ method );
155
- $ form ->expects ($ this ->any ())
156
- ->method ('all ' )
157
- ->will ($ this ->returnValue ([
158
- 'param1 ' => $ this ->getMockForm ('param1 ' ),
159
- 'param2 ' => $ this ->getMockForm ('param2 ' ),
160
- ]));
153
+ $ form = $ this ->createForm ('' , $ method , true );
154
+ $ form ->add ($ this ->createForm ('param1 ' ));
155
+ $ form ->add ($ this ->createForm ('param2 ' ));
161
156
162
157
$ this ->setRequestData ($ method , $ requestData = [
163
158
'param1 ' => 'submitted value ' ,
164
159
'paramx ' => 'submitted value ' ,
165
160
]);
166
161
167
- $ form ->expects ($ this ->once ())
168
- ->method ('submit ' )
169
- ->with ($ requestData , 'PATCH ' !== $ method );
170
-
171
162
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
163
+
164
+ $ this ->assertTrue ($ form ->isSubmitted ());
165
+ $ this ->assertTrue ($ form ->get ('param1 ' )->isSubmitted ());
166
+ $ this ->assertSame ('submitted value ' , $ form ->get ('param1 ' )->getData ());
167
+
168
+ if ('PATCH ' === $ method ) {
169
+ $ this ->assertFalse ($ form ->get ('param2 ' )->isSubmitted ());
170
+ } else {
171
+ $ this ->assertTrue ($ form ->get ('param2 ' )->isSubmitted ());
172
+ }
173
+
174
+ $ this ->assertNull ($ form ->get ('param2 ' )->getData ());
172
175
}
173
176
174
177
/**
175
178
* @dataProvider methodProvider
176
179
*/
177
180
public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest ($ method )
178
181
{
179
- $ form = $ this ->getMockForm ('' , $ method );
180
- $ form ->expects ($ this ->any ())
181
- ->method ('all ' )
182
- ->will ($ this ->returnValue ([
183
- 'param1 ' => $ this ->getMockForm ('param1 ' ),
184
- 'param2 ' => $ this ->getMockForm ('param2 ' ),
185
- ]));
182
+ $ form = $ this ->createForm ('' , $ method , true );
183
+ $ form ->add ($ this ->createForm ('param1 ' ));
184
+ $ form ->add ($ this ->createForm ('param2 ' ));
186
185
187
186
$ this ->setRequestData ($ method , [
188
187
'paramx ' => 'submitted value ' ,
189
188
]);
190
189
191
- $ form ->expects ($ this ->never ())
192
- ->method ('submit ' );
193
-
194
190
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
191
+
192
+ $ this ->assertFalse ($ form ->isSubmitted ());
195
193
}
196
194
197
195
/**
198
196
* @dataProvider methodExceptGetProvider
199
197
*/
200
198
public function testMergeParamsAndFiles ($ method )
201
199
{
202
- $ form = $ this ->getMockForm ('param1 ' , $ method );
200
+ $ form = $ this ->createForm ('param1 ' , $ method , true );
201
+ $ form ->add ($ this ->createForm ('field1 ' ));
202
+ $ form ->add ($ this ->createBuilder ('field2 ' , false , ['allow_file_upload ' => true ])->getForm ());
203
203
$ file = $ this ->getMockFile ();
204
204
205
205
$ this ->setRequestData ($ method , [
@@ -212,22 +212,19 @@ public function testMergeParamsAndFiles($method)
212
212
],
213
213
]);
214
214
215
- $ form ->expects ($ this ->once ())
216
- ->method ('submit ' )
217
- ->with ([
218
- 'field1 ' => 'DATA ' ,
219
- 'field2 ' => $ file ,
220
- ], 'PATCH ' !== $ method );
221
-
222
215
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
216
+
217
+ $ this ->assertTrue ($ form ->isSubmitted ());
218
+ $ this ->assertSame ('DATA ' , $ form ->get ('field1 ' )->getData ());
219
+ $ this ->assertSame ($ file , $ form ->get ('field2 ' )->getData ());
223
220
}
224
221
225
222
/**
226
223
* @dataProvider methodExceptGetProvider
227
224
*/
228
225
public function testParamTakesPrecedenceOverFile ($ method )
229
226
{
230
- $ form = $ this ->getMockForm ('param1 ' , $ method );
227
+ $ form = $ this ->createForm ('param1 ' , $ method );
231
228
$ file = $ this ->getMockFile ();
232
229
233
230
$ this ->setRequestData ($ method , [
@@ -236,19 +233,20 @@ public function testParamTakesPrecedenceOverFile($method)
236
233
'param1 ' => $ file ,
237
234
]);
238
235
239
- $ form ->expects ($ this ->once ())
240
- ->method ('submit ' )
241
- ->with ('DATA ' , 'PATCH ' !== $ method );
242
-
243
236
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
237
+
238
+ $ this ->assertTrue ($ form ->isSubmitted ());
239
+ $ this ->assertSame ('DATA ' , $ form ->getData ());
244
240
}
245
241
246
242
/**
247
243
* @dataProvider methodExceptGetProvider
248
244
*/
249
245
public function testSubmitFileIfNoParam ($ method )
250
246
{
251
- $ form = $ this ->getMockForm ('param1 ' , $ method );
247
+ $ form = $ this ->createBuilder ('param1 ' , false , ['allow_file_upload ' => true ])
248
+ ->setMethod ($ method )
249
+ ->getForm ();
252
250
$ file = $ this ->getMockFile ();
253
251
254
252
$ this ->setRequestData ($ method , [
@@ -257,19 +255,20 @@ public function testSubmitFileIfNoParam($method)
257
255
'param1 ' => $ file ,
258
256
]);
259
257
260
- $ form ->expects ($ this ->once ())
261
- ->method ('submit ' )
262
- ->with ($ file , 'PATCH ' !== $ method );
263
-
264
258
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
259
+
260
+ $ this ->assertTrue ($ form ->isSubmitted ());
261
+ $ this ->assertSame ($ file , $ form ->getData ());
265
262
}
266
263
267
264
/**
268
265
* @dataProvider methodExceptGetProvider
269
266
*/
270
267
public function testSubmitMultipleFiles ($ method )
271
268
{
272
- $ form = $ this ->getMockForm ('param1 ' , $ method );
269
+ $ form = $ this ->createBuilder ('param1 ' , false , ['allow_file_upload ' => true ])
270
+ ->setMethod ($ method )
271
+ ->getForm ();
273
272
$ file = $ this ->getMockFile ();
274
273
275
274
$ this ->setRequestData ($ method , [
@@ -280,32 +279,10 @@ public function testSubmitMultipleFiles($method)
280
279
'param3 ' => $ this ->getMockFile ('3 ' ),
281
280
]);
282
281
283
- $ form ->expects ($ this ->once ())
284
- ->method ('submit ' )
285
- ->with ($ file , 'PATCH ' !== $ method );
286
-
287
282
$ this ->requestHandler ->handleRequest ($ form , $ this ->request );
288
- }
289
-
290
- /**
291
- * @dataProvider methodExceptGetProvider
292
- */
293
- public function testSubmitFileWithNamelessForm ($ method )
294
- {
295
- $ form = $ this ->getMockForm (null , $ method );
296
- $ file = $ this ->getMockFile ();
297
-
298
- $ this ->setRequestData ($ method , [
299
- '' => null ,
300
- ], [
301
- '' => $ file ,
302
- ]);
303
-
304
- $ form ->expects ($ this ->once ())
305
- ->method ('submit ' )
306
- ->with ($ file , 'PATCH ' !== $ method );
307
283
308
- $ this ->requestHandler ->handleRequest ($ form , $ this ->request );
284
+ $ this ->assertTrue ($ form ->isSubmitted ());
285
+ $ this ->assertSame ($ file , $ form ->getData ());
309
286
}
310
287
311
288
/**
@@ -371,24 +348,26 @@ abstract protected function getMockFile($suffix = '');
371
348
372
349
abstract protected function getInvalidFile ();
373
350
374
- protected function getMockForm ($ name , $ method = null , $ compound = true )
351
+ protected function createForm ($ name , $ method = null , $ compound = false )
352
+ {
353
+ $ config = $ this ->createBuilder ($ name , $ compound );
354
+
355
+ if (null !== $ method ) {
356
+ $ config ->setMethod ($ method );
357
+ }
358
+
359
+ return new Form ($ config );
360
+ }
361
+
362
+ protected function createBuilder ($ name , $ compound = false , array $ options = [])
375
363
{
376
- $ config = $ this ->getMockBuilder ('Symfony\Component\Form\FormConfigInterface ' )->getMock ();
377
- $ config ->expects ($ this ->any ())
378
- ->method ('getMethod ' )
379
- ->will ($ this ->returnValue ($ method ));
380
- $ config ->expects ($ this ->any ())
381
- ->method ('getCompound ' )
382
- ->will ($ this ->returnValue ($ compound ));
383
-
384
- $ form = $ this ->getMockBuilder ('Symfony\Component\Form\Test\FormInterface ' )->getMock ();
385
- $ form ->expects ($ this ->any ())
386
- ->method ('getName ' )
387
- ->will ($ this ->returnValue ($ name ));
388
- $ form ->expects ($ this ->any ())
389
- ->method ('getConfig ' )
390
- ->will ($ this ->returnValue ($ config ));
391
-
392
- return $ form ;
364
+ $ builder = new FormBuilder ($ name , null , new EventDispatcher (), $ this ->getMockBuilder ('Symfony\Component\Form\FormFactoryInterface ' )->getMock (), $ options );
365
+ $ builder ->setCompound ($ compound );
366
+
367
+ if ($ compound ) {
368
+ $ builder ->setDataMapper (new PropertyPathMapper ());
369
+ }
370
+
371
+ return $ builder ;
393
372
}
394
373
}
0 commit comments