bug #20512 [DI] Fix accepting null as default env param value (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[DI] Fix accepting null as default env param value

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

Commits
-------

7625166 [DI] Fix accepting null as default env param value
This commit is contained in:
Fabien Potencier 2016-11-15 18:04:19 -05:00
commit f0aa6636c0
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();
}
}