[DI] Fix accepting null as default env param value

This commit is contained in:
Nicolas Grekas 2016-11-14 05:39:55 -05:00
parent 67acdb053a
commit 7625166b4e
2 changed files with 16 additions and 7 deletions

View File

@ -96,7 +96,7 @@ class EnvPlaceholderParameterBag extends ParameterBag
}
if (is_numeric($default = $this->parameters[$name])) {
$this->parameters[$name] = (string) $default;
} elseif (!is_string($default)) {
} elseif (null !== $default && !is_string($default)) {
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be string or null, %s given.', $env, gettype($default)));
}
}

View File

@ -113,21 +113,30 @@ class EnvPlaceholderParameterBagTest extends \PHPUnit_Framework_TestCase
public function testResolveEnvCastsIntToString()
{
$bag = new EnvPlaceholderParameterBag();
$bag->get('env(INT)');
$bag->set('env(INT)', 2);
$bag->get('env(INT_VAR)');
$bag->set('env(Int_Var)', 2);
$bag->resolve();
$this->assertSame('2', $bag->all()['env(int)']);
$this->assertSame('2', $bag->all()['env(int_var)']);
}
public function testResolveEnvAllowsNull()
{
$bag = new EnvPlaceholderParameterBag();
$bag->get('env(NULL_VAR)');
$bag->set('env(Null_Var)', null);
$bag->resolve();
$this->assertNull($bag->all()['env(null_var)']);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of env parameter "ARRAY" must be string or null, array given.
* @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be string or null, array given.
*/
public function testResolveThrowsOnBadDefaultValue()
{
$bag = new EnvPlaceholderParameterBag();
$bag->get('env(ARRAY)');
$bag->set('env(ARRAY)', array());
$bag->get('env(ARRAY_VAR)');
$bag->set('env(Array_Var)', array());
$bag->resolve();
}
}