Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d981fd3

Browse files
committed
[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
1 parent b59664b commit d981fd3

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class SymfonyStyle extends OutputStyle
4040
private $progressBar;
4141
private $lineLength;
4242
private $bufferedOutput;
43+
private $inputStream;
4344

4445
/**
4546
* @param InputInterface $input
@@ -350,6 +351,10 @@ public function askQuestion(Question $question)
350351

351352
if (!$this->questionHelper) {
352353
$this->questionHelper = new SymfonyQuestionHelper();
354+
355+
if ($this->inputStream) {
356+
$this->questionHelper->setInputStream($this->inputStream);
357+
}
353358
}
354359

355360
$answer = $this->questionHelper->ask($this->input, $this, $question);
@@ -389,6 +394,22 @@ public function newLine($count = 1)
389394
$this->bufferedOutput->write(str_repeat("\n", $count));
390395
}
391396

397+
/**
398+
* Sets the input stream to read from when interacting with the user.
399+
*
400+
* This is mainly useful for testing purpose.
401+
*
402+
* @param resource $stream The input stream
403+
*/
404+
public function setInputStream($stream)
405+
{
406+
$this->inputStream = $stream;
407+
408+
if ($this->questionHelper) {
409+
$this->questionHelper->setInputStream($stream);
410+
}
411+
}
412+
392413
/**
393414
* @return ProgressBar
394415
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Input\InputInterface;
4+
use Symfony\Component\Console\Output\OutputInterface;
5+
use Symfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
6+
7+
//Ensure that questions have the expected outputs
8+
return function (InputInterface $input, OutputInterface $output) {
9+
$output = new SymfonyStyleWithForcedLineLength($input, $output);
10+
$questions = array(
11+
'What\'s your name?',
12+
'How are you?',
13+
'Where do you come from?',
14+
);
15+
$inputs = array('Foo', 'Bar', 'Baz');
16+
$stream = fopen('php://memory', 'r+', false);
17+
18+
fputs($stream, implode(PHP_EOL, $inputs));
19+
rewind($stream);
20+
21+
$output->setInputStream($stream);
22+
$output->ask($questions[0]);
23+
$output->ask($questions[1]);
24+
$output->ask($questions[2]);
25+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
What's your name?:
3+
>
4+
How are you?:
5+
>
6+
Where do you come from?:
7+
>

src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ public function inputCommandToOutputFilesProvider()
5555
return array_map(null, glob($baseDir.'/command/command_*.php'), glob($baseDir.'/output/output_*.txt'));
5656
}
5757

58+
/**
59+
* @dataProvider inputInteractiveCommandToOutputFilesProvider
60+
*/
61+
public function testInteractiveOutputs($inputCommandFilepath, $outputFilepath)
62+
{
63+
$code = require $inputCommandFilepath;
64+
$this->command->setCode($code);
65+
$this->tester->execute(array(), array('interactive' => true, 'decorated' => false));
66+
$this->assertStringEqualsFile($outputFilepath, $this->tester->getDisplay(true));
67+
}
68+
69+
public function inputInteractiveCommandToOutputFilesProvider()
70+
{
71+
$baseDir = __DIR__.'/../Fixtures/Style/SymfonyStyle';
72+
73+
return array_map(null, glob($baseDir.'/command/interactive_command_*.php'), glob($baseDir.'/output/interactive_output_*.txt'));
74+
}
75+
5876
public function testLongWordsBlockWrapping()
5977
{
6078
$word = 'Lopadotemachoselachogaleokranioleipsanodrimhypotrimmatosilphioparaomelitokatakechymenokichlepikossyphophattoperisteralektryonoptekephalliokigklopeleiolagoiosiraiobaphetraganopterygovgollhjvhvljfezefeqifzeiqgiqzhrsdgihqzridghqridghqirshdghdghieridgheirhsdgehrsdvhqrsidhqshdgihrsidvqhneriqsdvjzergetsrfhgrstsfhsetsfhesrhdgtesfhbzrtfbrztvetbsdfbrsdfbrn';
@@ -70,6 +88,28 @@ public function testLongWordsBlockWrapping()
7088
$expectedCount = (int) ceil($wordLength / ($maxLineLength)) + (int) ($wordLength > $maxLineLength - 5);
7189
$this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' § '));
7290
}
91+
92+
public function testSetInputStream()
93+
{
94+
$command = $this->command;
95+
$inputs = array('Foo', 'Bar', 'Baz');
96+
$stream = fopen('php://memory', 'r+', false);
97+
98+
fputs($stream, implode(PHP_EOL, $inputs));
99+
rewind($stream);
100+
101+
$command->setCode(function ($input, $output) use ($command, $stream) {
102+
$sfStyle = new SymfonyStyle($input, $output);
103+
104+
$sfStyle->setInputStream($stream);
105+
$sfStyle->ask('What\'s your name?');
106+
$sfStyle->ask('How are you?');
107+
$sfStyle->ask('Where do you come from?');
108+
});
109+
110+
$this->tester->execute(array());
111+
$this->assertSame(0, $this->tester->getStatusCode());
112+
}
73113
}
74114

75115
/**

0 commit comments

Comments
 (0)