8000 [Console] Fix: Allow to inject QuestionHelper into SymfonyStyle by localheinz · Pull Request #17468 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Fix: Allow to inject QuestionHelper into SymfonyStyle #17468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
6 changes: 6 additions & 0 deletions src/Symfony/Component/Console/Style/SymfonyStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -337,6 +338,11 @@ public function askQuestion(Question $question)
return $answer;
}

public function setQuestionHelper(QuestionHelper $questionHelper)
{
$this->questionHelper = $questionHelper;
}

/**
* {@inheritdoc}
*/
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

/**
Expand Down
0