Added group_by tests for EntityChoiceList
This commit is contained in:
parent
040e988f0c
commit
882482a308
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
|
||||
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
|
||||
/** @Entity */
|
||||
class ItemGroupEntity
|
||||
{
|
||||
/** @Id @Column(type="integer") */
|
||||
protected $id;
|
||||
|
||||
/** @Column(type="string", nullable=true) */
|
||||
public $name;
|
||||
|
||||
/** @Column(type="string", nullable=true) */
|
||||
public $group_name;
|
||||
|
||||
public function __construct($id, $name, $group_name) {
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->group_name = $group_name;
|
||||
}
|
||||
}
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user