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

Skip to content

Commit 3d2ed65

Browse files
committed
[Console] Add SymfonyStyle::setInputStream for command testing
Use Console InvalidArgumentException, test the exception Use better phpdoc block Fabbot fixes
1 parent 4871079 commit 3d2ed65

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Application;
1515
use Symfony\Component\Console\Exception\RuntimeException;
16+
use Symfony\Component\Console\Exception\InvalidArgumentException;
1617
use Symfony\Component\Console\Formatter\OutputFormatter;
1718
use Symfony\Component\Console\Helper\Helper;
1819
use Symfony\Component\Console\Helper\ProgressBar;
@@ -40,6 +41,7 @@ class SymfonyStyle extends OutputStyle
4041
private $progressBar;
4142
private $lineLength;
4243
private $bufferedOutput;
44+
private $inputStream;
4345

4446
/**
4547
* @param InputInterface $input
@@ -348,6 +350,10 @@ public function askQuestion(Question $question)
348350

349351
if (!$this->questionHelper) {
350352
$this->questionHelper = new SymfonyQuestionHelper();
353+
354+
if ($this->inputStream) {
355+
$this->questionHelper->setInputStream($this->inputStream);
356+
}
351357
}
352358

353359
$answer = $this->questionHelper->ask($this->input, $this, $question);
@@ -387,6 +393,24 @@ public function newLine($count = 1)
387393
$this->bufferedOutput->write(str_repeat("\n", $count));
388394
}
389395

396+
/**
397+
* Sets the input stream to read from when interacting with the user.
398+
*
399+
* This is mainly useful for testing purpose.
400+
*
401+
* @param resource $stream The input stream
402+
*
403+
* @throws InvalidArgumentException In case the stream is not a resource
404+
*/
405+
public function setInputStream($stream)
406+
{
407+
if (!is_resource($stream)) {
408+
throw new InvalidArgumentException(sprintf('Input stream must be a valid resource, %s given', gettype($stream)));
409+
}
410+
411+
$this->inputStream = $stream;
412+
}
413+
390414
/**
391415
* @return ProgressBar
392416
*/

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,39 @@ 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+
$sfStyle = new SymfonyStyle($input, $output);
84+
85+
$sfStyle->setInputStream($stream);
86+
$sfStyle->ask('What\'s your name?');
87+
});
88+
89+
$this->tester->execute(array());
90+
$this->assertSame(0, $this->tester->getStatusCode());
91+
}
92+
93+
/**
94+
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
95+
* @expectedMessage Input stream must be a valid resource, string given
96+
*/
97+
public function testSetInputStreamWithWrongResource()
98+
{
99+
$this->command->setCode(function ($input, $output) {
100+
$sfStyle = new SymfonyStyle($input, $output);
101+
$sfStyle->setInputStream('invalid type');
102+
});
103+
104+
$this->tester->execute(array());
105+
}
73106
}
74107

75108
/**

0 commit comments

Comments
 (0)