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:
commit
f0aa6636c0
@ -96,7 +96,7 @@ class EnvPlaceholderParameterBag extends ParameterBag
|
|||||||
}
|
}
|
||||||
if (is_numeric($default = $this->parameters[$name])) {
|
if (is_numeric($default = $this->parameters[$name])) {
|
||||||
$this->parameters[$name] = (string) $default;
|
$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)));
|
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be string or null, %s given.', $env, gettype($default)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,21 +113,30 @@ class EnvPlaceholderParameterBagTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testResolveEnvCastsIntToString()
|
public function testResolveEnvCastsIntToString()
|
||||||
{
|
{
|
||||||
$bag = new EnvPlaceholderParameterBag();
|
$bag = new EnvPlaceholderParameterBag();
|
||||||
$bag->get('env(INT)');
|
$bag->get('env(INT_VAR)');
|
||||||
$bag->set('env(INT)', 2);
|
$bag->set('env(Int_Var)', 2);
|
||||||
$bag->resolve();
|
$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
|
* @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()
|
public function testResolveThrowsOnBadDefaultValue()
|
||||||
{
|
{
|
||||||
$bag = new EnvPlaceholderParameterBag();
|
$bag = new EnvPlaceholderParameterBag();
|
||||||
$bag->get('env(ARRAY)');
|
$bag->get('env(ARRAY_VAR)');
|
||||||
$bag->set('env(ARRAY)', array());
|
$bag->set('env(Array_Var)', array());
|
||||||
$bag->resolve();
|
$bag->resolve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user