8000 [Form] Added option type checks in some FormTypes · symfony/symfony@34e3a24 · GitHub
[go: up one dir, main page]

Skip to content

Commit 34e3a24

Browse files
committed
[Form] Added option type checks in some FormTypes
1 parent a2bdc72 commit 34e3a24

13 files changed

+97
-6
lines changed

UPGRADE-3.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ UPGRADE FROM 2.x to 3.0
307307
```php
308308
echo $form->getErrors(true, false);
309309
```
310+
* The array type hints for `ChoiceList::initialize()` method's `$labels` and
311+
`$preferredChoices` parameters were removed.
310312

311313
### FrameworkBundle
312314

src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ChoiceList implements ChoiceListInterface
8787
*
8888
* @throws UnexpectedTypeException If the choices are not an array or \Traversable.
8989
*/
90-
public function __construct($choices, array $labels, array $preferredChoices = array())
90+
public function __construct($choices, $labels, $preferredChoices = array())
9191
{
9292
if (!is_array($choices) && !$choices instanceof \Traversable) {
9393
throw new UnexpectedTypeException($choices, 'array or \Traversable');
@@ -105,7 +105,7 @@ public function __construct($choices, array $labels, array $preferredChoices = a
105105
* @param array $labels The labels belonging to the choices.
106106
* @param array $preferredChoices The choices to display with priority.
107107
*/
108-
protected function initialize($choices, array $labels, array $preferredChoices)
108+
protected function initialize($choices, $labels, $preferredChoices)
109109
{
110110
$this->choices = array();
111111
$this->values = array();
@@ -271,7 +271,7 @@ public function getIndicesForValues(array $values)
271271
* @throws InvalidArgumentException If the structures of the choices and labels array do not match.
272272
* @throws InvalidConfigurationException If no valid value or index could be created for a choice.
273273
*/
274-
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices)
274+
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, $labels, $preferredChoices)
275275
{
276276
// Add choices to the nested buckets
277277
foreach ($choices as $group => $choice) {

src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ObjectChoiceList extends ChoiceList
9090
* are generated instead.
9191
* @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths.
9292
*/
93-
public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null)
93+
public function __construct($choices, $labelPath = null, $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null)
9494
{
9595
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
9696
$this->labelPath = null !== $labelPath ? new PropertyPath($labelPath) : null;
@@ -112,7 +112,7 @@ public function __construct($choices, $labelPath = null, array $preferredChoices
112112
* @throws InvalidArgumentException When passing a hierarchy of choices and using
113113
* the "groupPath" option at the same time.
114114
*/
115-
protected function initialize($choices, array $labels, array $preferredChoices)
115+
protected function initialize($choices, $labels, $preferredChoices)
116116
{
117117
if (null !== $this->groupPath) {
118118
$groupedChoices = array();

src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getValuesForChoices(array $choices)
9191
* @param array $labels Ignored.
9292
* @param array $preferredChoices The preferred choices.
9393
*/
94-
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices)
94+
protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, $labels, $preferredChoices)
9595
{
9696
// Add choices to the nested buckets
9797
foreach ($choices as $choice => $label) {

src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public function configureOptions(OptionsResolver $resolver)
9090
'delete_empty' => false,
9191
));
9292

93+
$resolver->setAllowedTypes('options', 'array');
94+
9395
$resolver->setNormalizer('options', $optionsNormalizer);
9496
}
9597

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,4 +1671,21 @@ public function testInitializeWithDefaultObjectChoice()
16711671
// Trigger data initialization
16721672
$form->getViewData();
16731673
}
1674+
1675+
public function testChoicesOptionSupportsTraversable()
1676+
{
1677+
$this->factory->create('choice', null, array(
1678+
'choices' => new \ArrayObject($this->choices),
1679+
));
1680+
}
1681+
1682+
/**
1683+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
1684+
*/
1685+
public function testSetInvalidPreferredChoicesOption()
1686+
{
1687+
$this->factory->create('choice', null, array(
1688+
'preferred_choices' => 'bad value',
1689+
));
1690+
}
16741691
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,14 @@ public function testPrototypeDefaultLabel()
274274

275275
$this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']);
276276
}
277+
278+
/**
279+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
280+
*/
281+
public function testSetInvalidOptions()
282+
{
283+
$this->factory->create('collection', null, array(
284+
'options' => 'bad value',
285+
));
286+
}
277287
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ public function testUnknownCountryIsNotIncluded()
5050
}
5151
}
5252
}
53+
54+
/**
55+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
56+
*/
57+
public function testSetInvalidChoices()
58+
{
59+
$this->factory->create('country', null, array(
60+
'choices' => 'bad value',
61+
));
62+
}
5363
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,14 @@ public function testCurrenciesAreSelectable()
3434
$this->assertContains(new ChoiceView('USD', 'USD', 'US Dollar'), $choices, '', false, false);
3535
$this->assertContains(new ChoiceView('SIT', 'SIT', 'Slovenian Tolar'), $choices, '', false, false);
3636
}
37+
38+
/**
39+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
40+
*/
41+
public function testSetInvalidChoices()
42+
{
43+
$this->factory->create('currency', null, array(
44+
'choices' => 'bad value',
45+
));
46+
}
3747
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,14 @@ public function testMultipleLanguagesIsNotIncluded()
4545

4646
$this->assertNotContains(new ChoiceView('mul', 'mul', 'Mehrsprachig'), $choices, '', false, false);
4747
}
48+
49+
/**
50+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
51+
*/
52+
public function testSetInvalidChoices()
53+
{
54+
$this->factory->create('language', null, array(
55+
'choices' => 'bad value',
56+
));
57+
}
4858
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,14 @@ public function testLocalesAreSelectable()
3434
$this->assertContains(new ChoiceView('en_GB', 'en_GB', 'English (United Kingdom)'), $choices, '', false, false);
3535
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
3636
}
37+
38+
/**
39+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
40+
*/
41+
public function testSetInvalidChoices()
42+
{
43+
$this->factory->create('locale', null, array(
44+
'choices' => 'bad value',
45+
));
46+
}
3747
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,4 +731,14 @@ public function testThrowExceptionIfSecondsIsInvalid()
731731
'seconds' => 'bad value',
732732
));
733733
}
734+
735+
/**
736+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
737+
*/
738+
public function testThrowExceptionIfEmptyValueIsInvalid()
739+
{
740+
$this->factory->create('time', null, array(
741+
'empty_value' => array(),
742+
));
743+
}
734744
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ public function testTimezonesAreSelectable()
2727
$this->assertArrayHasKey('America', $choices);
2828
$this->assertContains(new ChoiceView('America/New_York', 'America/New_York', 'New York'), $choices['America'], '', false, false);
2929
}
30+
31+
/**
32+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
33+
*/
34+
public function testSetInvalidChoices()
35+
{
36+
$this->factory->create('timezone', null, array(
37+
'choices' => 'bad value',
38+
));
39+
}
3040
}

0 commit comments

Comments
 (0)
0