bug #15170 [Config] type specific check for emptiness (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Config] type specific check for emptiness

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13736
| License       | MIT
| Doc PR        |

Commits
-------

0199fbf [Config] type specific check for emptiness
This commit is contained in:
Fabien Potencier 2015-08-01 08:17:42 +02:00
commit 9293c439a5
8 changed files with 129 additions and 1 deletions

View File

@ -36,4 +36,13 @@ class BooleanNode extends ScalarNode
throw $ex;
}
}
/**
* {@inheritdoc}
*/
protected function isValueEmpty($value)
{
// a boolean value cannot be empty
return false;
}
}

View File

@ -52,4 +52,13 @@ class NumericNode extends ScalarNode
return $value;
}
/**
* {@inheritdoc}
*/
protected function isValueEmpty($value)
{
// a numeric value cannot be empty
return false;
}
}

View File

@ -43,4 +43,12 @@ class ScalarNode extends VariableNode
throw $ex;
}
}
/**
* {@inheritdoc}
*/
protected function isValueEmpty($value)
{
return null === $value || '' === $value;
}
}

View File

@ -84,7 +84,7 @@ class VariableNode extends BaseNode implements PrototypeNodeInterface
*/
protected function finalizeValue($value)
{
if (!$this->allowEmptyValue && empty($value)) {
if (!$this->allowEmptyValue && $this->isValueEmpty($value)) {
$ex = new InvalidConfigurationException(sprintf(
'The path "%s" cannot contain an empty value, but got %s.',
$this->getPath(),
@ -113,4 +113,20 @@ class VariableNode extends BaseNode implements PrototypeNodeInterface
{
return $rightSide;
}
/**
* Evaluates if the given value is to be treated as empty.
*
* By default, PHP's empty() function is used to test for emptiness. This
* method may be overridden by subtypes to better match their understanding
* of empty data.
*
* @param mixed $value
*
* @return bool
*/
protected function isValueEmpty($value)
{
return empty($value);
}
}

View File

@ -24,6 +24,19 @@ class BooleanNodeTest extends \PHPUnit_Framework_TestCase
$this->assertSame($value, $node->normalize($value));
}
/**
* @dataProvider getValidValues
*
* @param bool $value
*/
public function testValidNonEmptyValues($value)
{
$node = new BooleanNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidValues()
{
return array(

View File

@ -24,6 +24,19 @@ class FloatNodeTest extends \PHPUnit_Framework_TestCase
$this->assertSame($value, $node->normalize($value));
}
/**
* @dataProvider getValidValues
*
* @param int $value
*/
public function testValidNonEmptyValues($value)
{
$node = new FloatNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidValues()
{
return array(

View File

@ -24,6 +24,19 @@ class IntegerNodeTest extends \PHPUnit_Framework_TestCase
$this->assertSame($value, $node->normalize($value));
}
/**
* @dataProvider getValidValues
*
* @param int $value
*/
public function testValidNonEmptyValues($value)
{
$node = new IntegerNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidValues()
{
return array(

View File

@ -57,4 +57,51 @@ class ScalarNodeTest extends \PHPUnit_Framework_TestCase
array(new \stdClass()),
);
}
/**
* @dataProvider getValidNonEmptyValues
*
* @param mixed $value
*/
public function testValidNonEmptyValues($value)
{
$node = new ScalarNode('test');
$node->setAllowEmptyValue(false);
$this->assertSame($value, $node->finalize($value));
}
public function getValidNonEmptyValues()
{
return array(
array(false),
array(true),
array('foo'),
array(0),
array(1),
array(0.0),
array(0.1),
);
}
/**
* @dataProvider getEmptyValues
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*
* @param mixed $value
*/
public function testNotAllowedEmptyValuesThrowException($value)
{
$node = new ScalarNode('test');
$node->setAllowEmptyValue(false);
$node->finalize($value);
}
public function getEmptyValues()
{
return array(
array(null),
array(''),
);
}
}