From f785039ad1c325f16d0ca2143d2c63b7cba6da13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Thu, 21 Jan 2016 08:52:03 +0100 Subject: [PATCH 1/2] Enhancement: Assert that QuestionHelper can be injected into SymfonyStyle --- .../Console/Tests/Style/SymfonyStyleTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) 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; + } } /** From 3bd4b06324b56be72c5f28789f982b14cb5e7884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Thu, 21 Jan 2016 09:00:05 +0100 Subject: [PATCH 2/2] Fix: Allow to inject QuestionHelper into SymfonyStyle --- src/Symfony/Component/Console/Style/SymfonyStyle.php | 6 ++++++ 1 file changed, 6 insertions(+) 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} */