Просмотр исходного кода

[DoctrineBridge] enhanced an error message (closes #3155)

Fabien Potencier 13 лет назад
Родитель
Сommit
3c0b9c5b20

+ 1 - 1
src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php

@@ -185,7 +185,7 @@ class EntityChoiceList extends ArrayChoiceList
             } else {
                 // Otherwise expect a __toString() method in the entity
                 if (!method_exists($entity, '__toString')) {
-                    throw new FormException('Entities passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).');
+                    throw new FormException(sprintf('Entity "%s" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).', $this->class));
                 }
 
                 $value = (string) $entity;

+ 27 - 0
tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php

@@ -40,6 +40,33 @@ class EntityChoiceListTest extends DoctrineOrmTestCase
         $this->em = null;
     }
 
+    /**
+     * @expectedException Symfony\Component\Form\Exception\FormException
+     * @expectedMessage   Entity "Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).
+     */
+    public function testEntitesMustHaveAToStringMethod()
+    {
+        $entity1 = new SingleIdentEntity(1, 'Foo');
+        $entity2 = new SingleIdentEntity(2, 'Bar');
+
+        // Persist for managed state
+        $this->em->persist($entity1);
+        $this->em->persist($entity2);
+
+        $choiceList = new EntityChoiceList(
+            $this->em,
+            self::SINGLE_IDENT_CLASS,
+            null,
+            null,
+            array(
+                $entity1,
+                $entity2,
+            )
+        );
+
+        $choiceList->getEntities();
+    }
+
     /**
      * @expectedException Symfony\Component\Form\Exception\FormException
      */