Skip to content

Commit debbb26

Browse files
fduchAlexey Medvedev
authored and
Alexey Medvedev
committed
add setting to throw exception in non-interactive mode if some params are missing
1 parent 933c45a commit debbb26

File tree

10 files changed

+61
-4
lines changed

10 files changed

+61
-4
lines changed

Processor.php

+7-3
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

+15
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

+8-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ public function testParameterHandling($testCaseName)
120120

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

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

126128
private function initializeTestCase(array $testCase, $dataDir, $workingDir)
@@ -152,6 +154,11 @@ private function setInteractionExpectations(array $testCase)
152154
$this->io->isInteractive()->willReturn($testCase['interactive']);
153155

154156
if (!$testCase['interactive']) {
157+
if (!empty($testCase['config']['exception-if-missing']) && !empty($testCase['missing_params'])) {
158+
$this->setExpectedException(
159+
'RuntimeException',
160+
"Some parameters are missing: ".implode(", ", $testCase['missing_params']).". Please fill them.");
161+
}
155162
return;
156163
}
157164

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
foo: bar
3+
another: test
4+
boolean: true
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
foo: bar
3+
another: test
4+
boolean: true
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
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
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)