4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Helper;
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;
19 class DialogHelperTest extends \PHPUnit_Framework_TestCase
21 public function testAsk()
23 $dialog = new DialogHelper();
25 $dialog->setInputStream($this->getInputStream("\n8AM\n"));
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'));
30 rewind($output->getStream());
31 $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
34 public function testAskConfirmation()
36 $dialog = new DialogHelper();
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));
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));
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));
51 public function testAskAndValidate()
53 $dialog = new DialogHelper();
54 $helperSet = new HelperSet(array(new FormatterHelper()));
55 $dialog->setHelperSet($helperSet);
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);
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'));
71 $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
73 $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
75 } catch (\InvalidArgumentException $e) {
76 $this->assertEquals($error, $e->getMessage());
80 protected function getInputStream($input)
82 $stream = fopen('php://memory', 'r+', false);
83 fputs($stream, $input);
89 protected function getOutputStream()
91 return new StreamOutput(fopen('php://memory', 'r+', false));