diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList')
4 files changed, 716 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php new file mode 100644 index 00000000..63eae9bf --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php | |||
@@ -0,0 +1,200 @@ | |||
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 | } | ||
diff --git a/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/LazyChoiceListTest.php b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/LazyChoiceListTest.php new file mode 100644 index 00000000..bcd309e0 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/LazyChoiceListTest.php | |||
@@ -0,0 +1,116 @@ | |||
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\SimpleChoiceList; | ||
15 | use Symfony\Component\Form\Extension\Core\ChoiceList\LazyChoiceList; | ||
16 | use Symfony\Component\Form\Extension\Core\View\ChoiceView; | ||
17 | |||
18 | class LazyChoiceListTest extends \PHPUnit_Framework_TestCase | ||
19 | { | ||
20 | private $list; | ||
21 | |||
22 | protected function setUp() | ||
23 | { | ||
24 | parent::setUp(); | ||
25 | |||
26 | $this->list = new LazyChoiceListTest_Impl(new SimpleChoiceList(array( | ||
27 | 'a' => 'A', | ||
28 | 'b' => 'B', | ||
29 | 'c' => 'C', | ||
30 | ), array('b'))); | ||
31 | } | ||
32 | |||
33 | protected function tearDown() | ||
34 | { | ||
35 | parent::tearDown(); | ||
36 | |||
37 | $this->list = null; | ||
38 | } | ||
39 | |||
40 | public function testGetChoices() | ||
41 | { | ||
42 | $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices()); | ||
43 | } | ||
44 | |||
45 | public function testGetValues() | ||
46 | { | ||
47 | $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getValues()); | ||
48 | } | ||
49 | |||
50 | public function testGetPreferredViews() | ||
51 | { | ||
52 | $this->assertEquals(array(1 => new ChoiceView('b', 'b', 'B')), $this->list->getPreferredViews()); | ||
53 | } | ||
54 | |||
55 | public function testGetRemainingViews() | ||
56 | { | ||
57 | $this->assertEquals(array(0 => new ChoiceView('a', 'a', 'A'), 2 => new ChoiceView('c', 'c', 'C')), $this->list->getRemainingViews()); | ||
58 | } | ||
59 | |||
60 | public function testGetIndicesForChoices() | ||
61 | { | ||
62 | $choices = array('b', 'c'); | ||
63 | $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); | ||
64 | } | ||
65 | |||
66 | public function testGetIndicesForValues() | ||
67 | { | ||
68 | $values = array('b', 'c'); | ||
69 | $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); | ||
70 | } | ||
71 | |||
72 | public function testGetChoicesForValues() | ||
73 | { | ||
74 | $values = array('b', 'c'); | ||
75 | $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); | ||
76 | } | ||
77 | |||
78 | public function testGetValuesForChoices() | ||
79 | { | ||
80 | $choices = array('b', 'c'); | ||
81 | $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices)); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException | ||
86 | */ | ||
87 | public function testLoadChoiceListShouldReturnChoiceList() | ||
88 | { | ||
89 | $list = new LazyChoiceListTest_InvalidImpl(); | ||
90 | |||
91 | $list->getChoices(); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | class LazyChoiceListTest_Impl extends LazyChoiceList | ||
96 | { | ||
97 | private $choiceList; | ||
98 | |||
99 | public function __construct($choiceList) | ||
100 | { | ||
101 | $this->choiceList = $choiceList; | ||
102 | } | ||
103 | |||
104 | protected function loadChoiceList() | ||
105 | { | ||
106 | return $this->choiceList; | ||
107 | } | ||
108 | } | ||
109 | |||
110 | class LazyChoiceListTest_InvalidImpl extends LazyChoiceList | ||
111 | { | ||
112 | protected function loadChoiceList() | ||
113 | { | ||
114 | return new \stdClass(); | ||
115 | } | ||
116 | } | ||
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 | } | ||
diff --git a/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php new file mode 100644 index 00000000..69d27a18 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php | |||
@@ -0,0 +1,188 @@ | |||
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\ChoiceList\SimpleChoiceList; | ||
16 | use Symfony\Component\Form\Extension\Core\View\ChoiceView; | ||
17 | |||
18 | class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase | ||
19 | { | ||
20 | private $list; | ||
21 | |||
22 | private $numericList; | ||
23 | |||
24 | protected function setUp() | ||
25 | { | ||
26 | parent::setUp(); | ||
27 | |||
28 | $choices = array( | ||
29 | 'Group 1' => array('a' => 'A', 'b' => 'B'), | ||
30 | 'Group 2' => array('c' => 'C', 'd' => 'D'), | ||
31 | ); | ||
32 | $numericChoices = array( | ||
33 | 'Group 1' => array(0 => 'A', 1 => 'B'), | ||
34 | 'Group 2' => array(2 => 'C', 3 => 'D'), | ||
35 | ); | ||
36 | |||
37 | $this->list = new SimpleChoiceList($choices, array('b', 'c')); | ||
38 | |||
39 | // Use COPY_CHOICE strategy to test for the various associated problems | ||
40 | $this->numericList = new SimpleChoiceList($numericChoices, array(1, 2)); | ||
41 | } | ||
42 | |||
43 | protected function tearDown() | ||
44 | { | ||
45 | parent::tearDown(); | ||
46 | |||
47 | $this->list = null; | ||
48 | $this->numericList = null; | ||
49 | } | ||
50 | |||
51 | public function testInitArray() | ||
52 | { | ||
53 | $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C'); | ||
54 | $this->list = new SimpleChoiceList($choices, array('b')); | ||
55 | |||
56 | $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices()); | ||
57 | $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getValues()); | ||
58 | $this->assertEquals(array(1 => new ChoiceView('b', 'b', 'B')), $this->list->getPreferredViews()); | ||
59 | $this->assertEquals(array(0 => new ChoiceView('a', 'a', 'A'), 2 => new ChoiceView('c', 'c', 'C')), $this->list->getRemainingViews()); | ||
60 | } | ||
61 | |||
62 | public function testInitNestedArray() | ||
63 | { | ||
64 | $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $this->list->getChoices()); | ||
65 | $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $this->list->getValues()); | ||
66 | $this->assertEquals(array( | ||
67 | 'Group 1' => array(1 => new ChoiceView('b', 'b', 'B')), | ||
68 | 'Group 2' => array(2 => new ChoiceView('c', 'c', 'C')) | ||
69 | ), $this->list->getPreferredViews()); | ||
70 | $this->assertEquals(array( | ||
71 | 'Group 1' => array(0 => new ChoiceView('a', 'a', 'A')), | ||
72 | 'Group 2' => array(3 => new ChoiceView('d', 'd', 'D')) | ||
73 | ), $this->list->getRemainingViews()); | ||
74 | } | ||
75 | |||
76 | public function testGetIndicesForChoices() | ||
77 | { | ||
78 | $choices = array('b', 'c'); | ||
79 | $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); | ||
80 | } | ||
81 | |||
82 | public function testGetIndicesForChoicesIgnoresNonExistingChoices() | ||
83 | { | ||
84 | $choices = array('b', 'c', 'foobar'); | ||
85 | $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices)); | ||
86 | } | ||
87 | |||
88 | public function testGetIndicesForChoicesDealsWithNumericChoices() | ||
89 | { | ||
90 | // Pass choices as strings although they are integers | ||
91 | $choices = array('0', '1'); | ||
92 | $this->assertSame(array(0, 1), $this->numericList->getIndicesForChoices($choices)); | ||
93 | } | ||
94 | |||
95 | public function testGetIndicesForValues() | ||
96 | { | ||
97 | $values = array('b', 'c'); | ||
98 | $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); | ||
99 | } | ||
100 | |||
101 | public function testGetIndicesForValuesIgnoresNonExistingValues() | ||
102 | { | ||
103 | $values = array('b', 'c', '100'); | ||
104 | $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values)); | ||
105 | } | ||
106 | |||
107 | public function testGetIndicesForValuesDealsWithNumericValues() | ||
108 | { | ||
109 | // Pass values as strings although they are integers | ||
110 | $values = array('0', '1'); | ||
111 | $this->assertSame(array(0, 1), $this->numericList->getIndicesForValues($values)); | ||
112 | } | ||
113 | |||
114 | public function testGetChoicesForValues() | ||
115 | { | ||
116 | $values = array('b', 'c'); | ||
117 | $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); | ||
118 | } | ||
119 | |||
120 | public function testGetChoicesForValuesIgnoresNonExistingValues() | ||
121 | { | ||
122 | $values = array('b', 'c', '100'); | ||
123 | $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values)); | ||
124 | } | ||
125 | |||
126 | public function testGetChoicesForValuesDealsWithNumericValues() | ||
127 | { | ||
128 | // Pass values as strings although they are integers | ||
129 | $values = array('0', '1'); | ||
130 | $this->assertSame(array(0, 1), $this->numericList->getChoicesForValues($values)); | ||
131 | } | ||
132 | |||
133 | public function testGetValuesForChoices() | ||
134 | { | ||
135 | $choices = array('b', 'c'); | ||
136 | $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices)); | ||
137 | } | ||
138 | |||
139 | public function testGetValuesForChoicesIgnoresNonExistingValues() | ||
140 | { | ||
141 | $choices = array('b', 'c', 'foobar'); | ||
142 | $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices)); | ||
143 | } | ||
144 | |||
145 | public function testGetValuesForChoicesDealsWithNumericValues() | ||
146 | { | ||
147 | // Pass values as strings although they are integers | ||
148 | $values = array('0', '1'); | ||
149 | |||
150 | $this->assertSame(array('0', '1'), $this->numericList->getValuesForChoices($values)); | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * @dataProvider dirtyValuesProvider | ||
155 | */ | ||
156 | public function testGetValuesForChoicesDealsWithDirtyValues($choice, $value) | ||
157 | { | ||
158 | $choices = array( | ||
159 | '0' => 'Zero', | ||
160 | '1' => 'One', | ||
161 | '' => 'Empty', | ||
162 | '1.23' => 'Float', | ||
163 | 'foo' => 'Foo', | ||
164 | 'foo10' => 'Foo 10', | ||
165 | ); | ||
166 | |||
167 | // use COPY_CHOICE strategy to test the problems | ||
168 | $this->list = new SimpleChoiceList($choices, array()); | ||
169 | |||
170 | $this->assertSame(array($value), $this->list->getValuesForChoices(array($choice))); | ||
171 | } | ||
172 | |||
173 | public function dirtyValuesProvider() | ||
174 | { | ||
175 | return array( | ||
176 | array(0, '0'), | ||
177 | array('0', '0'), | ||
178 | array('1', '1'), | ||
179 | array(false, '0'), | ||
180 | array(true, '1'), | ||
181 | array('', ''), | ||
182 | array(null, ''), | ||
183 | array('1.23', '1.23'), | ||
184 | array('foo', 'foo'), | ||
185 | array('foo10', 'foo10'), | ||
186 | ); | ||
187 | } | ||
188 | } | ||