Skip to content

Commit c4b0de7

Browse files
committed
minor #22267 fix some more risky tests (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- fix some more risky 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 | Continuation of #22066 fixing the tests for the following components: * Config * Form * HttpFoundation * Security Commits ------- fffcd247b2 fix some risky tests
2 parents 196050a + 8d5732b commit c4b0de7

12 files changed

+97
-74
lines changed

Tests/CompoundFormTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ public function testRemoveThrowsExceptionIfAlreadySubmitted()
299299
public function testRemoveIgnoresUnknownName()
300300
{
301301
$this->form->remove('notexisting');
302+
303+
$this->assertCount(0, $this->form);
302304
}
303305

304306
public function testArrayAccess()

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ public function testChoiceLoaderOptionExpectsChoiceLoaderInterface()
126126

127127
public function testChoiceListAndChoicesCanBeEmpty()
128128
{
129-
$this->factory->create(static::TESTED_TYPE, null, array(
129+
$this->assertInstanceOf('Symfony\Component\Form\FormInterface', $this->factory->create(static::TESTED_TYPE, null, array(
130130
'choices_as_values' => true,
131-
));
131+
)));
132132
}
133133

134134
public function testExpandedChoicesOptionsTurnIntoChildren()
@@ -2251,10 +2251,10 @@ public function testAdjustFullNameForMultipleNonExpanded()
22512251
// https://github.com/symfony/symfony/issues/3298
22522252
public function testInitializeWithEmptyChoices()
22532253
{
2254-
$this->factory->createNamed('name', static::TESTED_TYPE, null, array(
2254+
$this->assertInstanceOf('Symfony\Component\Form\FormInterface', $this->factory->createNamed('name', static::TESTED_TYPE, null, array(
22552255
'choices' => array(),
22562256
'choices_as_values' => true,
2257-
));
2257+
)));
22582258
}
22592259

22602260
public function testInitializeWithDefaultObjectChoice()

Tests/Extension/Core/Type/CountryTypeTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ public function testUnknownCountryIsNotIncluded()
4343
$choices = $this->factory->create(static::TESTED_TYPE, 'country')
4444
->createView()->vars['choices'];
4545

46+
$countryCodes = array();
47+
4648
foreach ($choices as $choice) {
47-
if ('ZZ' === $choice->value) {
48-
$this->fail('Should not contain choice "ZZ"');
49-
}
49+
$countryCodes[] = $choice->value;
5050
}
51+
52+
$this->assertNotContains('ZZ', $countryCodes);
5153
}
5254

5355
public function testSubmitNull($expected = null, $norm = null, $view = null)

Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function testInitializeWithDateTime()
273273
{
274274
// Throws an exception if "data_class" option is not explicitly set
275275
// to null in the type
276-
$this->factory->create(static::TESTED_TYPE, new \DateTime());
276+
$this->assertInstanceOf('Symfony\Component\Form\FormInterface', $this->factory->create(static::TESTED_TYPE, new \DateTime()));
277277
}
278278

279279
public function testSingleTextWidgetShouldUseTheRightInputType()

Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ public function testInitializeWithDateTime()
711711
{
712712
// Throws an exception if "data_class" option is not explicitly set
713713
// to null in the type
714-
$this->factory->create(static::TESTED_TYPE, new \DateTime());
714+
$this->assertInstanceOf('Symfony\Component\Form\FormInterface', $this->factory->create(static::TESTED_TYPE, new \DateTime()));
715715
}
716716

717717
public function testSingleTextWidgetShouldUseTheRightInputType()

Tests/Extension/Core/Type/FormTypeTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,23 +149,23 @@ public function testPassMaxLengthBCToView()
149149

150150
public function testDataClassMayBeNull()
151151
{
152-
$this->factory->createBuilder(static::TESTED_TYPE, null, array(
152+
$this->assertInstanceOf('Symfony\Component\Form\FormBuilderInterface', $this->factory->createBuilder(static::TESTED_TYPE, null, array(
153153
'data_class' => null,
154-
));
154+
)));
155155
}
156156

157157
public function testDataClassMayBeAbstractClass()
158158
{
159-
$this->factory->createBuilder(static::TESTED_TYPE, null, array(
159+
$this->assertInstanceOf('Symfony\Component\Form\FormBuilderInterface', $this->factory->createBuilder(static::TESTED_TYPE, null, array(
160160
'data_class' => 'Symfony\Component\Form\Tests\Fixtures\AbstractAuthor',
161-
));
161+
)));
162162
}
163163

164164
public function testDataClassMayBeInterface()
165165
{
166-
$this->factory->createBuilder(static::TESTED_TYPE, null, array(
166+
$this->assertInstanceOf('Symfony\Component\Form\FormBuilderInterface', $this->factory->createBuilder(static::TESTED_TYPE, null, array(
167167
'data_class' => 'Symfony\Component\Form\Tests\Fixtures\AuthorInterface',
168-
));
168+
)));
169169
}
170170

171171
/**
@@ -652,7 +652,7 @@ public function testCanGetErrorsWhenButtonInForm()
652652
$form = $builder->getForm();
653653

654654
//This method should not throw a Fatal Error Exception.
655-
$form->getErrorsAsString();
655+
$this->assertInternalType('string', $form->getErrorsAsString());
656656
}
657657

658658
public function testSubmitNull($expected = null, $norm = null, $view = null)

Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public function testInitializeWithDateTime()
488488
{
489489
// Throws an exception if "data_class" option is not explicitly set
490490
// to null in the type
491-
$this->factory->create(static::TESTED_TYPE, new \DateTime());
491+
$this->assertInstanceOf('Symfony\Component\Form\FormInterface', $this->factory->create(static::TESTED_TYPE, new \DateTime()));
492492
}
493493

494494
public function testSingleTextWidgetShouldUseTheRightInputType()

Tests/Extension/DependencyInjection/DependencyInjectionExtensionTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,21 @@ public function testGetTypeExtensions()
4646

4747
public function testThrowExceptionForInvalidExtendedType()
4848
{
49+
$formTypeExtension = $this->createFormTypeExtensionMock('unmatched');
50+
4951
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
5052

5153
$container->expects($this->any())
5254
->method('get')
5355
->with('extension')
54-
->willReturn($this->createFormTypeExtensionMock('unmatched'));
56+
->willReturn($formTypeExtension);
5557

5658
$extension = new DependencyInjectionExtension($container, array(), array('test' => array('extension')), array());
5759

58-
$extension->getTypeExtensions('test');
60+
$extensions = $extension->getTypeExtensions('test');
61+
62+
$this->assertCount(1, $extensions);
63+
$this->assertSame($formTypeExtension, $extensions[0]);
5964
}
6065

6166
public function testGetTypeGuesser()

Tests/FormBuilderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ public function testAddButton()
161161
{
162162
$this->builder->add(new ButtonBuilder('reset'));
163163
$this->builder->add(new SubmitButtonBuilder('submit'));
164+
165+
$this->assertCount(2, $this->builder->all());
164166
}
165167

166168
public function testGetUnknown()

Tests/FormConfigTest.php

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
namespace Symfony\Component\Form\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1615
use Symfony\Component\Form\FormConfigBuilder;
17-
use Symfony\Component\Form\Exception\InvalidArgumentException;
1816

1917
/**
2018
* @author Bernhard Schussek <[email protected]>
@@ -24,72 +22,65 @@ class FormConfigTest extends TestCase
2422
public function getHtml4Ids()
2523
{
2624
return array(
27-
array('z0', true),
28-
array('A0', true),
29-
array('A9', true),
30-
array('Z0', true),
31-
array('#', false),
32-
array('a#', false),
33-
array('a$', false),
34-
array('a%', false),
35-
array('a ', false),
36-
array("a\t", false),
37-
array("a\n", false),
38-
array('a-', true),
39-
array('a_', true),
40-
array('a:', true),
25+
array('z0'),
26+
array('A0'),
27+
array('A9'),
28+
array('Z0'),
29+
array('#', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
30+
array('a#', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
31+
array('a$', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
32+
array('a%', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
33+
array('a ', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
34+
array("a\t", 'Symfony\Component\Form\Exception\InvalidArgumentException'),
35+
array("a\n", 'Symfony\Component\Form\Exception\InvalidArgumentException'),
36+
array('a-'),
37+
array('a_'),
38+
array('a:'),
4139
// Periods are allowed by the HTML4 spec, but disallowed by us
4240
// because they break the generated property paths
43-
array('a.', false),
41+
array('a.', 'Symfony\Component\Form\Exception\InvalidArgumentException'),
4442
// Contrary to the HTML4 spec, we allow names starting with a
4543
// number, otherwise naming fields by collection indices is not
4644
// possible.
4745
// For root forms, leading digits will be stripped from the
4846
// "id" attribute to produce valid HTML4.
49-
array('0', true),
50-
array('9', true),
47+
array('0'),
48+
array('9'),
5149
// Contrary to the HTML4 spec, we allow names starting with an
5250
// underscore, since this is already a widely used practice in
5351
// Symfony.
5452
// For root forms, leading underscores will be stripped from the
5553
// "id" attribute to produce valid HTML4.
56-
array('_', true),
54+
array('_'),
5755
// Integers are allowed
58-
array(0, true),
59-
array(123, true),
56+
array(0),
57+
array(123),
6058
// NULL is allowed
61-
array(null, true),
59+
array(null),
6260
// Other types are not
63-
array(1.23, false),
64-
array(5., false),
65-
array(true, false),
66-
array(new \stdClass(), false),
61+
array(1.23, 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
62+
array(5., 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
63+
array(true, 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
64+
array(new \stdClass(), 'Symfony\Component\Form\Exception\UnexpectedTypeException'),
6765
);
6866
}
6967

7068
/**
7169
* @dataProvider getHtml4Ids
7270
*/
73-
public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $accepted)
71+
public function testNameAcceptsOnlyNamesValidAsIdsInHtml4($name, $expectedException = null)
7472
{
7573
$dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
7674

77-
try {
78-
new FormConfigBuilder($name, null, $dispatcher);
79-
if (!$accepted) {
80-
$this->fail(sprintf('The value "%s" should not be accepted', $name));
81-
}
82-
} catch (UnexpectedTypeException $e) {
83-
// if the value was not accepted, but should be, rethrow exception
84-
if ($accepted) {
85-
throw $e;
86-
}
87-
} catch (InvalidArgumentException $e) {
88-
// if the value was not accepted, but should be, rethrow exception
89-
if ($accepted) {
90-
throw $e;
91-
}
75+
if (null !== $expectedException && method_exists($this, 'expectException')) {
76+
$this->expectException($expectedException);
77+
} elseif (null !== $expectedException) {
78+
$this->setExpectedException($expectedException);
9279
}
80+
81+
$formConfigBuilder = new FormConfigBuilder($name, null, $dispatcher);
82+
83+
$this->assertSame((string) $name, $formConfigBuilder->getName());
9384
}
9485

9586
public function testGetRequestHandlerCreatesNativeRequestHandlerIfNotSet()
@@ -109,27 +100,42 @@ public function testGetRequestHandlerReusesNativeRequestHandlerInstance()
109100

110101
public function testSetMethodAllowsGet()
111102
{
112-
$this->getConfigBuilder()->setMethod('GET');
103+
$formConfigBuilder = $this->getConfigBuilder();
104+
$formConfigBuilder->setMethod('GET');
105+
106+
self::assertSame('GET', $formConfigBuilder->getMethod());
113107
}
114108

115109
public function testSetMethodAllowsPost()
116110
{
117-
$this->getConfigBuilder()->setMethod('POST');
111+
$formConfigBuilder = $this->getConfigBuilder();
112+
$formConfigBuilder->setMethod('POST');
113+
114+
self::assertSame('POST', $formConfigBuilder->getMethod());
118115
}
119116

120117
public function testSetMethodAllowsPut()
121118
{
122-
$this->getConfigBuilder()->setMethod('PUT');
119+
$formConfigBuilder = $this->getConfigBuilder();
120+
$formConfigBuilder->setMethod('PUT');
121+
122+
self::assertSame('PUT', $formConfigBuilder->getMethod());
123123
}
124124

125125
public function testSetMethodAllowsDelete()
126126
{
127-
$this->getConfigBuilder()->setMethod('DELETE');
127+
$formConfigBuilder = $this->getConfigBuilder();
128+
$formConfigBuilder->setMethod('DELETE');
129+
130+
self::assertSame('DELETE', $formConfigBuilder->getMethod());
128131
}
129132

130133
public function testSetMethodAllowsPatch()
131134
{
132-
$this->getConfigBuilder()->setMethod('PATCH');
135+
$formConfigBuilder = $this->getConfigBuilder();
136+
$formConfigBuilder->setMethod('PATCH');
137+
138+
self::assertSame('PATCH', $formConfigBuilder->getMethod());
133139
}
134140

135141
/**

0 commit comments

Comments
 (0)