8000 minor #21877 [Form] Hardened form type tests (HeahDude) · symfony/symfony@ea8025b · GitHub
[go: up one dir, main page]

Skip to content

Commit ea8025b

Browse files
committed
minor #21877 [Form] Hardened form type tests (HeahDude)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Hardened form type tests | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ This one the PRs to come targeting 2.7 needed to harden the code base before doing some changes in master. It takes care of the form types tests part (even if some other tests will be added later, it wil 8000 l also be easier after it), and unlocks merging #21481. It also back ports tests added in #18357 for the TextType. Since it's very hard to merge branches when modifying tests because of BC, and making every test type extend the base type test would involve many function calls to get the tested type, the function `getTestedType` is no longer abstract and return a constant to override instead, it's much better for performance, I didn't change the call in the base type test to keep BC but I suggest to deprecate it in master. Even if those are tests I really think it is worth keeping BC here. The constants also ease testing in the ecosystem of form related libraries that need to be compatible with Symfony 2.7 and 3. I think using "test" as both prefix and suffix on namespaces, classes and names of the constants should discourage using them in real application code. Since this is just about our test suite, I don't think this should be considered a feature, so tis change should be good for 2.7. Two other PRs will follow to solve conflicts in 2.8 and 3.2. I missed last month patches, I hope I won't this time :). Commits ------- 8cfc3e9 [Form] Hardened form type tests
2 parents 69bb758 + 8cfc3e9 commit ea8025b

26 files changed

+1281
-912
lines changed

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Lines changed: 280 additions & 106 deletions
Large diffs are not rendered by default.

src/Symfony/Component/Form/Tests/Extension/Core/Type/BaseTypeTest.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
abstract class BaseTypeTest extends TypeTestCase
2020
{
21+
const TESTED_TYPE = '';
22+
2123
public function testPassDisabledAsOption()
2224
{
2325
$form = $this->factory->create($this->getTestedType(), null, array('disabled' => true));
@@ -47,7 +49,7 @@ public function testStripLeadingUnderscoresAndDigitsFromId()
4749

4850
public function testPassIdAndNameToViewWithParent()
4951
{
50-
$view = $this->factory->createNamedBuilder('parent', 'form')
52+
$view = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
5153
->add('child', $this->getTestedType())
5254
->getForm()
5355
->createView();
@@ -59,8 +61,8 @@ public function testPassIdAndNameToViewWithParent()
5961

6062
public function testPassIdAndNameToViewWithGrandParent()
6163
{
62-
$builder = $this->factory->createNamedBuilder('parent', 'form')
63-
->add('child', 'form');
64+
$builder = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
65+
->add('child', FormTypeTest::TESTED_TYPE);
6466
$builder->get('child')->add('grand_child', $this->getTestedType());
6567
$view = $builder->getForm()->createView();
6668

@@ -71,18 +73,18 @@ public function testPassIdAndNameToViewWithGrandParent()
7173

7274
public function testPassTranslationDomainToView()
7375
{
74-
$form = $this->factory->create($this->getTestedType(), null, array(
76+
$view = $this->factory->create($this->getTestedType(), null, array(
7577
'translation_domain' => 'domain',
76-
));
77-
$view = $form->createView();
78+
))
79+
->createView();
7880

7981
$this->assertSame('domain', $view->vars['translation_domain']);
8082
}
8183

8284
public function testInheritTranslationDomainFromParent()
8385
{
8486
$view = $this->factory
85-
->createNamedBuilder('parent', 'form', null, array(
87+
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, array(
8688
'translation_domain' => 'domain',
8789
))
8890
->add('child', $this->getTestedType())
@@ -95,7 +97,7 @@ public function testInheritTranslationDomainFromParent()
9597
public function testPreferOwnTranslationDomain()
9698
{
9799
$view = $this->factory
98-
->createNamedBuilder('parent', 'form', null, array(
100+
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, array(
99101
'translation_domain' => 'parent_domain',
100102
))
101103
->add('child', $this->getTestedType(), array(
@@ -109,7 +111,7 @@ public function testPreferOwnTranslationDomain()
109111

110112
public function testDefaultTranslationDomain()
111113
{
112-
$view = $this->factory->createNamedBuilder('parent', 'form')
114+
$view = $this->factory->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE)
113115
->add('child', $this->getTestedType())
114116
->getForm()
115117
->createView();
@@ -119,19 +121,32 @@ public function testDefaultTranslationDomain()
119121

120122
public function testPassLabelToView()
121123
{
122-
$form = $this->factory->createNamed('__test___field', $this->getTestedType(), null, array('label' => 'My label'));
123-
$view = $form->createView();
124+
$view = $this->factory->createNamed('__test___field', $this->getTestedType(), null, array('label' => 'My label'))
125+
->createView();
124126

125127
$this->assertSame('My label', $view->vars['label']);
126128
}
127129

128130
public function testPassMultipartFalseToView()
129131
{
130-
$form = $this->factory->create($this->getTestedType());
131-
$view = $form->createView();
132+
$view = $this->factory->create($this->getTestedType())
133+
->createView();
132134

133135
$this->assertFalse($view->vars['multipart']);
134136
}
135137

136-
abstract protected function getTestedType();
138+
public function testSubmitNull($expected = null, $norm = null, $view = null)
139+
{
140+
$form = $this->factory->create($this->getTestedType());
141+
$form->submit(null);
142+
143+
$this->assertSame($expected, $form->getData());
144+
$this->assertSame($norm, $form->getNormData());
145+
$this->assertSame($view, $form->getViewData());
146+
}
147+
148+
protected function getTestedType()
149+
{
150+
return static::TESTED_TYPE;
151+
}
137152
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/BirthdayTypeTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@
1414
/**
1515
* @author Stepan Anchugov <kixxx1@gmail.com>
1616
*/
17-
class BirthdayTypeTest extends BaseTypeTest
17+
class BirthdayTypeTest extends DateTypeTest
1818
{
19+
const TESTED_TYPE = 'birthday';
20+
1921
/**
2022
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
2123
*/
2224
public function testSetInvalidYearsOption()
2325
{
24-
$this->factory->create('birthday', null, array(
26+
$this->factory->create(static::TESTED_TYPE, null, array(
2527
'years' => 'bad value',
2628
));
2729
}
28-
29-
protected function getTestedType()
30-
{
31-
return 'birthday';
32-
}
3330
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/ButtonTypeTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
*/
1717
class ButtonTypeTest extends BaseTypeTest
1818
{
19-
public function testCreateButtonInstances()
20-
{
21-
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create('button'));
22-
}
19+
const TESTED_TYPE = 'button';
2320

24-
protected function getTestedType()
21+
public function testCreateButtonInstances()
2522
{
26-
return 'button';
23+
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create(static::TESTED_TYPE));
2724
}
2825
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\CallbackTransformer;
15-
use Symfony\Component\Form\Test\TypeTestCase;
1615

17-
class CheckboxTypeTest extends TypeTestCase
16+
class CheckboxTypeTest extends BaseTypeTest
1817
{
18+
const TESTED_TYPE = 'checkbox';
19+
1920
public function testDataIsFalseByDefault()
2021
{
21-
$form = $this->factory->create('checkbox');
22+
$form = $this->factory->create(static::TESTED_TYPE);
2223

2324
$this->assertFalse($form->getData());
2425
$this->assertFalse($form->getNormData());
@@ -27,42 +28,42 @@ public function testDataIsFalseByDefault()
2728

2829
public function testPassValueToView()
2930
{
30-
$form = $this->factory->create('checkbox', null, array('value' => 'foobar'));
31-
$view = $form->createView();
31+
$view = $this->factory->create(static::TESTED_TYPE, null, array('value' => 'foobar'))
32+
->createView();
3233

3334
$this->assertEquals('foobar', $view->vars['value']);
3435
}
3536

3637
public function testCheckedIfDataTrue()
3738
{
38-
$form = $this->factory->create('checkbox');
39-
$form->setData(true);
40-
$view = $form->createView();
39+
$view = $this->factory->create(static::TESTED_TYPE)
40+
->setData(true)
41+
->createView();
4142

4243
$this->assertTrue($view->vars['checked']);
4344
}
4445

4546
public function testCheckedIfDataTrueWithEmptyValue()
4647
{
47-
$form = $this->factory->create('checkbox', null, array('value' => ''));
48-
$form->setData(true);
49-
$view = $form->createView();
48+
$view = $this->factory->create(static::TESTED_TYPE, null, array('value' => ''))
49+
->setData(true)
50+
->createView();
5051

5152
$this->assertTrue($view->vars['checked']);
5253
}
5354

5455
public function testNotCheckedIfDataFalse()
5556
{
56-
$form = $this->factory->create('checkbox');
57-
$form->setData(false);
58-
$view = $form->createView();
57+
$view = $this->factory->create(static::TESTED_TYPE)
58+
->setData(false)
59+
->createView();
5960

6061
$this->assertFalse($view->vars['checked']);
6162
}
6263

6364
public function testSubmitWithValueChecked()
6465
{
65-
$form = $this->factory->create('checkbox', null, array(
66+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
6667
'value' => 'foobar',
6768
));
6869
$form->submit('foobar');
@@ -73,7 +74,7 @@ public function testSubmitWithValueChecked()
7374

7475
public function testSubmitWithRandomValueChecked()
7576
{
76-
$form = $this->factory->create('checkbox', null, array(
77+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
7778
'value' => 'foobar',
7879
));
7980
$form->submit('krixikraxi');
@@ -84,7 +85,7 @@ public function testSubmitWithRandomValueChecked()
8485

8586
public function testSubmitWithValueUnchecked()
8687
{
87-
$form = $this->factory->create('checkbox', null, array(
88+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
8889
'value' => 'foobar',
8990
));
9091
$form->submit(null);
@@ -95,7 +96,7 @@ public function testSubmitWithValueUnchecked()
9596

9697
public function testSubmitWithEmptyValueChecked()
9798
{
98-
$form = $this->factory->create('checkbox', null, array(
99+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
99100
'value' => '',
100101
));
101102
$form->submit('');
@@ -106,7 +107,7 @@ public function testSubmitWithEmptyValueChecked()
106107

107108
public function testSubmitWithEmptyValueUnchecked()
108109
{
109-
$form = $this->factory->create('checkbox', null, a 10000 rray(
110+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
110111
'value' => '',
111112
));
112113
$form->submit(null);
@@ -117,7 +118,7 @@ public function testSubmitWithEmptyValueUnchecked()
117118

118119
public function testSubmitWithEmptyValueAndFalseUnchecked()
119120
{
120-
$form = $this->factory->create('checkbox', null, array(
121+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
121122
'value' => '',
122123
));
123124
$form->submit(false);
@@ -128,7 +129,7 @@ public function testSubmitWithEmptyValueAndFalseUnchecked()
128129

129130
public function testSubmitWithEmptyValueAndTrueChecked()
130131
{
131-
$form = $this->factory->create('checkbox', null, array(
132+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
132133
'value' => '',
133134
));
134135
$form->submit(true);
@@ -152,7 +153,7 @@ function ($value) {
152153
}
153154
);
154155

155-
$form = $this->factory->createBuilder('checkbox')
156+
$form = $this->factory->createBuilder(static::TESTED_TYPE)
156157
->addModelTransformer($transformer)
157158
->getForm();
158159

@@ -171,4 +172,9 @@ public function provideCustomModelTransformerData()
171172
array('unchecked', false),
172173
);
173174
}
175+
176+
public function testSubmitNull($expected = null, $norm = null, $view = null)
177+
{
178+
parent::testSubmitNull(false, false, null);
179+
}
174180
}

0 commit comments

Comments
 (0)
0