[Form] Simplified a bit FormUtil
and extended test coverage
This commit is contained in:
parent
a74ae9d325
commit
18a83c67f4
@ -15,11 +15,7 @@ abstract class FormUtil
|
|||||||
{
|
{
|
||||||
static public function toArrayKey($value)
|
static public function toArrayKey($value)
|
||||||
{
|
{
|
||||||
if ((string) (int) $value === (string) $value) {
|
if (is_bool($value) || (string) (int) $value === (string) $value) {
|
||||||
return (int) $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_bool($value)) {
|
|
||||||
return (int) $value;
|
return (int) $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +48,7 @@ abstract class FormUtil
|
|||||||
*/
|
*/
|
||||||
static public function isChoiceSelected($choice, $value)
|
static public function isChoiceSelected($choice, $value)
|
||||||
{
|
{
|
||||||
$choice = FormUtil::toArrayKey($choice);
|
$choice = static::toArrayKey($choice);
|
||||||
|
|
||||||
// The value should already have been converted by value transformers,
|
// The value should already have been converted by value transformers,
|
||||||
// otherwise we had to do the conversion on every call of this method
|
// otherwise we had to do the conversion on every call of this method
|
||||||
|
@ -50,4 +50,56 @@ class FormUtilTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertSame($out, FormUtil::toArrayKeys($in));
|
$this->assertSame($out, FormUtil::toArrayKeys($in));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isChoiceGroupProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(false, 0),
|
||||||
|
array(false, '0'),
|
||||||
|
array(false, '1'),
|
||||||
|
array(false, 1),
|
||||||
|
array(false, ''),
|
||||||
|
array(false, null),
|
||||||
|
array(false, true),
|
||||||
|
|
||||||
|
array(true, array()),
|
||||||
|
array(true, new \SplFixedArray(1)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider isChoiceGroupProvider
|
||||||
|
*/
|
||||||
|
public function testIsChoiceGroup($expected, $value)
|
||||||
|
{
|
||||||
|
$this->assertSame($expected, FormUtil::isChoiceGroup($value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isChoiceSelectedProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(true, 0, 0),
|
||||||
|
array(true, '0', 0),
|
||||||
|
array(true, '1', 1),
|
||||||
|
array(true, false, 0),
|
||||||
|
array(true, true, 1),
|
||||||
|
array(true, '', ''),
|
||||||
|
array(true, null, ''),
|
||||||
|
array(true, '1.23', '1.23'),
|
||||||
|
array(true, 'foo', 'foo'),
|
||||||
|
array(true, 'foo10', 'foo10'),
|
||||||
|
array(true, 'foo', array(1, 'foo', 'foo10')),
|
||||||
|
|
||||||
|
array(false, 10, array(1, 'foo', 'foo10')),
|
||||||
|
array(false, 0, array(1, 'foo', 'foo10')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider isChoiceSelectedProvider
|
||||||
|
*/
|
||||||
|
public function testIsChoiceSelected($expected, $choice, $value)
|
||||||
|
{
|
||||||
|
$this->assertSame($expected, FormUtil::isChoiceSelected($choice, $value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user