Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5799940

Browse files
committed
[Form] Hardened form type tests in 2.8
1 parent 6e9d1bb commit 5799940

26 files changed

+1453
-940
lines changed

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

Lines changed: 282 additions & 108 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', 'Symfony\Component\Form\Extension\Core\Type\FormType')
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', 'Symfony\Component\Form\Extension\Core\Type\FormType')
63-
->add('child', 'Symfony\Component\Form\Extension\Core\Type\FormType');
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', 'Symfony\Component\Form\Extension\Core\Type\FormType', 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', 'Symfony\Component\Form\Extension\Core\Type\FormType', 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', 'Symfony\Component\Form\Extension\Core\Type\FormType')
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,8 +14,10 @@
1414
/**
1515
* @author Stepan Anchugov <[email protected]>
1616
*/
17-
class BirthdayTypeTest extends BaseTypeTest
17+
class BirthdayTypeTest extends DateTypeTest
1818
{
19+
const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\BirthdayType';
20+
1921
/**
2022
* @group legacy
2123
*/
@@ -31,13 +33,8 @@ public function testLegacyName()
3133
*/
3234
public function testSetInvalidYearsOption()
3335
{
34-
$this->factory->create('Symfony\Component\Form\Extension\Core\Type\BirthdayType', null, array(
36+
$this->factory->create(static::TESTED_TYPE, null, array(
3537
'years' => 'bad value',
3638
));
3739
}
38-
39-
protected function getTestedType()
40-
{
41-
return 'Symfony\Component\Form\Extension\Core\Type\BirthdayType';
42-
}
4340
}

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,6 +16,8 @@
1616
*/
1717
class ButtonTypeTest extends BaseTypeTest
1818
{
19+
const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\ButtonType';
20+
1921
/**
2022
* @group legacy
2123
*/
@@ -28,11 +30,6 @@ public function testLegacyName()
2830

2931
public function testCreateButtonInstances()
3032
{
31-
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ButtonType'));
32-
}
33-
34-
protected function getTestedType()
35-
{
36-
return 'Symfony\Component\Form\Extension\Core\Type\ButtonType';
33+
$this->assertInstanceOf('Symfony\Component\Form\Button', $this->factory->create(static::TESTED_TYPE));
3734
}
3835
}

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,10 +12,11 @@
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 = 'Symfony\Component\Form\Extension\Core\Type\CheckboxType';
19+
1920
/**
2021
* @group legacy
2122
*/
@@ -28,7 +29,7 @@ public function testLegacyName()
2829

2930
public function testDataIsFalseByDefault()
3031
{
31-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType');
32+
$form = $this->factory->create(static::TESTED_TYPE);
3233

3334
$this->assertFalse($form->getData());
3435
$this->assertFalse($form->getNormData());
@@ -37,42 +38,42 @@ public function testDataIsFalseByDefault()
3738

3839
public function testPassValueToView()
3940
{
40-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array('value' => 'foobar'));
41-
$view = $form->createView();
41+
$view = $this->factory->create(static::TESTED_TYPE, null, array('value' => 'foobar'))
42+
->createView();
4243

4344
$this->assertEquals('foobar', $view->vars['value']);
4445
}
4546

4647
public function testCheckedIfDataTrue()
4748
{
48-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType');
49-
$form->setData(true);
50-
$view = $form->createView();
49+
$view = $this->factory->create(static::TESTED_TYPE)
50+
->setData(true)
51+
->createView();
5152

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

5556
public function testCheckedIfDataTrueWithEmptyValue()
5657
{
57-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array('value' => ''));
58-
$form->setData(true);
59-
$view = $form->createView();
58+
$view = $this->factory->create(static::TESTED_TYPE, null, array('value' => ''))
59+
->setData(true)
60+
->createView();
6061

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

6465
public function testNotCheckedIfDataFalse()
6566
{
66-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType');
67-
$form->setData(false);
68-
$view = $form->createView();
67+
$view = $this->factory->create(static::TESTED_TYPE)
68+
->setData(false)
69+
->createView();
6970

7071
$this->assertFalse($view->vars['checked']);
7172
}
7273

7374
public function testSubmitWithValueChecked()
7475
{
75-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
76+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
7677
'value' => 'foobar',
7778
));
7879
$form->submit('foobar');
@@ -83,7 +84,7 @@ public function testSubmitWithValueChecked()
8384

8485
public function testSubmitWithRandomValueChecked()
8586
{
86-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
87+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
8788
'value' => 'foobar',
8889
));
8990
$form->submit('krixikraxi');
@@ -94,7 +95,7 @@ public function testSubmitWithRandomValueChecked()
9495

9596
public function testSubmitWithValueUnchecked()
9697
{
97-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
98+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
9899
'value' => 'foobar',
99100
));
100101
$form->submit(null);
@@ -105,7 +106,7 @@ public function testSubmitWithValueUnchecked()
105106

106107
public function testSubmitWithEmptyValueChecked()
107108
{
108-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
109+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
109110
'value' => '',
110111
));
111112
$form->submit('');
@@ -116,7 +117,7 @@ public function testSubmitWithEmptyValueChecked()
116117

117118
public function testSubmitWithEmptyValueUnchecked()
118119
{
119-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
120+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
120121
'value' => '',
121122
));
122123
$form->submit(null);
@@ -127,7 +128,7 @@ public function testSubmitWithEmptyValueUnchecked()
127128

128129
public function testSubmitWithEmptyValueAndFalseUnchecked()
129130
{
130-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
131+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
131132
'value' => '',
132133
));
133134
$form->submit(false);
@@ -138,7 +139,7 @@ public function testSubmitWithEmptyValueAndFalseUnchecked()
138139

139140
public function testSubmitWithEmptyValueAndTrueChecked()
140141
{
141-
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CheckboxType', null, array(
142+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
142143
'value' => '',
143144
));
144145
$form->submit(true);
@@ -162,7 +163,7 @@ function ($value) {
162163
}
163164
);
164165

165-
$form = $this->factory->createBuilder('Symfony\Component\Form\Extension\Core\Type\CheckboxType')
166+
$form = $this->factory->createBuilder(static::TESTED_TYPE)
166167
->addModelTransformer($transformer)
167168
->getForm();
168169

@@ -181,4 +182,9 @@ public function provideCustomModelTransformerData()
181182
array('unchecked', false),
182183
);
183184
}
185+
186+
public function testSubmitNull($expected = null, $norm = null, $view = null)
187+
{
188+
parent::testSubmitNull(false, false, null);
189+
}
184190
}

0 commit comments

Comments
 (0)