diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php new file mode 100644 index 00000000..69c5aa0f --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php | |||
@@ -0,0 +1,212 @@ | |||
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\ObjectChoiceList; | ||
15 | use Symfony\Component\Form\Extension\Core\View\ChoiceView; | ||
16 | |||
17 | class ObjectChoiceListTest_EntityWithToString | ||
18 | { | ||
19 | private $property; | ||
20 | |||
21 | public function __construct($property) | ||
22 | { | ||
23 | $this->property = $property; | ||
24 | } | ||
25 | |||
26 | public function __toString() | ||
27 | { | ||
28 | return $this->property; | ||
29 | } | ||
30 | } | ||
31 | |||
32 | class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase | ||
33 | { | ||
34 | private $obj1; | ||
35 | |||
36 | private $obj2; | ||
37 | |||
38 | private $obj3; | ||
39 | |||
40 | private $obj4; | ||
41 | |||
42 | /** | ||
43 | * @var ObjectChoiceList | ||
44 | */ | ||
45 | private $list; | ||
46 | |||
47 | protected function setUp() | ||
48 | { | ||
49 | parent::setUp(); | ||
50 | |||
51 | $this->obj1 = (object) array('name' => 'A'); | ||
52 | $this->obj2 = (object) array('name' => 'B'); | ||
53 | $this->obj3 = (object) array('name' => 'C'); | ||
54 | $this->obj4 = (object) array('name' => 'D'); | ||
55 | |||
56 | $this->list = new ObjectChoiceList( | ||
57 | array( | ||
58 | 'Group 1' => array($this->obj1, $this->obj2), | ||
59 | 'Group 2' => array($this->obj3, $this->obj4), | ||
60 | ), | ||
61 | 'name', | ||
62 | array($this->obj2, $this->obj3) | ||
63 | ); | ||
64 | } | ||
65 | |||
66 | protected function tearDown() | ||
67 | { | ||
68 | parent::tearDown(); | ||
69 | |||
70 | $this->obj1 = null; | ||
71 | $this->obj2 = null; | ||
72 | $this->obj3 = null; | ||
73 | $this->obj4 = null; | ||
74 | $this->list = null; | ||
75 | } | ||
76 | |||
77 | public function testInitArray() | ||
78 | { | ||
79 | $this->list = new ObjectChoiceList( | ||
80 | array($this->obj1, $this->obj2, $this->obj3, $this->obj4), | ||
81 | 'name', | ||
82 | array($this->obj2) | ||
83 | ); | ||
84 | |||
85 | $this->assertSame(array($this->obj1, $this->obj2, $this->obj3, $this->obj4), $this->list->getChoices()); | ||
86 | $this->assertSame(array('0', '1', '2', '3'), $this->list->getValues()); | ||
87 | $this->assertEquals(array(1 => new ChoiceView($this->obj2, '1', 'B')), $this->list->getPreferredViews()); | ||
88 | $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()); | ||
89 | } | ||
90 | |||
91 | public function testInitNestedArray() | ||
92 | { | ||
93 | $this->assertSame(array($this->obj1, $this->obj2, $this->obj3, $this->obj4), $this->list->getChoices()); | ||
94 | $this->assertSame(array('0', '1', '2', '3'), $this->list->getValues()); | ||
95 | $this->assertEquals(array( | ||
96 | 'Group 1' => array(1 => new ChoiceView($this->obj2, '1', 'B')), | ||
97 | 'Group 2' => array(2 => new ChoiceView($this->obj3, '2', 'C')) | ||
98 | ), $this->list->getPreferredViews()); | ||
99 | $this->assertEquals(array( | ||
100 | 'Group 1' => array(0 => new ChoiceView($this->obj1, '0', 'A')), | ||
101 | 'Group 2' => array(3 => new ChoiceView($this->obj4, '3', 'D')) | ||
102 | ), $this->list->getRemainingViews()); | ||
103 | } | ||
104 | |||
105 | public function testInitArrayWithGroupPath() | ||
106 | { | ||
107 | $this->obj1 = (object) array('name' => 'A', 'category' => 'Group 1'); | ||
108 | $this->obj2 = (object) array('name' => 'B', 'category' => 'Group 1'); | ||
109 | $this->obj3 = (object) array('name' => 'C', 'category' => 'Group 2'); | ||
110 | $this->obj4 = (object) array('name' => 'D', 'category' => 'Group 2'); | ||
111 | |||
112 | // Objects with NULL groups are not grouped | ||
113 | $obj5 = (object) array('name' => 'E', 'category' => null); | ||
114 | |||
115 | // Objects without the group property are not grouped either | ||
116 | // see https://github.com/symfony/symfony/commit/d9b7abb7c7a0f28e0ce970afc5e305dce5dccddf | ||
117 | $obj6 = (object) array('name' => 'F'); | ||
118 | |||
119 | $this->list = new ObjectChoiceList( | ||
120 | array($this->obj1, $this->obj2, $this->obj3, $this->obj4, $obj5, $obj6), | ||
121 | 'name', | ||
122 | array($this->obj2, $this->obj3), | ||
123 | 'category' | ||
124 | ); | ||
125 | |||
126 | $this->assertSame(array($this->obj1, $this->obj2, $this->obj3, $this->obj4, $obj5, $obj6), $this->list->getChoices()); | ||
127 | $this->assertSame(array('0', '1', '2', '3', '4', '5'), $this->list->getValues()); | ||
128 | $this->assertEquals(array( | ||
129 | 'Group 1' => array(1 => new ChoiceView($this->obj2, '1', 'B')), | ||
130 | 'Group 2' => array(2 => new ChoiceView($this->obj3, '2', 'C')) | ||
131 | ), $this->list->getPreferredViews()); | ||
132 | $this->assertEquals(array( | ||
133 | 'Group 1' => array(0 => new ChoiceView($this->obj1, '0', 'A')), | ||
134 | 'Group 2' => array(3 => new ChoiceView($this->obj4, '3', 'D')), | ||
135 | 4 => new ChoiceView($obj5, '4', 'E'), | ||
136 | 5 => new ChoiceView($obj6, '5', 'F'), | ||
137 | ), $this->list->getRemainingViews()); | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * @expectedException \InvalidArgumentException | ||
142 | */ | ||
143 | public function testInitArrayWithGroupPathThrowsExceptionIfNestedArray() | ||
144 | { | ||
145 | $this->obj1 = (object) array('name' => 'A', 'category' => 'Group 1'); | ||
146 | $this->obj2 = (object) array('name' => 'B', 'category' => 'Group 1'); | ||
147 | $this->obj3 = (object) array('name' => 'C', 'category' => 'Group 2'); | ||
148 | $this->obj4 = (object) array('name' => 'D', 'category' => 'Group 2'); | ||
149 | |||
150 | new ObjectChoiceList( | ||
151 | array( | ||
152 | 'Group 1' => array($this->obj1, $this->obj2), | ||
153 | 'Group 2' => array($this->obj3, $this->obj4), | ||
154 | ), | ||
155 | 'name', | ||
156 | array($this->obj2, $this->obj3), | ||
157 | 'category' | ||
158 | ); | ||
159 | } | ||
160 | |||
161 | public function testInitArrayWithValuePath() | ||
162 | { | ||
163 | $this->obj1 = (object) array('name' => 'A', 'id' => 10); | ||
164 | $this->obj2 = (object) array('name' => 'B', 'id' => 20); | ||
165 | $this->obj3 = (object) array('name' => 'C', 'id' => 30); | ||
166 | $this->obj4 = (object) array('name' => 'D', 'id' => 40); | ||
167 | |||
168 | $this->list = new ObjectChoiceList( | ||
169 | array($this->obj1, $this->obj2, $this->obj3, $this->obj4), | ||
170 | 'name', | ||
171 | array($this->obj2, $this->obj3), | ||
172 | null, | ||
173 | 'id' | ||
174 | ); | ||
175 | |||
176 | $this->assertSame(array($this->obj1, $this->obj2, $this->obj3, $this->obj4), $this->list->getChoices()); | ||
177 | $this->assertSame(array('10', '20', '30', '40'), $this->list->getValues()); | ||
178 | $this->assertEquals(array(1 => new ChoiceView($this->obj2, '20', 'B'), 2 => new ChoiceView($this->obj3, '30', 'C')), $this->list->getPreferredViews()); | ||
179 | $this->assertEquals(array(0 => new ChoiceView($this->obj1, '10', 'A'), 3 => new ChoiceView($this->obj4, '40', 'D')), $this->list->getRemainingViews()); | ||
180 | } | ||
181 | |||
182 | public function testInitArrayUsesToString() | ||
183 | { | ||
184 | $this->obj1 = new ObjectChoiceListTest_EntityWithToString('A'); | ||
185 | $this->obj2 = new ObjectChoiceListTest_EntityWithToString('B'); | ||
186 | $this->obj3 = new ObjectChoiceListTest_EntityWithToString('C'); | ||
187 | $this->obj4 = new ObjectChoiceListTest_EntityWithToString('D'); | ||
188 | |||
189 | $this->list = new ObjectChoiceList( | ||
190 | array($this->obj1, $this->obj2, $this->obj3, $this->obj4) | ||
191 | ); | ||
192 | |||
193 | $this->assertSame(array($this->obj1, $this->obj2, $this->obj3, $this->obj4), $this->list->getChoices()); | ||
194 | $this->assertSame(array('0', '1', '2', '3'), $this->list->getValues()); | ||
195 | $this->assertEquals(array(0 => new ChoiceView($this->obj1, '0', 'A'), 1 => new ChoiceView($this->obj2, '1', 'B'), 2 => new ChoiceView($this->obj3, '2', 'C'), 3 => new ChoiceView($this->obj4, '3', 'D')), $this->list->getRemainingViews()); | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | * @expectedException \Symfony\Component\Form\Exception\StringCastException | ||
200 | */ | ||
201 | public function testInitArrayThrowsExceptionIfToStringNotFound() | ||
202 | { | ||
203 | $this->obj1 = new ObjectChoiceListTest_EntityWithToString('A'); | ||
204 | $this->obj2 = new ObjectChoiceListTest_EntityWithToString('B'); | ||
205 | $this->obj3 = (object) array('name' => 'C'); | ||
206 | $this->obj4 = new ObjectChoiceListTest_EntityWithToString('D'); | ||
207 | |||
208 | new ObjectChoiceList( | ||
209 | array($this->obj1, $this->obj2, $this->obj3, $this->obj4) | ||
210 | ); | ||
211 | } | ||
212 | } | ||