diff --git a/tests/Symfony/Tests/Bridge/Doctrine/Fixtures/ItemGroupEntity.php b/tests/Symfony/Tests/Bridge/Doctrine/Fixtures/ItemGroupEntity.php new file mode 100644 index 0000000000..35c4c60f41 --- /dev/null +++ b/tests/Symfony/Tests/Bridge/Doctrine/Fixtures/ItemGroupEntity.php @@ -0,0 +1,26 @@ +id = $id; + $this->name = $name; + $this->group_name = $group_name; + } +} diff --git a/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php b/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php index 7e260a0c6e..a1bcc8846a 100644 --- a/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php +++ b/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php @@ -12,14 +12,18 @@ namespace Symfony\Tests\Bridge\Doctrine\Form\ChoiceList; require_once __DIR__.'/../DoctrineOrmTestCase.php'; +require_once __DIR__.'/../../Fixtures/ItemGroupEntity.php'; require_once __DIR__.'/../../Fixtures/SingleIdentEntity.php'; use Symfony\Tests\Bridge\Doctrine\Form\DoctrineOrmTestCase; +use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\ItemGroupEntity; use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity; use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList; class EntityChoiceListTest extends DoctrineOrmTestCase { + const ITEM_GROUP_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\ItemGroupEntity'; + const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity'; const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity'; @@ -113,4 +117,69 @@ class EntityChoiceListTest extends DoctrineOrmTestCase 'group2' => array(2 => 'Bar') ), $choiceList->getChoices()); } + + public function testGroupBySupportsString() + { + $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); + $item2 = new ItemGroupEntity(2, 'Bar', 'Group1'); + $item3 = new ItemGroupEntity(3, 'Baz', 'Group2'); + $item4 = new ItemGroupEntity(4, 'Boo!', null); + + $this->em->persist($item1); + $this->em->persist($item2); + $this->em->persist($item3); + $this->em->persist($item4); + + $choiceList = new EntityChoiceList( + $this->em, + self::ITEM_GROUP_CLASS, + 'name', + null, + array( + $item1, + $item2, + $item3, + $item4, + ), + 'group_name' + ); + + $this->assertEquals(array( + 'Group1' => array(1 => 'Foo', '2' => 'Bar'), + 'Group2' => array(3 => 'Baz'), + '4' => 'Boo!' + ), $choiceList->getChoices('choices')); + } + + public function testGroupBySupportsClosure() + { + $item1 = new ItemGroupEntity(1, 'Foo', 'Group1'); + $item2 = new ItemGroupEntity(2, 'Bar', 'Group2'); + $item3 = new ItemGroupEntity(3, 'Baz', null); + + $this->em->persist($item1); + $this->em->persist($item2); + $this->em->persist($item3); + + $choiceList = new EntityChoiceList( + $this->em, + self::ITEM_GROUP_CLASS, + 'name', + null, + array( + $item1, + $item2, + $item3, + ), + function($entity) { + return $entity->group_name; + } + ); + + $this->assertEquals(array( + 'Group1' => array(1 => 'Foo'), + 'Group2' => array(2 => 'Bar'), + '3' => 'Baz' + ), $choiceList->getChoices('choices')); + } }