From d981fd3cd80550be2bbd2d8cd36339ce638257a5 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Fri, 27 May 2016 13:06:51 +0200 Subject: [PATCH] [Console] Add SymfonyStyle::setInputStream for command testing Use Console InvalidArgumentException, test the exception Use better phpdoc block Fabbot fixes Don't check stream type in SymfonyStyle::setInputStream Test output of an interactive command with questions CS Fixes Add tests for SymfonyStyle::ask() output Add an additional check for SymfonyQuestionHelper::setInputStream --- .../Component/Console/Style/SymfonyStyle.php | 21 ++++++++++ .../command/interactive_command_1.php | 25 ++++++++++++ .../output/interactive_output_1.txt | 7 ++++ .../Console/Tests/Style/SymfonyStyleTest.php | 40 +++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/interactive_command_1.php create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/interactive_output_1.txt diff --git a/src/Symfony/Component/Console/Style/SymfonyStyle.php b/src/Symfony/Component/Console/Style/SymfonyStyle.php index 53a7951e016d7..41d09d6dab3d7 100644 --- a/src/Symfony/Component/Console/Style/SymfonyStyle.php +++ b/src/Symfony/Component/Console/Style/SymfonyStyle.php @@ -40,6 +40,7 @@ class SymfonyStyle extends OutputStyle private $progressBar; private $lineLength; private $bufferedOutput; + private $inputStream; /** * @param InputInterface $input @@ -350,6 +351,10 @@ public function askQuestion(Question $question) if (!$this->questionHelper) { $this->questionHelper = new SymfonyQuestionHelper(); + + if ($this->inputStream) { + $this->questionHelper->setInputStream($this->inputStream); + } } $answer = $this->questionHelper->ask($this->input, $this, $question); @@ -389,6 +394,22 @@ public function newLine($count = 1) $this->bufferedOutput->write(str_repeat("\n", $count)); } + /** + * Sets the input stream to read from when interacting with the user. + * + * This is mainly useful for testing purpose. + * + * @param resource $stream The input stream + */ + public function setInputStream($stream) + { + $this->inputStream = $stream; + + if ($this->questionHelper) { + $this->questionHelper->setInputStream($stream); + } + } + /** * @return ProgressBar */ diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/interactive_command_1.php b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/interactive_command_1.php new file mode 100644 index 0000000000000..da583761fb58b --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/command/interactive_command_1.php @@ -0,0 +1,25 @@ +setInputStream($stream); + $output->ask($questions[0]); + $output->ask($questions[1]); + $output->ask($questions[2]); +}; diff --git a/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/interactive_output_1.txt b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/interactive_output_1.txt new file mode 100644 index 0000000000000..6fc7d7eb4dee5 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/Style/SymfonyStyle/output/interactive_output_1.txt @@ -0,0 +1,7 @@ + + What's your name?: + > + How are you?: + > + Where do you come from?: + > diff --git a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php index 889a9c82f2dcb..23b5cf48c296e 100644 --- a/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php +++ b/src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php @@ -55,6 +55,24 @@ public function inputCommandToOutputFilesProvider() return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt')); } + /** + * @dataProvider inputInteractiveCommandToOutputFilesProvider + */ + public function testInteractiveOutputs($inputCommandFilepath, $outputFilepath) + { + $code = require $inputCommandFilepath; + $this->command->setCode($code); + $this->tester->execute(array(), array('interactive' => true, 'decorated' => false)); + $this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true)); + } + + public function inputInteractiveCommandToOutputFilesProvider() + { + $baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle'; + + return array_map(null, glob($baseDir.'/command/interactive_command_*.php'), glob($baseDir.'/output/interactive_output_*.txt')); + } + public function testLongWordsBlockWrapping() { $word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygovgollhjvhvljfezefeqifzeiqgiqzhrsdgihqzridghqridghqirshdghdghieridgheirhsdgehrsdvhqrsidhqshdgihrsidvqhneriqsdvjzergetsrfhgrstsfhsetsfhesrhdgtesfhbzrtfbrztvetbsdfbrsdfbrn'; @@ -70,6 +88,28 @@ public function testLongWordsBlockWrapping() $expectedCount = (int) ceil($wordLength / ($maxLineLength)) + (int) ($wordLength > $maxLineLength - 5); $this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' ยง ')); } + + public function testSetInputStream() + { + $command = $this->command; + $inputs = array('Foo', 'Bar', 'Baz'); + $stream = fopen('php://memory', 'r+', false); + + fputs($stream, implode(PHP_EOL, $inputs)); + rewind($stream); + + $command->setCode(function ($input, $output) use ($command, $stream) { + $sfStyle = new SymfonyStyle($input, $output); + + $sfStyle->setInputStream($stream); + $sfStyle->ask('What\'s your name?'); + $sfStyle->ask('How are you?'); + $sfStyle->ask('Where do you come from?'); + }); + + $this->tester->execute(array()); + $this->assertSame(0, $this->tester->getStatusCode()); + } } /**