Skip to content

Commit 0eb0479

Browse files
committed
[Form] Hardened test suite for empty data
1 parent 783643f commit 0eb0479

18 files changed

+202
-4
lines changed

Tests/Extension/Core/Type/BaseTypeTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
145145
$this->assertSame($view, $form->getViewData());
146146
}
147147

148+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
149+
{
150+
$builder = $this->factory->createBuilder($this->getTestedType());
151+
152+
if ($builder->getCompound()) {
153+
$emptyData = array();
154+
foreach ($builder as $field) {
155+
// empty children should map null (model data) in the compound view data
156+
$emptyData[$field->getName()] = null;
157+
}
158+
} else {
159+
// simple fields share the view and the model format, unless they use a transformer
160+
$expectedData = $emptyData;
161+
}
162+
163+
$form = $builder->setEmptyData($emptyData)->getForm()->submit(null);
164+
165+
$this->assertSame($emptyData, $form->getViewData());
166+
$this->assertSame($expectedData, $form->getNormData());
167+
$this->assertSame($expectedData, $form->getData());
168+
}
169+
148170
protected function getTestedType()
149171
{
150172
return static::TESTED_TYPE;

Tests/Extension/Core/Type/ButtonTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,16 @@ public function testCreateButtonInstances()
3232
{
3333
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create(static::TESTED_TYPE));
3434
}
35+
36+
/**
37+
* @expectedException \Symfony\Component\Form\Exception\BadMethodCallException
38+
* @expectedExceptionMessage Buttons do not support empty data.
39+
*
40+
* @param string $emptyData
41+
* @param null $expectedData
42+
*/
43+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
44+
{
45+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
46+
}
3547
}

Tests/Extension/Core/Type/CheckboxTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,17 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
187187
{
188188
parent::testSubmitNull(false, false, null);
189189
}
190+
191+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = true)
192+
{
193+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
194+
'empty_data' => $emptyData,
195+
));
196+
$form->submit(null);
197+
198+
// view data is transformed to the string true value
199+
$this->assertSame('1', $form->getViewData());
200+
$this->assertSame($expectedData, $form->getNormData());
201+
$this->assertSame($expectedData, $form->getData());
202+
}
190203
}

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -684,19 +684,20 @@ public function testSubmitSingleNonExpandedObjectChoices()
684684
$this->assertTrue($form->isSynchronized());
685685
}
686686

687-
public function testSubmitSingleChoiceWithEmptyData()
687+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
688688
{
689689
$form = $this->factory->create(static::TESTED_TYPE, null, array(
690690
'multiple' => false,
691691
'expanded' => false,
692-
'choices' => array('test'),
692+
// empty data must match string choice value
693+
'choices' => array($emptyData),
693694
'choices_as_values' => true,
694-
'empty_data' => 'test',
695+
'empty_data' => $emptyData,
695696
));
696697

697698
$form->submit(null);
698699

699-
$this->assertSame('test', $form->getData());
700+
$this->assertSame($emptyData, $form->getData());
700701
}
701702

702703
public function testSubmitSingleChoiceWithEmptyDataAndInitialData()

Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,10 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
410410
{
411411
parent::testSubmitNull(array(), array(), array());
412412
}
413+
414+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = array())
415+
{
416+
// resize form listener always set an empty array
417+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
418+
}
413419
}

Tests/Extension/Core/Type/CountryTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
6666
{
6767
parent::testSubmitNull($expected, $norm, '');
6868
}
69+
70+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'FR', $expectedData = 'FR')
71+
{
72+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
73+
}
6974
}

Tests/Extension/Core/Type/CurrencyTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
4949
{
5050
parent::testSubmitNull($expected, $norm, '');
5151
}
52+
53+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'EUR', $expectedData = 'EUR')
54+
{
55+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
56+
}
5257
}

Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,20 @@ public function testSubmitNullWithSingleText()
615615
$this->assertNull($form->getNormData());
616616
$this->assertSame('', $form->getViewData());
617617
}
618+
619+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
620+
{
621+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
622+
'empty_data' => $emptyData,
623+
));
624+
$form->submit(null);
625+
626+
// view transformer writes back empty strings in the view data
627+
$this->assertSame(
628+
array('date' => array('year' => '', 'month' => '', 'day' => ''), 'time' => array('hour' => '', 'minute' => '')),
629+
$form->getViewData()
630+
);
631+
$this->assertSame($expectedData, $form->getNormData());
632+
$this->assertSame($expectedData, $form->getData());
633+
}
618634
}

Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,4 +1003,33 @@ public function testSubmitNullWithSingleText()
10031003
$this->assertNull($form->getNormData());
10041004
$this->assertSame('', $form->getViewData());
10051005
}
1006+
1007+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
1008+
{
1009+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
1010+
'empty_data' => $emptyData,
1011+
));
1012+
$form->submit(null);
1013+
1014+
// view transformer write back empty strings in the view data
1015+
$this->assertSame(array('year' => '', 'month' => '', 'day' => ''), $form->getViewData());
1016+
$this->assertSame($expectedData, $form->getNormData());
1017+
$this->assertSame($expectedData, $form->getData());
1018+
}
1019+
1020+
public function testSingleTextSubmitNullUsesDefaultEmptyData()
1021+
{
1022+
$emptyData = '2018-11-11';
1023+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
1024+
'widget' => 'single_text',
1025+
'empty_data' => $emptyData,
1026+
));
1027+
$form->submit(null);
1028+
1029+
$date = new \DateTime($emptyData);
1030+
1031+
$this->assertSame($emptyData, $form->getViewData());
1032+
$this->assertEquals($date, $form->getNormData());
1033+
$this->assertEquals($date, $form->getData());
1034+
}
10061035
}

Tests/Extension/Core/Type/FormTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ public function testDataClassMustBeValidClassOrInterface()
227227
));
228228
}
229229

230+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = array())
231+
{
232+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
233+
}
234+
230235
public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()
231236
{
232237
$form = $this->factory->createBuilder(static::TESTED_TYPE, null, array(

0 commit comments

Comments
 (0)