Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dfd22bb

Browse filesBrowse 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
1 parent bfdd905 commit dfd22bb
Copy full SHA for dfd22bb

File tree

Expand file treeCollapse file tree

4 files changed

+89
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+89
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Style/SymfonyStyle.php
+17Lines changed: 17 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
@@ -348,6 +349,10 @@ public function askQuestion(Question $question)
348349

349350
if (!$this->questionHelper) {
350351
$this->questionHelper = new SymfonyQuestionHelper();
352+
353+
if ($this->inputStream) {
354+
$this->questionHelper->setInputStream($this->inputStream);
355+
}
351356
}
352357

353358
$answer = $this->questionHelper->ask($this->input, $this, $question);
@@ -387,6 +392,18 @@ public function newLine($count = 1)
387392
$this->bufferedOutput->write(str_repeat("\n", $count));
388393
}
389394

395+
/**
396+
* Sets the input stream to read from when interacting with the user.
397+
*
398+
* This is mainly useful for testing purpose.
399+
*
400+
* @param resource $stream The input stream
401+
*/
402+
public function setInputStream($stream)
403+
{
404+
$this->inputStream = $stream;
405+
}
406+
390407
/**
391408
* @return ProgressBar
392409
*/
+25Lines 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 = ['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+
};
+7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Style/SymfonyStyleTest.php
+40Lines 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());
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 = ['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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.