From 7625166b4e1f5bbd41bf31d9975f1c86b06970be Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 14 Nov 2016 05:39:55 -0500 Subject: [PATCH] [DI] Fix accepting null as default env param value --- .../EnvPlaceholderParameterBag.php | 2 +- .../EnvPlaceholderParameterBagTest.php | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php b/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php index 0e7b116b6c..46d2a739dc 100644 --- a/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php +++ b/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php @@ -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))); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php b/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php index 6f055dab7d..1a24b3d6e2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ParameterBag/EnvPlaceholderParameterBagTest.php @@ -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(); } }