From 047492fafa5b973c38020fe65dbef503b2ab3e04 Mon Sep 17 00:00:00 2001 From: jaugustin Date: Thu, 2 Jan 2014 22:43:12 +0100 Subject: [PATCH] [Propel1Bridge][ModelChoiceList] add exception message for invalid classes --- .../Propel1/Form/ChoiceList/ModelChoiceList.php | 9 +++++++++ .../Form/ChoiceList/ModelChoiceListTest.php | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php index 6a5b757183..c3e6c5f6b1 100644 --- a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php +++ b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php @@ -17,6 +17,8 @@ use \Persistent; use Symfony\Component\Form\Exception\StringCastException; use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; +use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; /** @@ -84,6 +86,13 @@ class ModelChoiceList extends ObjectChoiceList $this->class = $class; $queryClass = $this->class.'Query'; + if (!class_exists($queryClass)) { + if (empty($this->class)) { + throw new MissingOptionsException('The "class" parameter is empty, you should provide the model class'); + } + throw new InvalidOptionsException(sprintf('The query class "%s" is not found, you should provide the FQCN of the model class', $queryClass)); + } + $query = new $queryClass(); $this->query = $queryObject ?: $query; diff --git a/src/Symfony/Bridge/Propel1/Tests/Form/ChoiceList/ModelChoiceListTest.php b/src/Symfony/Bridge/Propel1/Tests/Form/ChoiceList/ModelChoiceListTest.php index cf868fee5e..ef77b5edbb 100644 --- a/src/Symfony/Bridge/Propel1/Tests/Form/ChoiceList/ModelChoiceListTest.php +++ b/src/Symfony/Bridge/Propel1/Tests/Form/ChoiceList/ModelChoiceListTest.php @@ -243,4 +243,20 @@ class ModelChoiceListTest extends Propel1TestCase $this->assertEquals(array(), $choiceList->getValuesForChoices(array(new Item(2, 'Bar')))); $this->assertEquals(array(), $choiceList->getChoicesForValues(array(2))); } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException + */ + public function testEmptyClass() + { + $choiceList = new ModelChoiceList(''); + } + + /** + * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException + */ + public function testInvalidClass() + { + $choiceList = new ModelChoiceList('Foo\Bar\DoesNotExistClass'); + } }