Skip to content

Commit 18a83c6

Browse files
committed
[Form] Simplified a bit FormUtil and extended test coverage
1 parent a74ae9d commit 18a83c6

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

src/Symfony/Component/Form/Util/FormUtil.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ abstract class FormUtil
1515
{
1616
static public function toArrayKey($value)
1717
{
18-
if ((string) (int) $value === (string) $value) {
19-
return (int) $value;
20-
}
21-
22-
if (is_bool($value)) {
18+
if (is_bool($value) || (string) (int) $value === (string) $value) {
2319
return (int) $value;
2420
}
2521

@@ -52,7 +48,7 @@ static public function isChoiceGroup($choice)
5248
*/
5349
static public function isChoiceSelected($choice, $value)
5450
{
55-
$choice = FormUtil::toArrayKey($choice);
51+
$choice = static::toArrayKey($choice);
5652

5753
// The value should already have been converted by value transformers,
5854
// otherwise we had to do the conversion on every call of this method

tests/Symfony/Tests/Component/Form/Util/FormUtilTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,56 @@ public function testToArrayKeys()
5050

5151
$this->assertSame($out, FormUtil::toArrayKeys($in));
5252
}
53+
54+
public function isChoiceGroupProvider()
55+
{
56+
return array(
57+
array(false, 0),
58+
array(false, '0'),
59+
array(false, '1'),
60+
array(false, 1),
61+
array(false, ''),
62+
array(false, null),
63+
array(false, true),
64+
65+
array(true, array()),
66+
array(true, new \SplFixedArray(1)),
67+
);
68+
}
69+
70+
/**
71+
* @dataProvider isChoiceGroupProvider
72+
*/
73+
public function testIsChoiceGroup($expected, $value)
74+
{
75+
$this->assertSame($expected, FormUtil::isChoiceGroup($value));
76+
}
77+
78+
public function isChoiceSelectedProvider()
79+
{
80+
return array(
81+
array(true, 0, 0),
82+
array(true, '0', 0),
83+
array(true, '1', 1),
84+
array(true, false, 0),
85+
array(true, true, 1),
86+
array(true, '', ''),
87+
array(true, null, ''),
88+
array(true, '1.23', '1.23'),
89+
array(true, 'foo', 'foo'),
90+
array(true, 'foo10', 'foo10'),
91+
array(true, 'foo', array(1, 'foo', 'foo10')),
92+
93+
array(false, 10, array(1, 'foo', 'foo10')),
94+
array(false, 0, array(1, 'foo', 'foo10')),
95+
);
96+
}
97+
98+
/**
99+
* @dataProvider isChoiceSelectedProvider
100+
*/
101+
public function testIsChoiceSelected($expected, $choice, $value)
102+
{
103+
$this->assertSame($expected, FormUtil::isChoiceSelected($choice, $value));
104+
}
53105
}

0 commit comments

Comments
 (0)