From 2ab23ea86a0abb6f6a7494d62c7243042065df16 Mon Sep 17 00:00:00 2001 From: Silvan Thus Date: Wed, 18 Jan 2017 16:10:19 +0100 Subject: [PATCH 1/3] Adds the option to throw an exception on missing parameters in non-interactive mode https://github.com/Incenteev/ParameterHandler/issues/105 --- Processor.php | 16 +++++++++++++--- README.md | 13 ++++++++++++- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Processor.php b/Processor.php index 6dc208f..4dc653c 100644 --- a/Processor.php +++ b/Processor.php @@ -104,7 +104,7 @@ private function processParams(array $config, array $expectedParams, array $actu // Add the params coming from the environment values $actualParams = array_replace($actualParams, $this->getEnvValues($envMap)); - return $this->getParams($expectedParams, $actualParams); + return $this->getParams($config, $expectedParams, $actualParams); } private function getEnvValues(array $envMap) @@ -137,10 +137,20 @@ private function processRenamedValues(array $renameMap, array $actualParams) return $actualParams; } - private function getParams(array $expectedParams, array $actualParams) + private function getParams(array $config, array $expectedParams, array $actualParams) { - // Simply use the expectedParams value as default for the missing params. if (!$this->io->isInteractive()) { + if (isset($config['exception-when-missing'])) { + foreach ($expectedParams as $key => $message) { + if (array_key_exists($key, $actualParams)) { + continue; + } + + throw new \InvalidArgumentException(sprintf('Some parameters are missing. Please provide them. Missing %s', $key)); + } + } + + // Simply use the expectedParams value as default for the missing params. return array_replace($expectedParams, $actualParams); } diff --git a/README.md b/README.md index 0f42600..9c60718 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,18 @@ in the parameters file, using the value of the dist file as default value. All prompted values are parsed as inline Yaml, to allow you to define ``true``, ``false``, ``null`` or numbers easily. If composer is run in a non-interactive mode, the values of the dist file -will be used for missing parameters. +will be used for missing parameters, unless the ``exception-when-missing`` is set +in the configuration, then an exception is thrown. Example: + +```json +{ + "extra": { + "incenteev-parameters": { + "exception-when-missing": true + } + } +} +``` **Warning:** This parameters handler will overwrite any comments or spaces into your parameters.yml file so handle with care. If you want to give format From 9cf0f8a82ac846b964fefe73d8fd363ab662b920 Mon Sep 17 00:00:00 2001 From: Silvan Thus Date: Wed, 18 Jan 2017 18:30:04 +0100 Subject: [PATCH 2/3] Setting 'exception-when-missing' to false will now not throw --- Processor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Processor.php b/Processor.php index 4dc653c..7c5bd79 100644 --- a/Processor.php +++ b/Processor.php @@ -140,7 +140,7 @@ private function processRenamedValues(array $renameMap, array $actualParams) private function getParams(array $config, array $expectedParams, array $actualParams) { if (!$this->io->isInteractive()) { - if (isset($config['exception-when-missing'])) { + if (!empty($config['exception-when-missing']) && Inline::parse($config['exception-when-missing'])) { foreach ($expectedParams as $key => $message) { if (array_key_exists($key, $actualParams)) { continue; From 111e11180749921c889240c253ce832a3b8992a3 Mon Sep 17 00:00:00 2001 From: Silvan Thus Date: Wed, 18 Jan 2017 19:17:44 +0100 Subject: [PATCH 3/3] Adds tests: throws exception when missing when option exception-when-missing is true, and combines dist when exception-when-missing is false --- Tests/ProcessorTest.php | 5 +++++ .../testcases/exception_when_missing/dist.yml | 3 +++ .../testcases/exception_when_missing/setup.yml | 11 +++++++++++ .../testcases/exception_when_missing_false/dist.yml | 3 +++ .../exception_when_missing_false/expected.yml | 4 ++++ .../testcases/exception_when_missing_false/setup.yml | 9 +++++++++ 6 files changed, 35 insertions(+) create mode 100644 Tests/fixtures/testcases/exception_when_missing/dist.yml create mode 100644 Tests/fixtures/testcases/exception_when_missing/setup.yml create mode 100644 Tests/fixtures/testcases/exception_when_missing_false/dist.yml create mode 100644 Tests/fixtures/testcases/exception_when_missing_false/expected.yml create mode 100644 Tests/fixtures/testcases/exception_when_missing_false/setup.yml diff --git a/Tests/ProcessorTest.php b/Tests/ProcessorTest.php index babf44a..d3dd95e 100644 --- a/Tests/ProcessorTest.php +++ b/Tests/ProcessorTest.php @@ -101,6 +101,7 @@ public function testParameterHandling($testCaseName) 'dist-file' => 'parameters.yml.dist', 'environment' => array(), 'interactive' => false, + 'expect-exception' => false, ), (array) Yaml::parse(file_get_contents($dataDir.'/setup.yml')) ); @@ -112,6 +113,10 @@ public function testParameterHandling($testCaseName) $this->io->write($message)->shouldBeCalled(); $this->setInteractionExpectations($testCase); + + if (!empty($testCase['expect-exception'])) { + $this->setExpectedException($testCase['expect-exception']); + } $this->processor->processFile($testCase['config']); diff --git a/Tests/fixtures/testcases/exception_when_missing/dist.yml b/Tests/fixtures/testcases/exception_when_missing/dist.yml new file mode 100644 index 0000000..e919734 --- /dev/null +++ b/Tests/fixtures/testcases/exception_when_missing/dist.yml @@ -0,0 +1,3 @@ +parameters: + provided: foo + missing: bar diff --git a/Tests/fixtures/testcases/exception_when_missing/setup.yml b/Tests/fixtures/testcases/exception_when_missing/setup.yml new file mode 100644 index 0000000..869033e --- /dev/null +++ b/Tests/fixtures/testcases/exception_when_missing/setup.yml @@ -0,0 +1,11 @@ +title: Missing keys in the environment will cause a throw when the option is set to true + +config: + env-map: + provided: IC_TEST_PROVIDED + exception-when-missing: true + +environment: + IC_TEST_PROVIDED: bar + +expect-exception: "InvalidArgumentException" diff --git a/Tests/fixtures/testcases/exception_when_missing_false/dist.yml b/Tests/fixtures/testcases/exception_when_missing_false/dist.yml new file mode 100644 index 0000000..e919734 --- /dev/null +++ b/Tests/fixtures/testcases/exception_when_missing_false/dist.yml @@ -0,0 +1,3 @@ +parameters: + provided: foo + missing: bar diff --git a/Tests/fixtures/testcases/exception_when_missing_false/expected.yml b/Tests/fixtures/testcases/exception_when_missing_false/expected.yml new file mode 100644 index 0000000..a0069f5 --- /dev/null +++ b/Tests/fixtures/testcases/exception_when_missing_false/expected.yml @@ -0,0 +1,4 @@ +# This file is auto-generated during the composer install +parameters: + provided: bar + missing: bar diff --git a/Tests/fixtures/testcases/exception_when_missing_false/setup.yml b/Tests/fixtures/testcases/exception_when_missing_false/setup.yml new file mode 100644 index 0000000..4cf2600 --- /dev/null +++ b/Tests/fixtures/testcases/exception_when_missing_false/setup.yml @@ -0,0 +1,9 @@ +title: Missing keys in the environment will not cause a throw when the option is set to false + +config: + env-map: + provided: IC_TEST_PROVIDED + exception-when-missing: false + +environment: + IC_TEST_PROVIDED: bar