4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\ChoiceList
;
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
;
18 class SimpleChoiceListTest
extends \PHPUnit_Framework_TestCase
24 protected function setUp()
29 'Group 1' => array('a' => 'A', 'b' => 'B'),
30 'Group 2' => array('c' => 'C', 'd' => 'D'),
32 $numericChoices = array(
33 'Group 1' => array(0 => 'A', 1 => 'B'),
34 'Group 2' => array(2 => 'C', 3 => 'D'),
37 $this->list = new SimpleChoiceList($choices, array('b', 'c'));
39 // Use COPY_CHOICE strategy to test for the various associated problems
40 $this->numericList
= new SimpleChoiceList($numericChoices, array(1, 2));
43 protected function tearDown()
48 $this->numericList
= null;
51 public function testInitArray()
53 $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
54 $this->list = new SimpleChoiceList($choices, array('b'));
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());
62 public function testInitNestedArray()
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());
76 public function testGetIndicesForChoices()
78 $choices = array('b', 'c');
79 $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
82 public function testGetIndicesForChoicesIgnoresNonExistingChoices()
84 $choices = array('b', 'c', 'foobar');
85 $this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
88 public function testGetIndicesForChoicesDealsWithNumericChoices()
90 // Pass choices as strings although they are integers
91 $choices = array('0', '1');
92 $this->assertSame(array(0, 1), $this->numericList
->getIndicesForChoices($choices));
95 public function testGetIndicesForValues()
97 $values = array('b', 'c');
98 $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
101 public function testGetIndicesForValuesIgnoresNonExistingValues()
103 $values = array('b', 'c', '100');
104 $this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
107 public function testGetIndicesForValuesDealsWithNumericValues()
109 // Pass values as strings although they are integers
110 $values = array('0', '1');
111 $this->assertSame(array(0, 1), $this->numericList
->getIndicesForValues($values));
114 public function testGetChoicesForValues()
116 $values = array('b', 'c');
117 $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
120 public function testGetChoicesForValuesIgnoresNonExistingValues()
122 $values = array('b', 'c', '100');
123 $this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
126 public function testGetChoicesForValuesDealsWithNumericValues()
128 // Pass values as strings although they are integers
129 $values = array('0', '1');
130 $this->assertSame(array(0, 1), $this->numericList
->getChoicesForValues($values));
133 public function testGetValuesForChoices()
135 $choices = array('b', 'c');
136 $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
139 public function testGetValuesForChoicesIgnoresNonExistingValues()
141 $choices = array('b', 'c', 'foobar');
142 $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
145 public function testGetValuesForChoicesDealsWithNumericValues()
147 // Pass values as strings although they are integers
148 $values = array('0', '1');
150 $this->assertSame(array('0', '1'), $this->numericList
->getValuesForChoices($values));
154 * @dataProvider dirtyValuesProvider
156 public function testGetValuesForChoicesDealsWithDirtyValues($choice, $value)
167 // use COPY_CHOICE strategy to test the problems
168 $this->list = new SimpleChoiceList($choices, array());
170 $this->assertSame(array($value), $this->list->getValuesForChoices(array($choice)));
173 public function dirtyValuesProvider()
183 array('1.23', '1.23'),
185 array('foo10', 'foo10'),