Skip to content

Commit 872488d

Browse files
xabbuhnicolas-grekas
authored andcommitted
[Form] ensure compatibility with older PHPUnit mocks
1 parent b24a67f commit 872488d

14 files changed

+250
-298
lines changed

Form.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,11 @@ public function submit($submittedData, $clearMissing = true)
532532
$submittedData = null;
533533
} elseif (is_scalar($submittedData)) {
534534
$submittedData = (string) $submittedData;
535-
} elseif (!$this->config->getOption('allow_file_upload') && $this->config->getRequestHandler()->isFileUpload($submittedData)) {
536-
$submittedData = null;
537-
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
535+
} elseif ($this->config->getRequestHandler()->isFileUpload($submittedData)) {
536+
if (!$this->config->getOption('allow_file_upload')) {
537+
$submittedData = null;
538+
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, file upload given.');
539+
}
538540
} elseif (\is_array($submittedData) && !$this->config->getCompound() && !$this->config->hasOption('multiple')) {
539541
$submittedData = null;
540542
$this->transformationFailure = new TransformationFailedException('Submitted data was expected to be text or number, array given.');

Tests/AbstractFormTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,6 @@ protected function getBuilder($name = 'name', EventDispatcherInterface $dispatch
6565
return new FormBuilder($name, $dataClass, $dispatcher ?: $this->dispatcher, $this->factory, $options);
6666
}
6767

68-
/**
69-
* @param string $name
70-
*
71-
* @return \PHPUnit_Framework_MockObject_MockObject
72-
*/
73-
protected function getMockForm($name = 'name')
74-
{
75-
$form = $this->getMockBuilder('Symfony\Component\Form\Test\FormInterface')->getMock();
76-
$config = $this->getMockBuilder('Symfony\Component\Form\FormConfigInterface')->getMock();
77-
78-
$form->expects($this->any())
79-
->method('getName')
80-
->will($this->returnValue($name));
81-
$form->expects($this->any())
82-
->method('getConfig')
83-
->will($this->returnValue($config));
84-
85-
return $form;
86-
}
87-
8868
/**
8969
* @return \PHPUnit_Framework_MockObject_MockObject
9070
*/

Tests/AbstractRequestHandlerTest.php

Lines changed: 82 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
namespace Symfony\Component\Form\Tests;
1313

1414
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;
1519
use Symfony\Component\Form\FormError;
1620
use Symfony\Component\Form\FormFactory;
1721
use Symfony\Component\Form\Forms;
@@ -66,140 +70,136 @@ public function methodProvider()
6670
*/
6771
public function testSubmitIfNameInRequest($method)
6872
{
69-
$form = $this->getMockForm('param1', $method);
73+
$form = $this->createForm('param1', $method);
7074

7175
$this->setRequestData($method, [
7276
'param1' => 'DATA',
7377
]);
7478

75-
$form->expects($this->once())
76-
->method('submit')
77-
->with('DATA', 'PATCH' !== $method);
78-
7979
$this->requestHandler->handleRequest($form, $this->request);
80+
81+
$this->assertTrue($form->isSubmitted());
82+
$this->assertSame('DATA', $form->getData());
8083
}
8184

8285
/**
8386
* @dataProvider methodProvider
8487
*/
8588
public function testDoNotSubmitIfWrongRequestMethod($method)
8689
{
87-
$form = $this->getMockForm('param1', $method);
90+
$form = $this->createForm('param1', $method);
8891

8992
$otherMethod = 'POST' === $method ? 'PUT' : 'POST';
9093

9194
$this->setRequestData($otherMethod, [
9295
'param1' => 'DATA',
9396
]);
9497

95-
$form->expects($this->never())
96-
->method('submit');
97-
9898
$this->requestHandler->handleRequest($form, $this->request);
99+
100+
$this->assertFalse($form->isSubmitted());
99101
}
100102

101103
/**
102104
* @dataProvider methodExceptGetProvider
103105
*/
104106
public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($method)
105107
{
106-
$form = $this->getMockForm('param1', $method, false);
108+
$form = $this->createForm('param1', $method, false);
107109

108110
$this->setRequestData($method, [
109111
'paramx' => [],
110112
]);
111113

112-
$form->expects($this->never())
113-
->method('submit');
114-
115114
$this->requestHandler->handleRequest($form, $this->request);
115+
116+
$this->assertFalse($form->isSubmitted());
116117
}
117118

118119
/**
119120
* @dataProvider methodExceptGetProvider
120121
*/
121122
public function testDoNotSubmitCompoundFormIfNameNotInRequestAndNotGetRequest($method)
122123
{
123-
$form = $this->getMockForm('param1', $method, true);
124+
$form = $this->createForm('param1', $method, true);
124125

125126
$this->setRequestData($method, [
126127
'paramx' => [],
127128
]);
128129

129-
$form->expects($this->never())
130-
->method('submit');
131-
132130
$this->requestHandler->handleRequest($form, $this->request);
131+
132+
$this->assertFalse($form->isSubmitted());
133133
}
134134

135135
public function testDoNotSubmitIfNameNotInRequestAndGetRequest()
136136
{
137-
$form = $this->getMockForm('param1', 'GET');
137+
$form = $this->createForm('param1', 'GET');
138138

139139
$this->setRequestData('GET', [
140140
'paramx' => [],
141141
]);
142142

143-
$form->expects($this->never())
144-
->method('submit');
145-
146143
$this->requestHandler->handleRequest($form, $this->request);
144+
145+
$this->assertFalse($form->isSubmitted());
147146
}
148147

149148
/**
150149
* @dataProvider methodProvider
151150
*/
152151
public function testSubmitFormWithEmptyNameIfAtLeastOneFieldInRequest($method)
153152
{
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'));
161156

162157
$this->setRequestData($method, $requestData = [
163158
'param1' => 'submitted value',
164159
'paramx' => 'submitted value',
165160
]);
166161

167-
$form->expects($this->once())
168-
->method('submit')
169-
->with($requestData, 'PATCH' !== $method);
170-
171162
$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());
172175
}
173176

174177
/**
175178
* @dataProvider methodProvider
176179
*/
177180
public function testDoNotSubmitFormWithEmptyNameIfNoFieldInRequest($method)
178181
{
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'));
186185

187186
$this->setRequestData($method, [
188187
'paramx' => 'submitted value',
189188
]);
190189

191-
$form->expects($this->never())
192-
->method('submit');
193-
194190
$this->requestHandler->handleRequest($form, $this->request);
191+
192+
$this->assertFalse($form->isSubmitted());
195193
}
196194

197195
/**
198196
* @dataProvider methodExceptGetProvider
199197
*/
200198
public function testMergeParamsAndFiles($method)
201199
{
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());
203203
$file = $this->getMockFile();
204204

205205
$this->setRequestData($method, [
@@ -212,22 +212,19 @@ public function testMergeParamsAndFiles($method)
212212
],
213213
]);
214214

215-
$form->expects($this->once())
216-
->method('submit')
217-
->with([
218-
'field1' => 'DATA',
219-
'field2' => $file,
220-
], 'PATCH' !== $method);
221-
222215
$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());
223220
}
224221

225222
/**
226223
* @dataProvider methodExceptGetProvider
227224
*/
228225
public function testParamTakesPrecedenceOverFile($method)
229226
{
230-
$form = $this->getMockForm('param1', $method);
227+
$form = $this->createForm('param1', $method);
231228
$file = $this->getMockFile();
232229

233230
$this->setRequestData($method, [
@@ -236,19 +233,20 @@ public function testParamTakesPrecedenceOverFile($method)
236233
'param1' => $file,
237234
]);
238235

239-
$form->expects($this->once())
240-
->method('submit')
241-
->with('DATA', 'PATCH' !== $method);
242-
243236
$this->requestHandler->handleRequest($form, $this->request);
237+
238+
$this->assertTrue($form->isSubmitted());
239+
$this->assertSame('DATA', $form->getData());
244240
}
245241

246242
/**
247243
* @dataProvider methodExceptGetProvider
248244
*/
249245
public function testSubmitFileIfNoParam($method)
250246
{
251-
$form = $this->getMockForm('param1', $method);
247+
$form = $this->createBuilder('param1', false, ['allow_file_upload' => true])
248+
->setMethod($method)
249+
->getForm();
252250
$file = $this->getMockFile();
253251

254252
$this->setRequestData($method, [
@@ -257,19 +255,20 @@ public function testSubmitFileIfNoParam($method)
257255
'param1' => $file,
258256
]);
259257

260-
$form->expects($this->once())
261-
->method('submit')
262-
->with($file, 'PATCH' !== $method);
263-
264258
$this->requestHandler->handleRequest($form, $this->request);
259+
260+
$this->assertTrue($form->isSubmitted());
261+
$this->assertSame($file, $form->getData());
265262
}
266263

267264
/**
268265
* @dataProvider methodExceptGetProvider
269266
*/
270267
public function testSubmitMultipleFiles($method)
271268
{
272-
$form = $this->getMockForm('param1', $method);
269+
$form = $this->createBuilder('param1', false, ['allow_file_upload' => true])
270+
->setMethod($method)
271+
->getForm();
273272
$file = $this->getMockFile();
274273

275274
$this->setRequestData($method, [
@@ -280,32 +279,10 @@ public function testSubmitMultipleFiles($method)
280279
'param3' => $this->getMockFile('3'),
281280
]);
282281

283-
$form->expects($this->once())
284-
->method('submit')
285-
->with($file, 'PATCH' !== $method);
286-
287282
$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);
307283

308-
$this->requestHandler->handleRequest($form, $this->request);
284+
$this->assertTrue($form->isSubmitted());
285+
$this->assertSame($file, $form->getData());
309286
}
310287

311288
/**
@@ -371,24 +348,26 @@ abstract protected function getMockFile($suffix = '');
371348

372349
abstract protected function getInvalidFile();
373350

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 = [])
375363
{
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;
393372
}
394373
}

0 commit comments

Comments
 (0)