10000 [Form] Improved test coverage of ChoiceList classes · symfony/symfony@58e7c10 · GitHub
[go: up one dir, main page]

Skip to content

Commit 58e7c10

Browse files
committed
[Form] Improved test coverage of ChoiceList classes
1 parent 9542d72 commit 58e7c10

File tree

5 files changed

+486
-316
lines changed

5 files changed

+486
-316
lines changed
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
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+
/**
15+
* @author Bernhard Schussek <bschussek@gmail.com>
16+
*/
17+
abstract class AbstractChoiceListTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
21+
*/
22+
protected $list;
23+
24+
/**
25+
* @var array
26+
*/
27+
protected $choices;
28+
29+
/**
30+
* @var array
31+
*/
32+
protected $values;
33+
34+
/**
35+
* @var array
36+
*/
37+
protected $indices;
38+
39+
/**
40+
* @var array
41+
*/
42+
protected $labels;
43+
44+
/**
45+
* @var mixed
46+
*/
47+
protected $choice1;
48+
49+
/**
50+
* @var mixed
51+
*/
52+
protected $choice2;
53+
54+
/**
55+
* @var mixed
56+
*/
57+
protected $choice3;
58+
59+
/**
60+
* @var mixed
61+
*/
62+
protected $choice4;
63+
64+
/**
65+
* @var string
66+
*/
67+
protected $value1;
68+
69+
/**
70+
* @var string
71+
*/
72+
protected $value2;
73+
74+
/**
75+
* @var string
76+
*/
77+
protected $value3;
78+
79+
/**
80+
* @var string
81+
*/
82+
protected $value4;
83+
84+
/**
85+
* @var int|string
86+
*/
87+
protected $index1;
88+
89+
/**
90+
* @var int|string
91+
*/
92+
protected $index2;
93+
94+
/**
95+
* @var int|string
96+
*/
97+
protected $index3;
98+
99+
/**
100+
* @var int|string
101+
*/
102+
protected $index4;
103+
104+
/**
105+
* @var string
106+
*/
107+
protected $label1;
108+
109+
/**
110+
* @var string
111+
*/
112+
protected $label2;
113+
114+
/**
115+
* @var string
116+
*/
117+
protected $label3;
118+
119+
/**
120+
* @var string
121+
*/
122+
protected $label4;
123+
124+
protected function setUp()
125+
{
126+
parent::setUp();
127+
128+
$this->list = $this->createChoiceList();
129+
130+
$this->choices = $this->getChoices();
131+
$this->indices = $this->getIndices();
132+
$this->values = $this->getValues();
133+
$this->labels = $this->getLabels();
134+
135+
// allow access to the individual entries without relying on their indices
136+
reset($this->choices);
137+
reset($this->indices);
138+
reset($this->values);
139+
reset($this->labels);
140+
141+
for ($i = 1; $i <= 4; ++$i) {
142+
$this->{'choice'.$i} = current($this->choices);
143+
$this->{'index'.$i} = current($this->indices);
144+
$this->{'value'.$i} = current($this->values);
145+
$this->{'label'.$i} = current($this->labels);
146+
147+
next($this->choices);
148+
next($this->indices);
149+
next($this->values);
150+
next($this->labels);
151+
}
152+
}
153+
154+
public function testGetChoices()
155+
{
156+
$this->assertSame($this->choices, $this->list->getChoices());
157+
}
158+
159+
public function testGetValues()
160+
{
161+
$this->assertSame($this->values, $this->list->getValues());
162+
}
163+
164+
public function testGetIndicesForChoices()
165+
{
166+
$choices = array($this->choice1, $this->choice2);
167+
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
168+
}
169+
170+
public function testGetIndicesForChoicesPreservesKeys()
171+
{
172+
$choices = array(5 => $this->choice1, 8 => $this->choice2);
173+
$this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForChoices($choices));
174+
}
175+
176+
public function testGetIndicesForChoicesPreservesOrder()
177+
{
178+
$choices = array($this->choice2, $this->choice1);
179+
$this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForChoices($choices));
180+
}
181+
182+
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
183+
{
184+
$choices = array($this->choice1, $this->choice2, 'foobar');
185+
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForChoices($choices));
186+
}
187+
188+
public function testGetIndicesForChoicesEmpty()
189+
{
190+
$this->assertSame(array(), $this->list->getIndicesForChoices(array()));
191+
}
192+
193+
public function testGetIndicesForValues()
194+
{
195+
// values and indices are always the same
196+
$values = array($this->value1, $this->value2);
197+
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
198+
}
199+
200+
public function testGetIndicesForValuesPreservesKeys()
201+
{
202+
// values and indices are always the same
203+
$values = array(5 => $this->value1, 8 => $this->value2);
204+
$this->assertSame(array(5 => $this->index1, 8 => $this->index2), $this->list->getIndicesForValues($values));
205+
}
206+
207+
public function testGetIndicesForValuesPreservesOrder()
208+
{
209+
$values = array($this->value2, $this->value1);
210+
$this->assertSame(array($this->index2, $this->index1), $this->list->getIndicesForValues($values));
211+
}
212+
213+
public function testGetIndicesForValuesIgnoresNonExistingValues()
214+
{
215+
$values = array($this->value1, $this->value2, 'foobar');
216+
$this->assertSame(array($this->index1, $this->index2), $this->list->getIndicesForValues($values));
217+
}
218+
219+
public function testGetIndicesForValuesEmpty()
220+
{
221+
$this->assertSame(array(), $this->list->getIndicesForValues(array()));
222+
}
223+
224+
public function testGetChoicesForValues()
225+
{
226+
$values = array($this->value1, $this->value2);
227+
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
228+
}
229+
230+
public function testGetChoicesForValuesPreservesKeys()
231+
{
232+
$values = array(5 => $this->value1, 8 => $this->value2);
233+
$this->assertSame(array(5 => $this->choice1, 8 => $this->choice2), $this->list->getChoicesForValues($values));
234+
}
235+
236+
public function testGetChoicesForValuesPreservesOrder()
237+
{
238+
$values = array($this->value2, $this->value1);
239+
$this->assertSame(array($this->choice2, $this->choice1), $this->list->getChoicesForValues($values));
240+
}
241+
242+
public function testGetChoicesForValuesIgnoresNonExistingValues()
243+
{
244+
$values = array($this->value1, $this->value2, 'foobar');
245+
$this->assertSame(array($this->choice1, $this->choice2), $this->list->getChoicesForValues($values));
246+
}
247+
248+
// https://github.com/symfony/symfony/issues/3446
249+
public function testGetChoicesForValuesEmpty()
250+
{
251+
$this->assertSame(array(), $this->list->getChoicesForValues(array()));
252+
}
253+
254+
public function testGetValuesForChoices()
255+
{
256+
$choices = array($this->choice1, $this->choice2);
257+
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
258+
}
259+
260+
261+
public function testGetValuesForChoicesPreservesKeys()
262+
{
263+
$choices = array(5 => $this->choice1, 8 => $this->choice2);
264+
$this->assertSame(array(5 => $this->value1, 8 => $this->value2), $this->list->getValuesForChoices($choices));
265+
}
266+
267+
268+
public function testGetValuesForChoicesPreservesOrder()
269+
{
270+
$choices = array($this->choice2, $this->choice1);
271+
$this->assertSame(array($this->value2, $this->value1), $this->list->getValuesForChoices($choices));
272+
}
273+
274+
public function testGetValuesForChoicesIgnoresNonExistingChoices()
275+
{
276+
$choices = array($this->choice1, $this->choice2, 'foobar');
277+
$this->assertSame(array($this->value1, $this->value2), $this->list->getValuesForChoices($choices));
278+
}
279+
280+
public function testGetValuesForChoicesEmpty()
281+
{
282+
$this->assertSame(array(), $this->list->getValuesForChoices(array()));
283+
}
284+
285+
/**
286+
* @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
287+
*/
288+
abstract protected function createChoiceList();
289+
290+
abstract protected function getChoices();
291+
292+
abstract protected function getLabels();
293+
294+
abstract protected function getValues();
295+
296+
abstract protected function getIndices();
297+
}

0 commit comments

Comments
 (0)
0