Skip to content

Commit fe2f305

Browse files
committed
[#75] InputMerger not merging options with defaults properly
Add an InspectableArgvInput input class so that we may check if arguments/options are set.
1 parent 7091479 commit fe2f305

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

bin/php-semver-checker

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ if (!$found) {
2525
ini_set('xdebug.max_nesting_level', 5000);
2626

2727
$app = new PHPSemVerChecker\Console\Application();
28-
$app->run();
28+
$app->run(new PHPSemVerChecker\Console\InspectableArgvInput());

src/PHPSemVerChecker/Console/InputMerger.php

+14-17
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ class InputMerger
1919
*/
2020
public function merge(InputInterface $input, Configuration $config)
2121
{
22-
$missingArguments = $this->getKeysOfNullValues($input->getArguments());
23-
foreach ($missingArguments as $key) {
24-
$input->setArgument($key, $config->get($key));
22+
foreach ($input->getArguments() as $argument => $value) {
23+
if ($input->hasArgumentSet($argument)) {
24+
$config->set($argument, $value);
25+
} else {
26+
$input->setArgument($argument, $config->get($argument));
27+
}
2528
}
26-
$missingOptions = $this->getKeysOfNullValues($input->getOptions());
27-
foreach ($missingOptions as $key) {
28-
$input->setOption($key, $config->get($key));
29-
}
30-
$config->merge(array_merge($input->getArguments(), $input->getOptions()));
31-
}
3229

33-
/**
34-
* @param array $array
35-
* @return array
36-
*/
37-
private function getKeysOfNullValues(array $array)
38-
{
39-
return array_keys(array_filter($array, 'is_null'));
30+
foreach ($input->getOptions() as $option => $value) {
31+
if ($input->hasOptionSet($option)) {
32+
$config->set($option, $value);
33+
} else {
34+
$input->setOption($option, $config->get($option));
35+
}
36+
}
4037
}
41-
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Console;
4+
5+
use Symfony\Component\Console\Input\ArgvInput;
6+
7+
class InspectableArgvInput extends ArgvInput
8+
{
9+
/**
10+
* Returns true if the argument value is set.
11+
*
12+
* @param string $name The argument name
13+
*
14+
* @return bool true if the argument is set (not a default value)
15+
*/
16+
public function hasArgumentSet($name)
17+
{
18+
return isset($this->arguments[$name]);
19+
}
20+
21+
/**
22+
* Returns true if the option value is set.
23+
*
24+
* @param string $name The option name
25+
*
26+
* @return bool true if the option is set (not a default value)
27+
*/
28+
public function hasOptionSet($name)
29+
{
30+
return isset($this->options[$name]);
31+
}
32+
}

tests/PHPSemVerChecker/Console/InputMergerTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPSemVerChecker\Configuration\Configuration;
66
use PHPSemVerChecker\Console\Command\CompareCommand;
77
use PHPSemVerChecker\Console\InputMerger;
8+
use PHPSemVerChecker\Console\InspectableArgvInput;
89
use Symfony\Component\Console\Input\StringInput;
910

1011
class InputMergerTest extends \PHPUnit_Framework_TestCase
@@ -15,9 +16,10 @@ public function testMerge()
1516
// Prepare options and arguments
1617
$config->set('include-before', 'in-before config');
1718
$config->set('source-after', 'src-after config');
19+
$config->set('full-path', true);
1820

1921
// Specify options and arguments for input
20-
$input = new StringInput('--include-before "in-before cli" "src-before cli"');
22+
$input = new InspectableArgvInput([null, '--include-before', 'in-before cli', 'src-before cli']);
2123
$command = new CompareCommand();
2224
$input->bind($command->getDefinition());
2325
$this->assertEquals('in-before cli', $input->getOption('include-before'), 'Test setup: Could not prepare input arguments');
@@ -29,5 +31,6 @@ public function testMerge()
2931
$this->assertEquals('src-before cli', $input->getArgument('source-before'), 'Input arguments must not be overwritten by empty configuration');
3032
$this->assertEquals('src-after config', $config->get('source-after'), 'Configuration must not be overwritten by empty CLI argument');
3133
$this->assertEquals('src-after config', $input->getArgument('source-after'), 'Missing input arguments must take on existing configuration');
34+
$this->assertEquals(true, $config->get('full-path'), 'CLI option should use Configuration value and not CLI default');
3235
}
3336
}

0 commit comments

Comments
 (0)