Skip to content

Commit 9122b11

Browse files
committed
add setting to throw exception in non-interactive mode if some params are missing
1 parent ea15f3d commit 9122b11

File tree

10 files changed

+62
-4
lines changed

10 files changed

+62
-4
lines changed

Processor.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private function processParams(array $config, array $expectedParams, array $actu
104104
// Add the params coming from the environment values
105105
$actualParams = array_replace($actualParams, $this->getEnvValues($envMap));
106106

107-
return $this->getParams($expectedParams, $actualParams);
107+
return $this->getParams($config, $expectedParams, $actualParams);
108108
}
109109

110110
private function getEnvValues(array $envMap)
@@ -137,10 +137,14 @@ private function processRenamedValues(array $renameMap, array $actualParams)
137137
return $actualParams;
138138
}
139139

140-
private function getParams(array $expectedParams, array $actualParams)
140+
private function getParams(array $config, array $expectedParams, array $actualParams)
141141
{
142-
// Simply use the expectedParams value as default for the missing params.
143142
if (!$this->io->isInteractive()) {
143+
if (!empty($config['exception-if-missing']) && $missingParams = array_diff_key($expectedParams, $actualParams)) {
144+
// Throw exception in non-interactive mode if some params are missing
145+
throw new \RuntimeException(sprintf("Some parameters are missing: %s. Please fill them.", implode(", ", array_keys($missingParams))));
146+
}
147+
// Simply use the expectedParams value as default for the missing params
144148
return array_replace($expectedParams, $actualParams);
145149
}
146150

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ will be used for missing parameters.
6666
your parameters.yml file so handle with care. If you want to give format
6767
and comments to your parameter's file you should do it on your dist version.
6868

69+
### Throwing exception if some parameters are missing in non-interactive mode
70+
71+
You can also force the script handler to throw an exception when some parameters
72+
are missing in non-interactive mode by using `exception-if-missing` param in the configuration:
73+
74+
```json
75+
{
76+
"extra": {
77+
"incenteev-parameters": {
78+
"exception-if-missing": true
79+
}
80+
}
81+
}
82+
```
83+
6984
### Keeping outdated parameters
7085

7186
Warning: This script removes outdated params from ``parameters.yml`` which are not in ``parameters.yml.dist``

Tests/ProcessorTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public function testParameterHandling($testCaseName)
115115

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

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

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

149151
if (!$testCase['interactive']) {
152+
if (!empty($testCase['config']['exception-if-missing']) && !empty($testCase['missing_params'])) {
153+
$this->setExpectedException(
154+
'RuntimeException',
155+
"Some parameters are missing: ".implode(", ", $testCase['missing_params']).". Please fill them.");
156+
}
150157
return;
151158
}
152159

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
foo: bar
3+
another: test
4+
boolean: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This file is auto-generated during the composer install
2+
parameters:
3+
foo: existing_foo
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: Missing keys involves exception thrown in non-interaction mode
2+
3+
config:
4+
exception-if-missing: true
5+
6+
missing_params: ['another', 'boolean']
7+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
foo: bar
3+
another: test
4+
boolean: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is auto-generated during the composer install
2+
parameters:
3+
foo: existing_foo
4+
another: existing_test
5+
boolean: false
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is auto-generated during the composer install
2+
parameters:
3+
foo: existing_foo
4+
another: existing_test
5+
boolean: false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
title: No exception is thrown in non-interaction mode if all the required params are provided
2+
3+
config:
4+
exception-if-missing: true

0 commit comments

Comments
 (0)