diff --git a/Processor.php b/Processor.php index 6dc208f..7c5bd79 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 (!empty($config['exception-when-missing']) && Inline::parse($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 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