Skip to content
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

Add setting to throw exception in non-interactive mode if some params are missing #64

Open
wants to merge 1 commit 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
10 changes: 7 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,14 @@ 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-if-missing']) && $missingParams = array_diff_key($expectedParams, $actualParams)) {
// Throw exception in non-interactive mode if some params are missing
throw new \RuntimeException(sprintf("Some parameters are missing: %s. Please fill them.", implode(", ", array_keys($missingParams))));
}
// Simply use the expectedParams value as default for the missing params
return array_replace($expectedParams, $actualParams);
}

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ will be used for missing parameters.
your parameters.yml file so handle with care. If you want to give format
and comments to your parameter's file you should do it on your dist version.

### Throwing exception if some parameters are missing in non-interactive mode

You can also force the script handler to throw an exception when some parameters
are missing in non-interactive mode by using `exception-if-missing` param in the configuration:

```json
{
"extra": {
"incenteev-parameters": {
"exception-if-missing": true
}
}
}
```

### Keeping outdated parameters

Warning: This script removes outdated params from ``parameters.yml`` which are not in ``parameters.yml.dist``
Expand Down
9 changes: 8 additions & 1 deletion Tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ public function testParameterHandling($testCaseName)

$this->processor->processFile($testCase['config']);

$this->assertFileEquals($dataDir.'/expected.yml', $workingDir.'/'.$testCase['config']['file'], $testCase['title']);
if (file_exists($expected = $dataDir.'/expected.yml')) {
$this->assertFileEquals($expected, $workingDir.'/'.$testCase['config']['file'], $testCase['title']);
}
}

private function initializeTestCase(array $testCase, $dataDir, $workingDir)
Expand Down Expand Up @@ -147,6 +149,11 @@ private function setInteractionExpectations(array $testCase)
$this->io->isInteractive()->willReturn($testCase['interactive']);

if (!$testCase['interactive']) {
if (!empty($testCase['config']['exception-if-missing']) && !empty($testCase['missing_params'])) {
$this->setExpectedException(
'RuntimeException',
"Some parameters are missing: ".implode(", ", $testCase['missing_params']).". Please fill them.");
}
return;
}

Expand Down
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/exception_if_missing/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
foo: bar
another: test
boolean: true
3 changes: 3 additions & 0 deletions Tests/fixtures/testcases/exception_if_missing/existing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
6 changes: 6 additions & 0 deletions Tests/fixtures/testcases/exception_if_missing/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: Missing keys involves exception thrown in non-interaction mode

config:
exception-if-missing: true

missing_params: ['another', 'boolean']
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/no_exception_if_no_missing/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
foo: bar
another: test
boolean: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
another: existing_test
boolean: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
another: existing_test
boolean: false
4 changes: 4 additions & 0 deletions Tests/fixtures/testcases/no_exception_if_no_missing/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
title: No exception is thrown in non-interaction mode if all the required params are provided

config:
exception-if-missing: true