8000 [Console] Add SymfonyStyle::setInputStream for command testing · symfony/symfony@3a6b05d · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a6b05d

Browse files
committed
[Console] Add SymfonyStyle::setInputStream for command testing
1 parent 4871079 commit 3a6b05d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-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 = 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+
* @param resource $stream
397+
*/
398+
public function setInputStream($stream)
399+
{
400+
if (!is_resource($stream)) {
401+
throw new \InvalidArgumentException(sprintf('The stream must be a valid resource, %s given'), gettype($stream));
402+
}
403+
404+
$this->inputStream = $stream;
405+
}
406+
390407
/**
391408
* @return ProgressBar
392409
*/

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ public function testLongWordsBlockWrapping()
7070
$expectedCount = (int) ceil($wordLength / ($maxLineLength)) + (int) ($wordLength > $maxLineLength - 5);
7171
$this->assertSame($expectedCount, substr_count($this->tester->getDisplay(true), ' § '));
7272
}
73+
74+
public function testSetInputStream()
75+
{
76+
$command = $this->command;
77+
78+
$stream = fopen('php://memory', 'r+', false);
79+
fputs($stream, 'FooBar');
80+
rewind($stream);
81+
82+
$command->setCode(function ($input, $output) use ($command, $stream) {
83+
$io = new SymfonyStyle($input, $output);
84+
85+
$io->setInputStream($stream);
86+
$io->ask('What\'s your name?');
87+
});
88+
89+
$this->tester->execute(array());
90+
$this->assertSame(0, $this->tester->getStatusCode());
91+
}
7392
}
7493

7594
/**

0 commit comments

Comments
 (0)
0