Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitdfd22bb

Browse files
committed
[Console] Add SymfonyStyle::setInputStream for command testing
Use Console InvalidArgumentException, test the exceptionUse better phpdoc blockFabbot fixesDon't check stream type in SymfonyStyle::setInputStreamTest output of an interactive command with questions
1 parentbfdd905 commitdfd22bb

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

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

Lines 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 =newSymfonyQuestionHelper();
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+
publicfunctionsetInputStream($stream)
403+
{
404+
$this->inputStream =$stream;
405+
}
406+
390407
/**
391408
* @return ProgressBar
392409
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
useSymfony\Component\Console\Input\InputInterface;
4+
useSymfony\Component\Console\Output\OutputInterface;
5+
useSymfony\Component\Console\Tests\Style\SymfonyStyleWithForcedLineLength;
6+
7+
//Ensure that questions have the expected outputs
8+
returnfunction (InputInterface$input,OutputInterface$output) {
9+
$output =newSymfonyStyleWithForcedLineLength($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+
};
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
returnarray_map(null,glob($baseDir.'/command/command_*.php'),glob($baseDir.'/output/output_*.txt'));
5656
}
5757

58+
/**
59+
* @dataProvider inputInteractiveCommandToOutputFilesProvider
60+
*/
61+
publicfunctiontestInteractiveOutputs($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+
publicfunctioninputInteractiveCommandToOutputFilesProvider()
70+
{
71+
$baseDir =__DIR__.'/../Fixtures/Style/SymfonyStyle';
72+
73+
returnarray_map(null,glob($baseDir.'/command/interactive_command_*.php'),glob($baseDir.'/output/interactive_output_*.txt'));
74+
}
75+
5876
publicfunctiontestLongWordsBlockWrapping()
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+
publicfunctiontestSetInputStream()
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 =newSymfonyStyle($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)

[8]ページ先頭

©2009-2025 Movatter.jp