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 failing test regarding missing input #78

Open
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion src/PHPSemVerChecker/Console/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$configPath = $input->getOption('config');
$this->config = $configPath ? Configuration::fromFile($configPath) : Configuration::defaults('php-semver-checker');
$inputMerger = new InputMerger();
$inputMerger->merge($input, $this->config);
$inputMerger->merge($input, $this->getDefinition(), $this->config);

// Set overrides
LevelMapping::setOverrides($this->config->getLevelMapping());
Expand Down
29 changes: 20 additions & 9 deletions src/PHPSemVerChecker/Console/InputMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPSemVerChecker\Console;

use PHPSemVerChecker\Configuration\Configuration;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;

/**
Expand All @@ -14,24 +15,34 @@
class InputMerger
{
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \PHPSemVerChecker\Configuration\Configuration $config
* @param \Symfony\Component\Console\Input\InputInterface $input Actual input
* @param \Symfony\Component\Console\Input\InputDefinition $inputDefinition Definition of input arguments/options
* @param \PHPSemVerChecker\Configuration\Configuration $config
*/
public function merge(InputInterface $input, Configuration $config)
public function merge(InputInterface $input, InputDefinition $inputDefinition, Configuration $config)
{
foreach ($input->getArguments() as $argument => $value) {
if ($input->hasArgumentSet($argument)) {
if ($value !== null) {
$config->set($argument, $value);
} else {
$input->setArgument($argument, $config->get($argument));
$configValue = $config->get($argument);
// Only set an argument from config if actually known
if ($configValue !== null) {
$input->setArgument($argument, $configValue);
}
}
}

foreach ($input->getOptions() as $option => $value) {
if ($input->hasOptionSet($option)) {
$config->set($option, $value);
foreach ($input->getOptions() as $optionName => $value) {
$option = $inputDefinition->getOption($optionName);
// Make sure VALUE_NONE is only used when differing from default
if ((!$option->acceptValue() && $value !== $option->getDefault()) || ($option->acceptValue() && $value !== null)) {
$config->set($optionName, $value);
} else {
$input->setOption($option, $config->get($option));
$configValue = $config->get($optionName);
if ($configValue !== null) {
$input->setOption($optionName, $configValue);
}
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion tests/PHPSemVerChecker/Console/InputMergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,28 @@ public function testMerge()
$this->assertEquals('in-before cli', $input->getOption('include-before'), 'Test setup: Could not prepare input arguments');

$im = new InputMerger();
$im->merge($input, $config);
$im->merge($input, $command->getDefinition(), $config);
$this->assertEquals('in-before cli', $config->get('include-before'), 'Configuration must be overwritten by CLI option');
$this->assertEquals('src-before cli', $config->get('source-before'), 'Configuration must be overwritten by CLI argument');
$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');
}

/**
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
*/
public function testEmptyInputShouldThrowException()
{
// Default/empty configuration
$config = new Configuration([]);
// No input arguments
$input = new InspectableArgvInput([null]);
$command = new CompareCommand();
$input->bind($command->getDefinition());
$im = new InputMerger();
$im->merge($input, $command->getDefinition(), $config);
$input->validate();
}
}