From 34e3a24b50b0ecab8b2c004f0d7a1b728ce8da1c Mon Sep 17 00:00:00 2001 From: Stepan Anchugov Date: Fri, 22 Jan 2016 05:17:49 +0500 Subject: [PATCH] [Form] Added option type checks in some FormTypes --- UPGRADE-3.0.md | 2 ++ .../Extension/Core/ChoiceList/ChoiceList.php | 6 +++--- .../Core/ChoiceList/ObjectChoiceList.php | 4 ++-- .../Core/ChoiceList/SimpleChoiceList.php | 2 +- .../Form/Extension/Core/Type/CollectionType.php | 2 ++ .../Extension/Core/Type/ChoiceTypeTest.php | 17 +++++++++++++++++ .../Extension/Core/Type/CollectionTypeTest.php | 10 ++++++++++ .../Extension/Core/Type/CountryTypeTest.php | 10 ++++++++++ .../Extension/Core/Type/CurrencyTypeTest.php | 10 ++++++++++ .../Extension/Core/Type/LanguageTypeTest.php | 10 ++++++++++ .../Extension/Core/Type/LocaleTypeTest.php | 10 ++++++++++ .../Tests/Extension/Core/Type/TimeTypeTest.php | 10 ++++++++++ .../Extension/Core/Type/TimezoneTypeTest.php | 10 ++++++++++ 13 files changed, 97 insertions(+), 6 deletions(-) diff --git a/UPGRADE-3.0.md b/UPGRADE-3.0.md index c20bb51c579a6..1a0e78d6ecfbf 100644 --- a/UPGRADE-3.0.md +++ b/UPGRADE-3.0.md @@ -307,6 +307,8 @@ UPGRADE FROM 2.x to 3.0 ```php echo $form->getErrors(true, false); ``` + * The array type hints for `ChoiceList::initialize()` method's `$labels` and + `$preferredChoices` parameters were removed. ### FrameworkBundle diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php index b77f4d4e75495..48f2f46bde6ff 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php @@ -87,7 +87,7 @@ class ChoiceList implements ChoiceListInterface * * @throws UnexpectedTypeException If the choices are not an array or \Traversable. */ - public function __construct($choices, array $labels, array $preferredChoices = array()) + public function __construct($choices, $labels, $preferredChoices = array()) { if (!is_array($choices) && !$choices instanceof \Traversable) { throw new UnexpectedTypeException($choices, 'array or \Traversable'); @@ -105,7 +105,7 @@ public function __construct($choices, array $labels, array $preferredChoices = a * @param array $labels The labels belonging to the choices. * @param array $preferredChoices The choices to display with priority. */ - protected function initialize($choices, array $labels, array $preferredChoices) + protected function initialize($choices, $labels, $preferredChoices) { $this->choices = array(); $this->values = array(); @@ -271,7 +271,7 @@ public function getIndicesForValues(array $values) * @throws InvalidArgumentException If the structures of the choices and labels array do not match. * @throws InvalidConfigurationException If no valid value or index could be created for a choice. */ - protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices) + protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, $labels, $preferredChoices) { // Add choices to the nested buckets foreach ($choices as $group => $choice) { diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php index 65742c45bce50..925d388875468 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php @@ -90,7 +90,7 @@ class ObjectChoiceList extends ChoiceList * are generated instead. * @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths. */ - public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null) + public function __construct($choices, $labelPath = null, $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null) { $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); $this->labelPath = null !== $labelPath ? new PropertyPath($labelPath) : null; @@ -112,7 +112,7 @@ public function __construct($choices, $labelPath = null, array $preferredChoices * @throws InvalidArgumentException When passing a hierarchy of choices and using * the "groupPath" option at the same time. */ - protected function initialize($choices, array $labels, array $preferredChoices) + protected function initialize($choices, $labels, $preferredChoices) { if (null !== $this->groupPath) { $groupedChoices = array(); diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php index 90cc4ac0236e1..ea383220d1f16 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php @@ -91,7 +91,7 @@ public function getValuesForChoices(array $choices) * @param array $labels Ignored. * @param array $preferredChoices The preferred choices. */ - protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices) + protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, $labels, $preferredChoices) { // Add choices to the nested buckets foreach ($choices as $choice => $label) { diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php index 599aa457b7343..d061d855cd528 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php @@ -90,6 +90,8 @@ public function configureOptions(OptionsResolver $resolver) 'delete_empty' => false, )); + $resolver->setAllowedTypes('options', 'array'); + $resolver->setNormalizer('options', $optionsNormalizer); } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 8e79b7219994b..2f97dae18843c 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -1671,4 +1671,21 @@ public function testInitializeWithDefaultObjectChoice() // Trigger data initialization $form->getViewData(); } + + public function testChoicesOptionSupportsTraversable() + { + $this->factory->create('choice', null, array( + 'choices' => new \ArrayObject($this->choices), + )); + } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testSetInvalidPreferredChoicesOption() + { + $this->factory->create('choice', null, array( + 'preferred_choices' => 'bad value', + )); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php index 589ccdbb7ac21..bdb77140c7197 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php @@ -274,4 +274,14 @@ public function testPrototypeDefaultLabel() $this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']); } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testSetInvalidOptions() + { + $this->factory->create('collection', null, array( + 'options' => 'bad value', + )); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php index 16af981e624ab..8dbf7054ad2b4 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php @@ -50,4 +50,14 @@ public function testUnknownCountryIsNotIncluded() } } } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testSetInvalidChoices() + { + $this->factory->create('country', null, array( + 'choices' => 'bad value', + )); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CurrencyTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CurrencyTypeTest.php index 2d572d60b45df..870ddb8e4d32d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CurrencyTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CurrencyTypeTest.php @@ -34,4 +34,14 @@ public function testCurrenciesAreSelectable() $this->assertContains(new ChoiceView('USD', 'USD', 'US Dollar'), $choices, '', false, false); $this->assertContains(new ChoiceView('SIT', 'SIT', 'Slovenian Tolar'), $choices, '', false, false); } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testSetInvalidChoices() + { + $this->factory->create('currency', null, array( + 'choices' => 'bad value', + )); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php index ea6255d034b6b..e37b3dc6463c2 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php @@ -45,4 +45,14 @@ public function testMultipleLanguagesIsNotIncluded() $this->assertNotContains(new ChoiceView('mul', 'mul', 'Mehrsprachig'), $choices, '', false, false); } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testSetInvalidChoices() + { + $this->factory->create('language', null, array( + 'choices' => 'bad value', + )); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/LocaleTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LocaleTypeTest.php index 7f6d922ec9867..46ae9dec071ca 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/LocaleTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LocaleTypeTest.php @@ -34,4 +34,14 @@ public function testLocalesAreSelectable() $this->assertContains(new ChoiceView('en_GB', 'en_GB', 'English (United Kingdom)'), $choices, '', false, false); $this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false); } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testSetInvalidChoices() + { + $this->factory->create('locale', null, array( + 'choices' => 'bad value', + )); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php index a1bb3a53f7648..8ef3a91e2d87a 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php @@ -731,4 +731,14 @@ public function testThrowExceptionIfSecondsIsInvalid() 'seconds' => 'bad value', )); } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testThrowExceptionIfEmptyValueIsInvalid() + { + $this->factory->create('time', null, array( + 'empty_value' => array(), + )); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimezoneTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimezoneTypeTest.php index 05e234698404c..9c8bcc0b54ca9 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimezoneTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimezoneTypeTest.php @@ -27,4 +27,14 @@ public function testTimezonesAreSelectable() $this->assertArrayHasKey('America', $choices); $this->assertContains(new ChoiceView('America/New_York', 'America/New_York', 'New York'), $choices['America'], '', false, false); } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testSetInvalidChoices() + { + $this->factory->create('timezone', null, array( + 'choices' => 'bad value', + )); + } }