From 4a5dadb520323999430fab5f07afc224e0949258 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 26 Jul 2012 00:59:47 +0200 Subject: [PATCH] [Form] raise exception when label for choice cannot be read --- .../Form/Extension/Core/ChoiceList/ChoiceList.php | 5 +++++ .../Extension/Core/ChoiceList/ChoiceListTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php index 7ad15d04b2..fe4536eb42 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php @@ -254,12 +254,17 @@ class ChoiceList implements ChoiceListInterface * @param array $labels The labels corresponding to the choices. * @param array $preferredChoices The preferred choices. * + * @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) { // Add choices to the nested buckets foreach ($choices as $group => $choice) { + if (!isset($labels[$group])) { + throw new \InvalidArgumentException('The structures of the choices and labels array do not match.'); + } + if (is_array($choice)) { // Don't do the work if the array is empty if (count($choice) > 0) { diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php index bfd58ee583..86533e8af8 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php @@ -173,4 +173,15 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase $choices = array($this->obj2, $this->obj3, 'foobar'); $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices)); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testNonMatchingLabels() + { + $this->list = new ChoiceList( + array($this->obj1, $this->obj2), + array('A') + ); + } }