[Console] Fix input validation when required arguments are missing

Previous rule was only working when arguments are passed from command line, as in command line there is no way of skipping an argument. The rule does not work for arguments set on the Input after a command is run.
This commit is contained in:
Jakub Zalas 2015-08-13 08:25:37 +01:00
parent b185056776
commit f12a4c1aee
2 changed files with 21 additions and 3 deletions

View File

@ -72,8 +72,15 @@ abstract class Input implements InputInterface
*/
public function validate()
{
if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
throw new \RuntimeException('Not enough arguments.');
$definition = $this->definition;
$givenArguments = $this->arguments;
$missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
});
if (count($missingArguments) > 0) {
throw new \RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
}
}

View File

@ -94,7 +94,7 @@ class InputTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Not enough arguments.
* @expectedExceptionMessage Not enough arguments (missing: "name").
*/
public function testValidateWithMissingArguments()
{
@ -103,6 +103,17 @@ class InputTest extends \PHPUnit_Framework_TestCase
$input->validate();
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Not enough arguments (missing: "name").
*/
public function testValidateWithMissingRequiredArguments()
{
$input = new ArrayInput(array('bar' => 'baz'));
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL))));
$input->validate();
}
public function testValidate()
{
$input = new ArrayInput(array('name' => 'foo'));