Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Helper / LegacyDialogHelperTest.php
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 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;
22
23 /**
24  * @group legacy
25  */
26 class LegacyDialogHelperTest extends TestCase
27 {
28     public function testSelect()
29     {
30         $dialog = new DialogHelper();
31
32         $helperSet = new HelperSet(array(new FormatterHelper()));
33         $dialog->setHelperSet($helperSet);
34
35         $heroes = array('Superman', 'Batman', 'Spiderman');
36
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));
42
43         rewind($output->getStream());
44         $this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream()));
45
46         try {
47             $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, 1));
48             $this->fail();
49         } catch (\InvalidArgumentException $e) {
50             $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
51         }
52
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));
58     }
59
60     public function testSelectOnErrorOutput()
61     {
62         $dialog = new DialogHelper();
63
64         $helperSet = new HelperSet(array(new FormatterHelper()));
65         $dialog->setHelperSet($helperSet);
66
67         $heroes = array('Superman', 'Batman', 'Spiderman');
68
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));
71
72         rewind($output->getErrorOutput()->getStream());
73         $this->assertContains('Input "Stdout" is not a superhero!', stream_get_contents($output->getErrorOutput()->getStream()));
74     }
75
76     public function testAsk()
77     {
78         $dialog = new DialogHelper();
79
80         $dialog->setInputStream($this->getInputStream("\n8AM\n"));
81
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'));
84
85         rewind($output->getStream());
86         $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
87     }
88
89     public function testAskOnErrorOutput()
90     {
91         if (!$this->hasSttyAvailable()) {
92             $this->markTestSkipped('`stderr` is required to test stderr output functionality');
93         }
94
95         $dialog = new DialogHelper();
96
97         $dialog->setInputStream($this->getInputStream("not stdout\n"));
98
99         $this->assertEquals('not stdout', $dialog->ask($output = $this->getConsoleOutput($this->getOutputStream()), 'Where should output go?', 'stderr'));
100
101         rewind($output->getErrorOutput()->getStream());
102         $this->assertEquals('Where should output go?', stream_get_contents($output->getErrorOutput()->getStream()));
103     }
104
105     public function testAskWithAutocomplete()
106     {
107         if (!$this->hasSttyAvailable()) {
108             $this->markTestSkipped('`stty` is required to test autocomplete functionality');
109         }
110
111         // Acm<NEWLINE>
112         // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
113         // <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");
120
121         $dialog = new DialogHelper();
122         $dialog->setInputStream($inputStream);
123
124         $bundles = array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle');
125
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));
134     }
135
136     /**
137      * @group tty
138      */
139     public function testAskHiddenResponse()
140     {
141         if ('\\' === DIRECTORY_SEPARATOR) {
142             $this->markTestSkipped('This test is not supported on Windows');
143         }
144
145         $dialog = new DialogHelper();
146
147         $dialog->setInputStream($this->getInputStream("8AM\n"));
148
149         $this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
150     }
151
152     /**
153      * @group tty
154      */
155     public function testAskHiddenResponseOnErrorOutput()
156     {
157         if ('\\' === DIRECTORY_SEPARATOR) {
158             $this->markTestSkipped('This test is not supported on Windows');
159         }
160
161         $dialog = new DialogHelper();
162
163         $dialog->setInputStream($this->getInputStream("8AM\n"));
164
165         $this->assertEquals('8AM', $dialog->askHiddenResponse($output = $this->getConsoleOutput($this->getOutputStream()), 'What time is it?'));
166
167         rewind($output->getErrorOutput()->getStream());
168         $this->assertContains('What time is it?', stream_get_contents($output->getErrorOutput()->getStream()));
169     }
170
171     public function testAskConfirmation()
172     {
173         $dialog = new DialogHelper();
174
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));
178
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));
182
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));
186     }
187
188     public function testAskAndValidate()
189     {
190         $dialog = new DialogHelper();
191         $helperSet = new HelperSet(array(new FormatterHelper()));
192         $dialog->setHelperSet($helperSet);
193
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);
199             }
200
201             return $color;
202         };
203
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'));
207
208         $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
209         try {
210             $this->assertEquals('white', $dialog->askAndValidate($output = $this->getConsoleOutput($this->getOutputStream()), $question, $validator, 2, 'white'));
211             $this->fail();
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()));
216         }
217     }
218
219     public function testNoInteraction()
220     {
221         $dialog = new DialogHelper();
222
223         $input = new ArrayInput(array());
224         $input->setInteractive(false);
225
226         $dialog->setInput($input);
227
228         $this->assertEquals('not yet', $dialog->ask($this->getOutputStream(), 'Do you have a job?', 'not yet'));
229     }
230
231     protected function getInputStream($input)
232     {
233         $stream = fopen('php://memory', 'r+', false);
234         fwrite($stream, $input);
235         rewind($stream);
236
237         return $stream;
238     }
239
240     protected function getOutputStream()
241     {
242         return new StreamOutput(fopen('php://memory', 'r+', false));
243     }
244
245     protected function getConsoleOutput($stderr)
246     {
247         $output = new ConsoleOutput();
248         $output->setErrorOutput($stderr);
249
250         return $output;
251     }
252
253     private function hasSttyAvailable()
254     {
255         exec('stty 2>&1', $output, $exitcode);
256
257         return $exitcode === 0;
258     }
259 }