]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Tests / Extension / Core / ChoiceList / SimpleChoiceListTest.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\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 }