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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Helper\DialogHelper;
17 use Symfony\Component\Console\Helper\HelperSet;
18 use Symfony\Component\Console\Helper\FormatterHelper;
19 use Symfony\Component\Console\Output\ConsoleOutput;
20 use Symfony\Component\Console\Output\StreamOutput;
21 use Symfony\Component\Console\Exception\InvalidArgumentException;
26 class LegacyDialogHelperTest extends TestCase
28 public function testSelect()
30 $dialog = new DialogHelper();
32 $helperSet = new HelperSet(array(new FormatterHelper()));
33 $dialog->setHelperSet($helperSet);
35 $heroes = array('Superman', 'Batman', 'Spiderman');
37 $dialog->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
38 $this->assertEquals('2', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2'));
39 $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
40 $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
41 $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false));
43 rewind($output->getStream());
44 $this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream()));
47 $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, 1));
49 } catch (\InvalidArgumentException $e) {
50 $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
53 $this->assertEquals(array('1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
54 $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
55 $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
56 $this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '0,1', false, 'Input "%s" is not a superhero!', true));
57 $this->assertEquals(array('0', '1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, ' 0 , 1 ', false, 'Input "%s" is not a superhero!', true));
60 public function testSelectOnErrorOutput()
62 $dialog = new DialogHelper();
64 $helperSet = new HelperSet(array(new FormatterHelper()));
65 $dialog->setHelperSet($helperSet);
67 $heroes = array('Superman', 'Batman', 'Spiderman');
69 $dialog->setInputStream($this->getInputStream("Stdout\n1\n"));
70 $this->assertEquals('1', $dialog->select($output = $this->getConsoleOutput($this->getOutputStream()), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false));
72 rewind($output->getErrorOutput()->getStream());
73 $this->assertContains('Input "Stdout" is not a superhero!', stream_get_contents($output->getErrorOutput()->getStream()));
76 public function testAsk()
78 $dialog = new DialogHelper();
80 $dialog->setInputStream($this->getInputStream("\n8AM\n"));
82 $this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
83 $this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
85 rewind($output->getStream());
86 $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
89 public function testAskOnErrorOutput()
91 if (!$this->hasSttyAvailable()) {
92 $this->markTestSkipped('`stderr` is required to test stderr output functionality');
95 $dialog = new DialogHelper();
97 $dialog->setInputStream($this->getInputStream("not stdout\n"));
99 $this->assertEquals('not stdout', $dialog->ask($output = $this->getConsoleOutput($this->getOutputStream()), 'Where should output go?', 'stderr'));
101 rewind($output->getErrorOutput()->getStream());
102 $this->assertEquals('Where should output go?', stream_get_contents($output->getErrorOutput()->getStream()));
105 public function testAskWithAutocomplete()
107 if (!$this->hasSttyAvailable()) {
108 $this->markTestSkipped('`stty` is required to test autocomplete functionality');
112 // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
114 // <UP ARROW><UP ARROW><NEWLINE>
115 // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
116 // <DOWN ARROW><NEWLINE>
117 // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
118 // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
119 $inputStream = $this->getInputStream("Acm\nAc\177\177s\tTest\n\n\033[A\033[A\n\033[A\033[A\033[A\033[A\033[A\tTest\n\033[B\nS\177\177\033[B\033[B\nF00\177\177oo\t\n");
121 $dialog = new DialogHelper();
122 $dialog->setInputStream($inputStream);
124 $bundles = array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle');
126 $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
127 $this->assertEquals('AsseticBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
128 $this->assertEquals('FrameworkBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
129 $this->assertEquals('SecurityBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
130 $this->assertEquals('FooBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
131 $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
132 $this->assertEquals('AsseticBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
133 $this->assertEquals('FooBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
139 public function testAskHiddenResponse()
141 if ('\\' === DIRECTORY_SEPARATOR) {
142 $this->markTestSkipped('This test is not supported on Windows');
145 $dialog = new DialogHelper();
147 $dialog->setInputStream($this->getInputStream("8AM\n"));
149 $this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
155 public function testAskHiddenResponseOnErrorOutput()
157 if ('\\' === DIRECTORY_SEPARATOR) {
158 $this->markTestSkipped('This test is not supported on Windows');
161 $dialog = new DialogHelper();
163 $dialog->setInputStream($this->getInputStream("8AM\n"));
165 $this->assertEquals('8AM', $dialog->askHiddenResponse($output = $this->getConsoleOutput($this->getOutputStream()), 'What time is it?'));
167 rewind($output->getErrorOutput()->getStream());
168 $this->assertContains('What time is it?', stream_get_contents($output->getErrorOutput()->getStream()));
171 public function testAskConfirmation()
173 $dialog = new DialogHelper();
175 $dialog->setInputStream($this->getInputStream("\n\n"));
176 $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
177 $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
179 $dialog->setInputStream($this->getInputStream("y\nyes\n"));
180 $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
181 $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
183 $dialog->setInputStream($this->getInputStream("n\nno\n"));
184 $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
185 $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
188 public function testAskAndValidate()
190 $dialog = new DialogHelper();
191 $helperSet = new HelperSet(array(new FormatterHelper()));
192 $dialog->setHelperSet($helperSet);
194 $question = 'What color was the white horse of Henry IV?';
195 $error = 'This is not a color!';
196 $validator = function ($color) use ($error) {
197 if (!in_array($color, array('white', 'black'))) {
198 throw new InvalidArgumentException($error);
204 $dialog->setInputStream($this->getInputStream("\nblack\n"));
205 $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
206 $this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
208 $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
210 $this->assertEquals('white', $dialog->askAndValidate($output = $this->getConsoleOutput($this->getOutputStream()), $question, $validator, 2, 'white'));
212 } catch (\InvalidArgumentException $e) {
213 $this->assertEquals($error, $e->getMessage());
214 rewind($output->getErrorOutput()->getStream());
215 $this->assertContains('What color was the white horse of Henry IV?', stream_get_contents($output->getErrorOutput()->getStream()));
219 public function testNoInteraction()
221 $dialog = new DialogHelper();
223 $input = new ArrayInput(array());
224 $input->setInteractive(false);
226 $dialog->setInput($input);
228 $this->assertEquals('not yet', $dialog->ask($this->getOutputStream(), 'Do you have a job?', 'not yet'));
231 protected function getInputStream($input)
233 $stream = fopen('php://memory', 'r+', false);
234 fwrite($stream, $input);
240 protected function getOutputStream()
242 return new StreamOutput(fopen('php://memory', 'r+', false));
245 protected function getConsoleOutput($stderr)
247 $output = new ConsoleOutput();
248 $output->setErrorOutput($stderr);
253 private function hasSttyAvailable()
255 exec('stty 2>&1', $output, $exitcode);
257 return $exitcode === 0;