Skip to content

Commit

Permalink
[#75] InputMerger not merging options with defaults properly
Browse files Browse the repository at this point in the history
Add an InspectableArgvInput input class so that we may check if arguments/options are set.
  • Loading branch information
tomzx committed Jan 24, 2016
1 parent 7091479 commit fe2f305
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion bin/php-semver-checker
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ if (!$found) {
ini_set('xdebug.max_nesting_level', 5000);

$app = new PHPSemVerChecker\Console\Application();
$app->run();
$app->run(new PHPSemVerChecker\Console\InspectableArgvInput());
31 changes: 14 additions & 17 deletions src/PHPSemVerChecker/Console/InputMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@ class InputMerger
*/
public function merge(InputInterface $input, Configuration $config)
{
$missingArguments = $this->getKeysOfNullValues($input->getArguments());
foreach ($missingArguments as $key) {
$input->setArgument($key, $config->get($key));
foreach ($input->getArguments() as $argument => $value) {
if ($input->hasArgumentSet($argument)) {
$config->set($argument, $value);
} else {
$input->setArgument($argument, $config->get($argument));
}
}
$missingOptions = $this->getKeysOfNullValues($input->getOptions());
foreach ($missingOptions as $key) {
$input->setOption($key, $config->get($key));
}
$config->merge(array_merge($input->getArguments(), $input->getOptions()));
}

/**
* @param array $array
* @return array
*/
private function getKeysOfNullValues(array $array)
{
return array_keys(array_filter($array, 'is_null'));
foreach ($input->getOptions() as $option => $value) {
if ($input->hasOptionSet($option)) {
$config->set($option, $value);
} else {
$input->setOption($option, $config->get($option));
}
}
}
}
}
32 changes: 32 additions & 0 deletions src/PHPSemVerChecker/Console/InspectableArgvInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PHPSemVerChecker\Console;

use Symfony\Component\Console\Input\ArgvInput;

class InspectableArgvInput extends ArgvInput
{
/**
* Returns true if the argument value is set.
*
* @param string $name The argument name
*
* @return bool true if the argument is set (not a default value)
*/
public function hasArgumentSet($name)
{
return isset($this->arguments[$name]);
}

/**
* Returns true if the option value is set.
*
* @param string $name The option name
*
* @return bool true if the option is set (not a default value)
*/
public function hasOptionSet($name)
{
return isset($this->options[$name]);
}
}
5 changes: 4 additions & 1 deletion tests/PHPSemVerChecker/Console/InputMergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPSemVerChecker\Configuration\Configuration;
use PHPSemVerChecker\Console\Command\CompareCommand;
use PHPSemVerChecker\Console\InputMerger;
use PHPSemVerChecker\Console\InspectableArgvInput;
use Symfony\Component\Console\Input\StringInput;

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

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

0 comments on commit fe2f305

Please sign in to comment.