8000 [Console] Fix command testing with missing inputs · symfony/symfony@89a8b0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 89a8b0e

Browse files
author
Robin Chalas
committed
[Console] Fix command testing with missing inputs
1 parent 37b0eeb commit 89a8b0e

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private function doAsk(OutputInterface $output, Question $question)
166166
if (false === $ret) {
167167
$ret = fgets($inputStream, 4096);
168168
if (false === $ret) {
169-
throw new RuntimeException('Aborted');
169+
throw new RuntimeException('Aborted.');
170170
}
171171
$ret = trim($ret);
172172
}
@@ -252,8 +252,10 @@ private function autocomplete(OutputInterface $output, Question $question, $inpu
252252
while (!feof($inputStream)) {
253253
$c = fread($inputStream, 1);
254254

255-
// Backspace Character
256-
if ("\177" === $c) {
255+
// as opposed to fgets(), fread() returns an empty string when the stream content is empty, not false.
256+
if (false === $c || ('' === $ret && '' === $c && null === $question->getDefault())) {
257+
throw new RuntimeException('Aborted.');
258+
} else if ("\177" === $c) { // Backspace Character
257259
if (0 === $numMatches && 0 !== $i) {
258260
--$i;
259261
// Move cursor backwards
@@ -380,7 +382,7 @@ private function getHiddenResponse(OutputInterface $output, $inputStream)
380382
shell_exec(sprintf('stty %s', $sttyMode));
381383

382384
if (false === $value) {
383-
throw new RuntimeException('Aborted');
385+
throw new RuntimeException('Aborted.');
384386
}
385387

386388
$value = trim($value);

src/Symfony/Component/Console/Tester/CommandTester.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ public function execute(array $input, array $options = [])
6262
}
6363

6464
$this->input = new ArrayInput($input);
65-
if ($this->inputs) {
66-
$this->input->setStream(self::createStream($this->inputs));
67-
}
65+
// Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
66+
$this->input->setStream(self::createStream($this->inputs));
6867

6968
if (isset($options['interactive'])) {
7069
$this->input->setInteractive($options['interactive']);

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Question\ChoiceQuestion;
2020
use Symfony\Component\Console\Question\ConfirmationQuestion;
2121
use Symfony\Component\Console\Question\Question;
22+
use Symfony\Component\Validator\Constraints\Choice;
2223

2324
/**
2425
* @group tty
@@ -908,6 +909,16 @@ public function testAskThrowsExceptionOnMissingInput()
908909
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
909910
}
910911

912+
/**
913+
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
914+
* @expectedExceptionMessage Aborted
915+
*/
916+
public function testAskThrowsExceptionOnMissingInputForChoiceQuestion()
917+
{
918+
$dialog = new QuestionHelper();
919+
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new ChoiceQuestion('Choice', ['a', 'b']));
920+
}
921+
911922
/**
912923
* @expectedException \Symfony\Component\Console\Exception\RuntimeException
913924
* @expectedExceptionMessage Aborted

src/Symfony/Component/Console/Tests/Tester/CommandTesterTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Helper\HelperSet;
1818
use Symfony\Component\Console\Helper\QuestionHelper;
1919
use Symfony\Component\Console\Output\Output;
20+
use Symfony\Component\Console\Question\ChoiceQuestion;
2021
use Symfony\Component\Console\Question\Question;
2122
use Symfony\Component\Console\Style\SymfonyStyle;
2223
use Symfony\Component\Console\Tester\CommandTester;
@@ -139,7 +140,7 @@ public function testCommandWithDefaultInputs()
139140

140141
/**
141142
* @expectedException \RuntimeException
142-
* @expectedMessage Aborted
143+
* @expectedExceptionMessage Aborted
143144
*/
144145
public function testCommandWithWrongInputsNumber()
145146
{
@@ -153,13 +154,40 @@ public function testCommandWithWrongInputsNumber()
153154
$command->setHelperSet(new HelperSet([new QuestionHelper()]));
154155
$command->setCode(function ($input, $output) use ($questions, $command) {
155156
$helper = $command->getHelper('question');
157+
$helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
158+
$helper->ask($input, $output, new Question($questions[0]));
159+
$helper->ask($input, $output, new Question($questions[1]));
160+
$helper->ask($input, $output, new Question($questions[2]));
161+
});
162+
163+
$tester = new CommandTester($command);
164+
$tester->setInputs(['a', 'Bobby', 'Fine']);
165+
$tester->execute([]);
166+
}
167+
168+
/**
169+
* @expectedException \RuntimeException
170+
* @expectedExceptionMessage Aborted
171+
*/
172+
public function testCommandWithQuestionsButNoInputs()
173+
{
174+
$questions = [
175+
'What\'s your name?',
176+
'How are you?',
177+
'Where do you come from?',
178+
];
179+
180+
$command = new Command('foo');
181+
$command->setHelperSet(new HelperSet([new QuestionHelper()]));
182+
$command->setCode(function ($input, $output) use ($questions, $command) {
183+
$helper = $command->getHelper('question');
184+
$helper->ask($input, $output, new ChoiceQuestion('choice', ['a', 'b']));
156185
$helper->ask($input, $output, new Question($questions[0]));
157186
$helper->ask($input, $output, new Question($questions[1]));
158187
$helper->ask($input, $output, new Question($questions[2]));
159188
});
160189

161190
$tester = new CommandTester($command);
162-
$tester->setInputs(['Bobby', 'Fine']);
163191
$tester->execute([]);
164192
}
165193

0 commit comments

Comments
 (0)
0