Skip to content

Commit cb16d4a

Browse files
Merge branch '3.4' into 4.1
* 3.4: [Form] Hardened test suite for empty data Bump phpunit XSD version to 5.2 [Fwb][EventDispatcher][HttpKernel] Fix getClosureScopeClass usage to describe callables Add required key attribute
2 parents c686aef + e0dbf87 commit cb16d4a

20 files changed

+216
-5
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
@@ -22,4 +22,16 @@ public function testCreateButtonInstances()
2222
{
2323
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create(static::TESTED_TYPE));
2424
}
25+
26+
/**
27+
* @expectedException \Symfony\Component\Form\Exception\BadMethodCallException
28+
* @expectedExceptionMessage Buttons do not support empty data.
29+
*
30+
* @param string $emptyData
31+
* @param null $expectedData
32+
*/
33+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
34+
{
35+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
36+
}
2537
}

Tests/Extension/Core/Type/CheckboxTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,17 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
209209
{
210210
parent::testSubmitNull(false, false, null);
211211
}
212+
213+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = true)
214+
{
215+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
216+
'empty_data' => $emptyData,
217+
));
218+
$form->submit(null);
219+
220+
// view data is transformed to the string true value
221+
$this->assertSame('1', $form->getViewData());
222+
$this->assertSame($expectedData, $form->getNormData());
223+
$this->assertSame($expectedData, $form->getData());
224+
}
212225
}

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,18 +584,19 @@ public function testSubmitSingleNonExpandedObjectChoices()
584584
$this->assertTrue($form->isSynchronized());
585585
}
586586

587-
public function testSubmitSingleChoiceWithEmptyData()
587+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = null)
588588
{
589589
$form = $this->factory->create(static::TESTED_TYPE, null, array(
590590
'multiple' => false,
591591
'expanded' => false,
592-
'choices' => array('test'),
593-
'empty_data' => 'test',
592+
// empty data must match string choice value
593+
'choices' => array($emptyData),
594+
'empty_data' => $emptyData,
594595
));
595596

596597
$form->submit(null);
597598

598-
$this->assertSame('test', $form->getData());
599+
$this->assertSame($emptyData, $form->getData());
599600
}
600601

601602
public function testSubmitSingleChoiceWithEmptyDataAndInitialData()

Tests/Extension/Core/Type/CollectionTypeTest.php

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

Tests/Extension/Core/Type/CountryTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
7575
{
7676
parent::testSubmitNull($expected, $norm, '');
7777
}
78+
79+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'FR', $expectedData = 'FR')
80+
{
81+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
82+
}
7883
}

Tests/Extension/Core/Type/CurrencyTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
5656
{
5757
parent::testSubmitNull($expected, $norm, '');
5858
}
59+
60+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'EUR', $expectedData = 'EUR')
61+
{
62+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
63+
}
5964
}

Tests/Extension/Core/Type/DateIntervalTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,4 +410,17 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
410410
'days' => '',
411411
));
412412
}
413+
414+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
415+
{
416+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
417+
'empty_data' => $emptyData,
418+
));
419+
$form->submit(null);
420+
421+
// view transformer writes back empty strings in the view data
422+
$this->assertSame(array('years' => '', 'months' => '', 'days' => ''), $form->getViewData());
423+
$this->assertSame($expectedData, $form->getNormData());
424+
$this->assertSame($expectedData, $form->getData());
425+
}
413426
}

Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,4 +628,20 @@ public function testSubmitNullWithSingleText()
628628
$this->assertNull($form->getNormData());
629629
$this->assertSame('', $form->getViewData());
630630
}
631+
632+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
633+
{
634+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
635+
'empty_data' => $emptyData,
636+
));
637+
$form->submit(null);
638+
639+
// view transformer writes back empty strings in the view data
640+
$this->assertSame(
641+
array('date' => array('year' => '', 'month' => '', 'day' => ''), 'time' => array('hour' => '', 'minute' => '')),
642+
$form->getViewData()
643+
);
644+
$this->assertSame($expectedData, $form->getNormData());
645+
$this->assertSame($expectedData, $form->getData());
646+
}
631647
}

Tests/Extension/Core/Type/DateTypeTest.php

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

Tests/Extension/Core/Type/FormTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ public function testDataClassMustBeValidClassOrInterface()
170170
));
171171
}
172172

173+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = array())
174+
{
175+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
176+
}
177+
173178
public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()
174179
{
175180
$form = $this->factory->createBuilder(static::TESTED_TYPE, null, array(

Tests/Extension/Core/Type/IntegerTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
3838
{
3939
parent::testSubmitNull($expected, $norm, '');
4040
}
41+
42+
public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedData = 10)
43+
{
44+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
45+
'empty_data' => $emptyData,
46+
));
47+
$form->submit(null);
48+
49+
$this->assertSame($emptyData, $form->getViewData());
50+
$this->assertSame($expectedData, $form->getNormData());
51+
$this->assertSame($expectedData, $form->getData());
52+
}
4153
}

Tests/Extension/Core/Type/LanguageTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
6868
{
6969
parent::testSubmitNull($expected, $norm, '');
7070
}
71+
72+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'en', $expectedData = 'en')
73+
{
74+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
75+
}
7176
}

Tests/Extension/Core/Type/LocaleTypeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
5656
{
5757
parent::testSubmitNull($expected, $norm, '');
5858
}
59+
60+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'en', $expectedData = 'en')
61+
{
62+
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
63+
}
5964
}

Tests/Extension/Core/Type/MoneyTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public function testMoneyPatternWithoutCurrency()
7171
$this->assertSame('{{ widget }}', $view->vars['money_pattern']);
7272
}
7373

74+
public function testSubmitNullUsesDefaultEmptyData($emptyData = '10.00', $expectedData = 10.0)
75+
{
76+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
77+
'empty_data' => $emptyData,
78+
));
79+
$form->submit(null);
80+
81+
$this->assertSame($emptyData, $form->getViewData());
82+
$this->assertSame($expectedData, $form->getNormData());
83+
$this->assertSame($expectedData, $form->getData());
84+
}
85+
7486
public function testDefaultFormattingWithDefaultRounding()
7587
{
7688
$form = $this->factory->create(static::TESTED_TYPE, null, array('scale' => 0));

Tests/Extension/Core/Type/NumberTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,16 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
6363
{
6464
parent::testSubmitNull($expected, $norm, '');
6565
}
66+
67+
public function testSubmitNullUsesDefaultEmptyData($emptyData = '10', $expectedData = 10.0)
68+
{
69+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
70+
'empty_data' => $emptyData,
71+
));
72+
$form->submit(null);
73+
74+
$this->assertSame($emptyData, $form->getViewData());
75+
$this->assertSame($expectedData, $form->getNormData());
76+
$this->assertSame($expectedData, $form->getData());
77+
}
6678
}

Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,4 +790,17 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
790790

791791
parent::testSubmitNull($expected, $norm, $view);
792792
}
793+
794+
public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expectedData = null)
795+
{
796+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
797+
'empty_data' => $emptyData,
798+
));
799+
$form->submit(null);
800+
801+
// view transformer write back empty strings in the view data
802+
$this->assertSame(array('hour' => '', 'minute' => ''), $form->getViewData());
803+
$this->assertSame($expectedData, $form->getNormData());
804+
$this->assertSame($expectedData, $form->getData());
805+
}
793806
}

Tests/Extension/Core/Type/TimezoneTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
3434
parent::testSubmitNull($expected, $norm, '');
3535
}
3636

37+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'Africa/Kinshasa', $expectedData = 'Africa/Kinshasa')
38+
{
39+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
40+
'empty_data' => $emptyData,
41+
));
42+
$form->submit(null);
43+
44+
$this->assertSame($emptyData, $form->getViewData());
45+
$this->assertSame($expectedData, $form->getNormData());
46+
$this->assertSame($expectedData, $form->getData());
47+
}
48+
3749
public function testDateTimeZoneInput()
3850
{
3951
$form = $this->factory->create(static::TESTED_TYPE, new \DateTimeZone('America/New_York'), array('input' => 'datetimezone'));

Tests/Extension/Core/Type/UrlTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@ public function testSubmitWithNonStringDataDoesNotBreakTheFixUrlProtocolListener
9090

9191
$this->assertSame(array('domain.com', 'www.domain.com'), $form->getData());
9292
}
93+
94+
public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expectedData = 'http://empty')
95+
{
96+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
97+
'empty_data' => $emptyData,
98+
));
99+
$form->submit(null);
100+
101+
// listener normalizes data on submit
102+
$this->assertSame($expectedData, $form->getViewData());
103+
$this->assertSame($expectedData, $form->getNormData());
104+
$this->assertSame($expectedData, $form->getData());
105+
}
93106
}

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
55
backupGlobals="false"
66
colors="true"
77
bootstrap="vendor/autoload.php"

0 commit comments

Comments
 (0)