|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\Console\Tests\Helper;
|
13 | 13 |
|
| 14 | +use Symfony\Component\Console\Application; |
14 | 15 | use Symfony\Component\Console\Exception\InvalidArgumentException;
|
15 | 16 | use Symfony\Component\Console\Formatter\OutputFormatter;
|
16 | 17 | use Symfony\Component\Console\Helper\FormatterHelper;
|
|
21 | 22 | use Symfony\Component\Console\Question\ConfirmationQuestion;
|
22 | 23 | use Symfony\Component\Console\Question\Question;
|
23 | 24 | use Symfony\Component\Console\Terminal;
|
| 25 | +use Symfony\Component\Console\Tester\ApplicationTester; |
24 | 26 |
|
25 | 27 | /**
|
26 | 28 | * @group tty
|
@@ -727,21 +729,36 @@ public function testAskThrowsExceptionOnMissingInputWithValidator()
|
727 | 729 | $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), $question);
|
728 | 730 | }
|
729 | 731 |
|
730 |
| - public function testAskThrowsExceptionFromValidatorEarlyWhenTtyIsMissing() |
| 732 | + public function testQuestionValidatorRepeatsThePrompt() |
731 | 733 | {
|
732 |
| - $this->expectException('Exception'); |
733 |
| - $this->expectExceptionMessage('Bar, not Foo'); |
| 734 | + $tries = 0; |
| 735 | + $application = new Application(); |
| 736 | + $application->setAutoExit(false); |
| 737 | + $application->register('question') |
| 738 | + ->setCode(function ($input, $output) use (&$tries) { |
| 739 | + $question = new Question('This is a promptable question'); |
| 740 | + $question->setValidator(function ($value) use (&$tries) { |
| 741 | + ++$tries; |
| 742 | + if (!$value) { |
| 743 | + throw new \Exception(); |
| 744 | + } |
734 | 745 |
|
735 |
| - $output = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')->getMock(); |
736 |
| - $output->expects($this->once())->method('writeln'); |
| 746 | + return $value; |
| 747 | + }); |
| 748 | + |
| 749 | + (new QuestionHelper())->ask($input, $output, $question); |
737 | 750 |
|
738 |
| - (new QuestionHelper())->ask( |
739 |
| - $this->createStreamableInputInterfaceMock($this->getInputStream('Foo'), true), |
740 |
| - $output, |
741 |
| - (new Question('Q?'))->setHidden(true)->setValidator(function ($input) { |
742 |
| - throw new \Exception("Bar, not $input"); |
| 751 | + return 0; |
743 | 752 | })
|
744 |
| - ); |
| 753 | + ; |
| 754 | + |
| 755 | + $tester = new ApplicationTester($application); |
| 756 | + $tester->setInputs(['', 'not-empty']); |
| 757 | + |
| 758 | + $statusCode = $tester->run(['command' => 'question'], ['interactive' => true]); |
| 759 | + |
| 760 | + $this->assertSame(2, $tries); |
| 761 | + $this->assertSame($statusCode, 0); |
745 | 762 | }
|
746 | 763 |
|
747 | 764 | public function testEmptyChoices()
|
|
0 commit comments