[DependencyInjection] allow null for scalar nodes

This commit is contained in:
Johannes M. Schmitt 2011-02-06 20:40:21 +01:00 committed by Fabien Potencier
parent cdff8b2bf8
commit 2b256a0804
2 changed files with 57 additions and 1 deletions

View File

@ -8,6 +8,13 @@ use Symfony\Component\DependencyInjection\Configuration\Exception\InvalidTypeExc
/**
* This node represents a scalar value in the config tree.
*
* The following values are considered scalars:
* * booleans
* * strings
* * null
* * integers
* * floats
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ScalarNode extends BaseNode implements PrototypeNodeInterface
@ -44,7 +51,7 @@ class ScalarNode extends BaseNode implements PrototypeNodeInterface
protected function validateType($value)
{
if (!is_scalar($value)) {
if (!is_scalar($value) && null !== $value) {
throw new InvalidTypeException(sprintf(
'Invalid type for path "%s". Expected scalar, but got %s.',
$this->getPath(),

View File

@ -0,0 +1,49 @@
<?php
namespace Symfony\Component\DependencyInjection\Configuration;
class ScalarNodeTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getValidValues
*/
public function testNormalize($value)
{
$node = new ScalarNode('test');
$this->assertSame($value, $node->normalize($value));
}
public function getValidValues()
{
return array(
array(false),
array(true),
array(null),
array(''),
array('foo'),
array(0),
array(1),
array(0.0),
array(0.1),
);
}
/**
* @dataProvider getInvalidValues
* @expectedException Symfony\Component\DependencyInjection\Configuration\Exception\InvalidTypeException
*/
public function testNormalizeThrowsExceptionOnInvalidValues($value)
{
$node = new ScalarNode('test');
$node->normalize($value);
}
public function getInvalidValues()
{
return array(
array(array()),
array(array('foo' => 'bar')),
array(new \stdClass()),
);
}
}