Skip to content

Adds the option to throw an exception on missing parameters #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
);
Expand All @@ -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']);

Expand Down
3 changes: 3 additions & 0 deletions Tests/fixtures/testcases/exception_when_missing/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
provided: foo
missing: bar
11 changes: 11 additions & 0 deletions Tests/fixtures/testcases/exception_when_missing/setup.yml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
provided: foo
missing: bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
provided: bar
missing: bar
Original file line number Diff line number Diff line change
@@ -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