4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList
;
14 use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList
;
15 use Symfony\Component\Form\Extension\Core\View\ChoiceView
;
17 class ChoiceListTest
extends \PHPUnit_Framework_TestCase
29 protected function setUp()
33 $this->obj1
= new \
stdClass();
34 $this->obj2
= new \
stdClass();
35 $this->obj3
= new \
stdClass();
36 $this->obj4
= new \
stdClass();
38 $this->list = new ChoiceList(
40 'Group 1' => array($this->obj1
, $this->obj2
),
41 'Group 2' => array($this->obj3
, $this->obj4
),
44 'Group 1' => array('A', 'B'),
45 'Group 2' => array('C', 'D'),
47 array($this->obj2
, $this->obj3
)
51 protected function tearDown()
62 public function testInitArray()
64 $this->list = new ChoiceList(
65 array($this->obj1
, $this->obj2
, $this->obj3
, $this->obj4
),
66 array('A', 'B', 'C', 'D'),
70 $this->assertSame(array($this->obj1
, $this->obj2
, $this->obj3
, $this->obj4
), $this->list->getChoices());
71 $this->assertSame(array('0', '1', '2', '3'), $this->list->getValues());
72 $this->assertEquals(array(1 => new ChoiceView($this->obj2
, '1', 'B')), $this->list->getPreferredViews());
73 $this->assertEquals(array(0 => new ChoiceView($this->obj1
, '0', 'A'), 2 => new ChoiceView($this->obj3
, '2', 'C'), 3 => new ChoiceView($this->obj4
, '3', 'D')), $this->list->getRemainingViews());
77 * Necessary for interoperability with MongoDB cursors or ORM relations as
78 * choices parameter. A choice itself that is an object implementing \Traversable
79 * is not treated as hierarchical structure, but as-is.
81 public function testInitNestedTraversable()
83 $traversableChoice = new \
ArrayIterator(array($this->obj3
, $this->obj4
));
85 $this->list = new ChoiceList(
86 new \
ArrayIterator(array(
87 'Group' => array($this->obj1
, $this->obj2
),
88 'Not a Group' => $traversableChoice
91 'Group' => array('A', 'B'),
97 $this->assertSame(array($this->obj1
, $this->obj2
, $traversableChoice), $this->list->getChoices());
98 $this->assertSame(array('0', '1', '2'), $this->list->getValues());
99 $this->assertEquals(array(
100 'Group' => array(1 => new ChoiceView($this->obj2
, '1', 'B'))
101 ), $this->list->getPreferredViews());
102 $this->assertEquals(array(
103 'Group' => array(0 => new ChoiceView($this->obj1
, '0', 'A')),
104 2 => new ChoiceView($traversableChoice, '2', 'C')
105 ), $this->list->getRemainingViews());
108 public function testInitNestedArray()
110 $this->assertSame(array($this->obj1
, $this->obj2
, $this->obj3
, $this->obj4
), $this->list->getChoices());
111 $this->assertSame(array('0', '1', '2', '3'), $this->list->getValues());
112 $this->assertEquals(array(
113 'Group 1' => array(1 => new ChoiceView($this->obj2
, '1', 'B')),
114 'Group 2' => array(2 => new ChoiceView($this->obj3
, '2', 'C'))
115 ), $this->list->getPreferredViews());
116 $this->assertEquals(array(
117 'Group 1' => array(0 => new ChoiceView($this->obj1
, '0', 'A')),
118 'Group 2' => array(3 => new ChoiceView($this->obj4
, '3', 'D'))
119 ), $this->list->getRemainingViews());
122 public function testGetIndicesForChoices()
124 $choices = array($this->obj2
, $this->obj3
);
125 $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
128 public function testGetIndicesForChoicesIgnoresNonExistingChoices()
130 $choices = array($this->obj2
, $this->obj3
, 'foobar');
131 $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
134 public function testGetIndicesForValues()
136 // values and indices are always the same
137 $values = array('1', '2');
138 $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
141 public function testGetIndicesForValuesIgnoresNonExistingValues()
143 $values = array('1', '2', '5');
144 $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
147 public function testGetChoicesForValues()
149 $values = array('1', '2');
150 $this->assertSame(array($this->obj2
, $this->obj3
), $this->list->getChoicesForValues($values));
153 public function testGetChoicesForValuesCorrectOrderingOfResult()
155 $values = array('2', '1');
156 $this->assertSame(array($this->obj3
, $this->obj2
), $this->list->getChoicesForValues($values));
159 public function testGetChoicesForValuesIgnoresNonExistingValues()
161 $values = array('1', '2', '5');
162 $this->assertSame(array($this->obj2
, $this->obj3
), $this->list->getChoicesForValues($values));
165 public function testGetValuesForChoices()
167 $choices = array($this->obj2
, $this->obj3
);
168 $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
171 public function testGetValuesForChoicesIgnoresNonExistingChoices()
173 $choices = array($this->obj2
, $this->obj3
, 'foobar');
174 $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
178 * @expectedException \InvalidArgumentException
180 public function testNonMatchingLabels()
182 $this->list = new ChoiceList(
183 array($this->obj1
, $this->obj2
),
188 public function testLabelsContainingNull()
190 $this->list = new ChoiceList(
191 array($this->obj1
, $this->obj2
),
196 array(0 => new ChoiceView($this->obj1
, '0', 'A'), 1 => new ChoiceView($this->obj2
, '1', null)),
197 $this->list->getRemainingViews()