8000 [Console] added unit tests for DialogHelper · brki/symfony@5744b52 · GitHub
[go: up one dir, main page]

Sk 8000 ip to content

Commit 5744b52

Browse files
committed
[Console] added unit tests for DialogHelper
1 parent 08017fd commit 5744b52

File tree

3 files changed

+109
-8
lines changed

3 files changed

+109
-8
lines changed

src/Symfony/Component/Console/Helper/DialogHelper.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
class DialogHelper extends Helper
2222
{
23+
private $inputStream;
24+
2325
/**
2426
* Asks a question to the user.
2527
*
@@ -31,13 +33,11 @@ class DialogHelper extends Helper
3133
*/
3234
public function ask(OutputInterface $output, $question, $default = null)
3335
{
34-
// @codeCoverageIgnoreStart
3536
$output->write($question);
3637

37-
$ret = trim(fgets(STDIN));
38+
$ret = trim(fgets(null === $this->inputStream ? STDIN : $this->inputStream));
3839

3940
return $ret ? $ret : $default;
40-
// @codeCoverageIgnoreEnd
4141
}
4242

4343
/**
@@ -53,7 +53,6 @@ public function ask(OutputInterface $output, $question, $default = null)
5353
*/
5454
public function askConfirmation(OutputInterface $output, $question, $default = true)
5555
{
56-
// @codeCoverageIgnoreStart
5756
$answer = 'z';
5857
while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) {
5958
$answer = $this->ask($output, $question);
@@ -64,7 +63,6 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
6463
}
6564

6665
return !$answer || 'y' == strtolower($answer[0]);
67-
// @codeCoverageIgnoreEnd
6866
}
6967

7068
/**
@@ -86,7 +84,6 @@ public function askConfirmation(OutputInterface $output, $question, $default = t
8684
*/
8785
public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null)
8886
{
89-
// @codeCoverageIgnoreStart
9087
$error = null;
9188
while (false === $attempts || $attempts--) {
9289
if (null !== $error) {
@@ -102,7 +99,18 @@ public function askAndValidate(OutputInterface $output, $question, $validator, $
10299
}
103100

104101
throw $error;
105-
// @codeCoverageIgnoreEnd
102+
}
103+
104+
/**
105+
* Sets the input stream to read from when interacting with the user.
106+
*
107+
* This is mainly useful for testing purpose.
108+
*
109+
* @param resource $stream The input stream
110+
*/
111+
public function setInputStream($stream)
112+
{
113+
$this->inputStream = $stream;
106114
}
107115

108116
/**
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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\Tests\Component\Console\Helper;
13+
14+
use Symfony\Component\Console\Helper\DialogHelper;
15+
use Symfony\Component\Console\Helper\HelperSet;
16+
use Symfony\Component\Console\Helper\FormatterHelper;
17+
use Symfony\Component\Console\Output\StreamOutput;
18+
19+
class DialogHelperTest extends \PHPUnit_Framework_TestCase
20+
{
21+
public function testAsk()
22+
{
23+
$dialog = new DialogHelper();
24+
25+
$dialog->setInputStream($this->getInputStream("\n8AM\n"));
26+
27+
$this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
28+
$this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
29+
30+
rewind($output->getStream());
31+
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
32+
}
33+
34+
public function testAskConfirmation()
35+
{
36+
$dialog = new DialogHelper();
37+
38+
$dialog->setInputStream($this->getInputStream("\n\n"));
39+
$this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
40+
$this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
41+
42+
$dialog->setInputStream($this->getInputStream("y\nyes\n"));
43+
$this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
44+
$this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
45+
46+
$dialog->setInputStream($this->getInputStream("n\nno\n"));
47+
$this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
48+
$this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
49+
}
50+
51+
public function testAskAndValidate()
52+
{
53+
$dialog = new DialogHelper();
54+
$helperSet = new HelperSet(array(new FormatterHelper()));
55+
$dialog->setHelperSet($helperSet);
56+
57+
$question ='What color was the white horse of Henry IV?';
58+
$error = 'This is not a color!';
59+
$validator = function ($color) use ($error) {
60+
if (!in_array($color, array('white', 'black'))) {
61+
throw new \InvalidArgumentException($error);
62+
}
63+
64+
return $color;
65+
};
66+
67+
$dialog->setInputStream($this->getInputStream("\nblack\n"));
68+
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
69+
$this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
70+
71+
$dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
72+
try {
73+
$this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
74+
$this->fail();
75+
} catch (\InvalidArgumentException $e) {
76+
$this->assertEquals($error, $e->getMessage());
77+
}
78+
}
79+
80+
protected function getInputStream($input)
81+
{
82+
$stream = fopen('php://memory', 'r+', false);
83+
fputs($stream, $input);
84+
rewind($stream);
85+
86+
return $stream;
87+
}
88+
89+
protected function getOutputStream()
90+
{
91+
return new StreamOutput(fopen('php://memory', 'r+', false));
92+
}
93+
}

tests/Symfony/Tests/Component/Console/Helper/FormatterHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Tests\Component\Console\Formatter;
12+
namespace Symfony\Tests\Component\Console\Helper;
1313

1414
use Symfony\Component\Console\Helper\FormatterHelper;
1515

0 commit comments

Comments
 (0)
0