Skip to content

Commit f12a4c1

Browse files
committed
[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.
1 parent b185056 commit f12a4c1

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Symfony/Component/Console/Input/Input.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,15 @@ abstract protected function parse();
7272
*/
7373
public function validate()
7474
{
75-
if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) {
76-
throw new \RuntimeException('Not enough arguments.');
75+
$definition = $this->definition;
76+
$givenArguments = $this->arguments;
77+
78+
$missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
79+
return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
80+
});
81+
82+
if (count($missingArguments) > 0) {
83+
throw new \RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
7784
}
7885
}
7986

src/Symfony/Component/Console/Tests/Input/InputTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testGetInvalidArgument()
9494

9595
/**
9696
* @expectedException \RuntimeException
97-
* @expectedExceptionMessage Not enough arguments.
97+
* @expectedExceptionMessage Not enough arguments (missing: "name").
9898
*/
9999
public function testValidateWithMissingArguments()
100100
{
@@ -103,6 +103,17 @@ public function testValidateWithMissingArguments()
103103
$input->validate();
104104
}
105105

106+
/**
107+
* @expectedException \RuntimeException
108+
* @expectedExceptionMessage Not enough arguments (missing: "name").
109+
*/
110+
public function testValidateWithMissingRequiredArguments()
111+
{
112+
$input = new ArrayInput(array('bar' => 'baz'));
113+
$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED), new InputArgument('bar', InputArgument::OPTIONAL))));
114+
$input->validate();
115+
}
116+
106117
public function testValidate()
107118
{
108119
$input = new ArrayInput(array('name' => 'foo'));

0 commit comments

Comments
 (0)