Skip to content

Commit 76708ec

Browse files
minor symfony#19234 [Console] fix input stream related tests (xabbuh)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Console] fix input stream related tests | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Commits ------- 1cca740 [Console] fix input stream related tests
2 parents d6b38d1 + 1cca740 commit 76708ec

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Helper;
13+
14+
use Symfony\Component\Console\Input\StreamableInputInterface;
15+
16+
abstract class AbstractQuestionHelperTest extends \PHPUnit_Framework_TestCase
17+
{
18+
protected function createStreamableInputInterfaceMock($stream = null, $interactive = true)
19+
{
20+
$mock = $this->getMock(StreamableInputInterface::class);
21+
$mock->expects($this->any())
22+
->method('isInteractive')
23+
->will($this->returnValue($interactive));
24+
25+
if ($stream) {
26+
$mock->expects($this->any())
27+
->method('getStream')
28+
->willReturn($stream);
29+
}
30+
31+
return $mock;
32+
}
33+
}

src/Symfony/Component/Console/Tests/Helper/QuestionHelperTest.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
use Symfony\Component\Console\Question\ChoiceQuestion;
2020
use Symfony\Component\Console\Question\ConfirmationQuestion;
2121
use Symfony\Component\Console\Question\Question;
22-
use Symfony\Component\Console\Input\StreamableInputInterface;
2322

2423
/**
2524
* @group tty
2625
*/
27-
class QuestionHelperTest extends \PHPUnit_Framework_TestCase
26+
class QuestionHelperTest extends AbstractQuestionHelperTest
2827
{
2928
public function testAskChoice()
3029
{
@@ -769,22 +768,6 @@ protected function createInputInterfaceMock($interactive = true)
769768
return $mock;
770769
}
771770

772-
protected function createStreamableInputInterfaceMock($stream = null, $interactive = true)
773-
{
774-
$mock = $this->getMock(StreamableInputInterface::class);
775-
$mock->expects($this->any())
776-
->method('isInteractive')
777-
->will($this->returnValue($interactive));
778-
779-
if ($stream) {
780-
$mock->expects($this->any())
781-
->method('getStream')
782-
->willReturn($stream);
783-
}
784-
785-
return $mock;
786-
}
787-
788771
private function hasSttyAvailable()
789772
{
790773
exec('stty 2>&1', $output, $exitcode);

src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* @group tty
1313
*/
14-
class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase
14+
class SymfonyQuestionHelperTest extends AbstractQuestionHelperTest
1515
{
1616
public function testAskChoice()
1717
{
@@ -22,29 +22,29 @@ public function testAskChoice()
2222

2323
$heroes = array('Superman', 'Batman', 'Spiderman');
2424

25-
$questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
25+
$inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
2626

2727
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
2828
$question->setMaxAttempts(1);
2929
// first answer is an empty answer, we're supposed to receive the default value
30-
$this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
30+
$this->assertEquals('Spiderman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
3131
$this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
3232

3333
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
3434
$question->setMaxAttempts(1);
35-
$this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
36-
$this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
35+
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
36+
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
3737

3838
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
3939
$question->setErrorMessage('Input "%s" is not a superhero!');
4040
$question->setMaxAttempts(2);
41-
$this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
41+
$this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
4242
$this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
4343

4444
try {
4545
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
4646
$question->setMaxAttempts(1);
47-
$questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
47+
$questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
4848
$this->fail();
4949
} catch (\InvalidArgumentException $e) {
5050
$this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
@@ -54,22 +54,22 @@ public function testAskChoice()
5454
$question->setMaxAttempts(1);
5555
$question->setMultiselect(true);
5656

57-
$this->assertEquals(array('Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
58-
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
59-
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
57+
$this->assertEquals(array('Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
58+
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
59+
$this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
6060

6161
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
6262
$question->setMaxAttempts(1);
6363
$question->setMultiselect(true);
6464

65-
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
65+
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
6666
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
6767

6868
$question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
6969
$question->setMaxAttempts(1);
7070
$question->setMultiselect(true);
7171

72-
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
72+
$this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
7373
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
7474
}
7575

0 commit comments

Comments
 (0)