From abc7480828c08098e2319f681e7b76d34077cc2c Mon Sep 17 00:00:00 2001 From: Fred Cox Date: Mon, 12 Mar 2018 19:35:41 +0200 Subject: [PATCH] Allow "json:" env var processor to parse null values Amend EnvVarProcessorTest to include all possible json values --- .../DependencyInjection/EnvVarProcessor.php | 4 +- .../Tests/Dumper/PhpDumperTest.php | 21 +++ .../Tests/EnvVarProcessorTest.php | 21 ++- .../Tests/Fixtures/php/services_json_env.php | 138 ++++++++++++++++++ 4 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php diff --git a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php index 1bb68a98cd923..43022ddb7b9d3 100644 --- a/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php +++ b/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php @@ -129,8 +129,8 @@ public function getEnv($prefix, $name, \Closure $getEnv) throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name)); } - if (!is_array($env)) { - throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env))); + if (null !== $env && !is_array($env)) { + throw new RuntimeException(sprintf('Invalid JSON env var "%s": array or null expected, %s given.', $name, gettype($env))); } return $env; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index cdeaac72cf787..89130902b4a11 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -432,6 +432,27 @@ public function testDumpedCsvEnvParameters() $this->assertSame(array('foo', 'bar'), $container->getParameter('hello')); } + public function testDumpedJsonEnvParameters() + { + $container = new ContainerBuilder(); + $container->setParameter('env(foo)', '["foo","bar"]'); + $container->setParameter('env(bar)', 'null'); + $container->setParameter('hello', '%env(json:foo)%'); + $container->setParameter('hello-bar', '%env(json:bar)%'); + $container->compile(); + + $dumper = new PhpDumper($container); + $dumper->dump(); + + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_json_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_JsonParameters'))); + + putenv('foobar="hello"'); + require self::$fixturesPath.'/php/services_json_env.php'; + $container = new \Symfony_DI_PhpDumper_Test_JsonParameters(); + $this->assertSame(array('foo', 'bar'), $container->getParameter('hello')); + $this->assertNull($container->getParameter('hello-bar')); + } + public function testCustomEnvParameters() { $container = new ContainerBuilder(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php index b1ee044e9e5c0..79b3e47c79de9 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php @@ -233,17 +233,29 @@ public function testGetEnvBase64() $this->assertSame('hello', $result); } - public function testGetEnvJson() + /** + * @dataProvider validJson + */ + public function testGetEnvJson($value, $processed) { $processor = new EnvVarProcessor(new Container()); - $result = $processor->getEnv('json', 'foo', function ($name) { + $result = $processor->getEnv('json', 'foo', function ($name) use ($value) { $this->assertSame('foo', $name); - return json_encode(array(1)); + return $value; }); - $this->assertSame(array(1), $result); + $this->assertSame($processed, $result); + } + + public function validJson() + { + return array( + array('[1]', array(1)), + array('{"key": "value"}', array('key' => 'value')), + array(null, null), + ); } /** @@ -284,6 +296,7 @@ public function otherJsonValues() array(1.1), array(true), array(false), + array('foo'), ); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php new file mode 100644 index 0000000000000..dd2930a424ba1 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php @@ -0,0 +1,138 @@ +parameters = $this->getDefaultParameters(); + + $this->services = $this->privates = array(); + + $this->aliases = array(); + } + + public function reset() + { + $this->privates = array(); + parent::reset(); + } + + public function compile() + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled() + { + return true; + } + + public function getRemovedIds() + { + return array( + 'Psr\\Container\\ContainerInterface' => true, + 'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true, + ); + } + + public function getParameter($name) + { + $name = (string) $name; + + if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) { + throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name)); + } + if (isset($this->loadedDynamicParameters[$name])) { + return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name); + } + + return $this->parameters[$name]; + } + + public function hasParameter($name) + { + $name = (string) $name; + + return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters); + } + + public function setParameter($name, $value) + { + throw new LogicException('Impossible to call set() on a frozen ParameterBag.'); + } + + public function getParameterBag() + { + if (null === $this->parameterBag) { + $parameters = $this->parameters; + foreach ($this->loadedDynamicParameters as $name => $loaded) { + $parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name); + } + $this->parameterBag = new FrozenParameterBag($parameters); + } + + return $this->parameterBag; + } + + private $loadedDynamicParameters = array( + 'hello' => false, + 'hello-bar' => false, + ); + private $dynamicParameters = array(); + + /** + * Computes a dynamic parameter. + * + * @param string The name of the dynamic parameter to load + * + * @return mixed The value of the dynamic parameter + * + * @throws InvalidArgumentException When the dynamic parameter does not exist + */ + private function getDynamicParameter($name) + { + switch ($name) { + case 'hello': $value = $this->getEnv('json:foo'); break; + case 'hello-bar': $value = $this->getEnv('json:bar'); break; + default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)); + } + $this->loadedDynamicParameters[$name] = true; + + return $this->dynamicParameters[$name] = $value; + } + + /** + * Gets the default parameters. + * + * @return array An array of the default parameters + */ + protected function getDefaultParameters() + { + return array( + 'env(foo)' => '["foo","bar"]', + 'env(bar)' => 'null', + ); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy