]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Core / ChoiceList / ChoiceListTest.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList;
13
14 use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
15 use Symfony\Component\Form\Extension\Core\View\ChoiceView;
16
17 class ChoiceListTest extends \PHPUnit_Framework_TestCase
18 {
19 private $obj1;
20
21 private $obj2;
22
23 private $obj3;
24
25 private $obj4;
26
27 private $list;
28
29 protected function setUp()
30 {
31 parent::setUp();
32
33 $this->obj1 = new \stdClass();
34 $this->obj2 = new \stdClass();
35 $this->obj3 = new \stdClass();
36 $this->obj4 = new \stdClass();
37
38 $this->list = new ChoiceList(
39 array(
40 'Group 1' => array($this->obj1, $this->obj2),
41 'Group 2' => array($this->obj3, $this->obj4),
42 ),
43 array(
44 'Group 1' => array('A', 'B'),
45 'Group 2' => array('C', 'D'),
46 ),
47 array($this->obj2, $this->obj3)
48 );
49 }
50
51 protected function tearDown()
52 {
53 parent::tearDown();
54
55 $this->obj1 = null;
56 $this->obj2 = null;
57 $this->obj3 = null;
58 $this->obj4 = null;
59 $this->list = null;
60 }
61
62 public function testInitArray()
63 {
64 $this->list = new ChoiceList(
65 array($this->obj1, $this->obj2, $this->obj3, $this->obj4),
66 array('A', 'B', 'C', 'D'),
67 array($this->obj2)
68 );
69
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());
74 }
75
76 /**
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.
80 */
81 public function testInitNestedTraversable()
82 {
83 $traversableChoice = new \ArrayIterator(array($this->obj3, $this->obj4));
84
85 $this->list = new ChoiceList(
86 new \ArrayIterator(array(
87 'Group' => array($this->obj1, $this->obj2),
88 'Not a Group' => $traversableChoice
89 )),
90 array(
91 'Group' => array('A', 'B'),
92 'Not a Group' => 'C',
93 ),
94 array($this->obj2)
95 );
96
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());
106 }
107
108 public function testInitNestedArray()
109 {
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());
120 }
121
122 public function testGetIndicesForChoices()
123 {
124 $choices = array($this->obj2, $this->obj3);
125 $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
126 }
127
128 public function testGetIndicesForChoicesIgnoresNonExistingChoices()
129 {
130 $choices = array($this->obj2, $this->obj3, 'foobar');
131 $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
132 }
133
134 public function testGetIndicesForValues()
135 {
136 // values and indices are always the same
137 $values = array('1', '2');
138 $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
139 }
140
141 public function testGetIndicesForValuesIgnoresNonExistingValues()
142 {
143 $values = array('1', '2', '5');
144 $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
145 }
146
147 public function testGetChoicesForValues()
148 {
149 $values = array('1', '2');
150 $this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
151 }
152
153 public function testGetChoicesForValuesCorrectOrderingOfResult()
154 {
155 $values = array('2', '1');
156 $this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values));
157 }
158
159 public function testGetChoicesForValuesIgnoresNonExistingValues()
160 {
161 $values = array('1', '2', '5');
162 $this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
163 }
164
165 public function testGetValuesForChoices()
166 {
167 $choices = array($this->obj2, $this->obj3);
168 $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
169 }
170
171 public function testGetValuesForChoicesIgnoresNonExistingChoices()
172 {
173 $choices = array($this->obj2, $this->obj3, 'foobar');
174 $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
175 }
176
177 /**
178 * @expectedException \InvalidArgumentException
179 */
180 public function testNonMatchingLabels()
181 {
182 $this->list = new ChoiceList(
183 array($this->obj1, $this->obj2),
184 array('A')
185 );
186 }
187
188 public function testLabelsContainingNull()
189 {
190 $this->list = new ChoiceList(
191 array($this->obj1, $this->obj2),
192 array('A', null)
193 );
194
195 $this->assertEquals(
196 array(0 => new ChoiceView($this->obj1, '0', 'A'), 1 => new ChoiceView($this->obj2, '1', null)),
197 $this->list->getRemainingViews()
198 );
199 }
200 }