[Propel1Bridge][ModelChoiceList] add exception message for invalid classes

This commit is contained in:
jaugustin 2014-01-02 22:43:12 +01:00
parent 5519a3d948
commit 047492fafa
2 changed files with 25 additions and 0 deletions

View File

@ -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;

View File

@ -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');
}
}