[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:
parent
b185056776
commit
f12a4c1aee
@ -72,8 +72,15 @@ abstract class Input implements InputInterface
|
|||||||
*/
|
*/
|
||||||
public function validate()
|
public function validate()
|
||||||
{
|
{
|
||||||
if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
|
$definition = $this->definition;
|
||||||
throw new \RuntimeException('Not enough arguments.');
|
$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)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ class InputTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
* @expectedExceptionMessage Not enough arguments.
|
* @expectedExceptionMessage Not enough arguments (missing: "name").
|
||||||
*/
|
*/
|
||||||
public function testValidateWithMissingArguments()
|
public function testValidateWithMissingArguments()
|
||||||
{
|
{
|
||||||
@ -103,6 +103,17 @@ class InputTest extends \PHPUnit_Framework_TestCase
|
|||||||
$input->validate();
|
$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()
|
public function testValidate()
|
||||||
{
|
{
|
||||||
$input = new ArrayInput(array('name' => 'foo'));
|
$input = new ArrayInput(array('name' => 'foo'));
|
||||||
|
Reference in New Issue
Block a user