diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index c69b0dc61dfc4..e469ba12a198b 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Helper\Helper; use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Helper\SymfonyQuestionHelper; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; @@ -337,6 +338,11 @@ public function askQuestion(Question $question) return $answer; } + public function setQuestionHelper(QuestionHelper $questionHelper) + { + $this->questionHelper = $questionHelper; + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php index 6bf64129f4221..6a7dafe30f6e2 100644 --- a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php +++ b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php @@ -13,6 +13,8 @@ use PHPUnit_Framework_TestCase; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\HelperSet; +use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -70,6 +72,42 @@ public function testLongWordsBlockWrapping() $expectedCount = (int) ceil($wordLength / ($maxLineLength)) + (int) ($wordLength > $maxLineLength - 5); $this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' ยง ')); } + + public function testCanSetQuestionHelper() + { + $questionHelper = new QuestionHelper(); + + $this->command->setHelperSet(new HelperSet([ + 'question' => $questionHelper, + ])); + + $this->command->setCode(function (InputInterface $input, OutputInterface $output) { + $io = new SymfonyStyle($input, $output); + + $questionHelper = $this->command->getHelper('question'); + + $io->setQuestionHelper($questionHelper); + + if ($io->ask('Are you really sure?') !== 'yes') { + return 1; + } + + return 0; + }); + + $questionHelper->setInputStream($this->getInputStream("yes\n")); + + $this->assertSame(0, $this->tester->execute([])); + } + + private function getInputStream($input) + { + $stream = fopen('php://memory', 'r+', false); + fputs($stream, $input); + rewind($stream); + + return $stream; + } } /**