TYPO3  7.6
LegacyDialogHelperTest.php
Go to the documentation of this file.
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 
19 
23 class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
24 {
25  public function testSelect()
26  {
27  $dialog = new DialogHelper();
28 
29  $helperSet = new HelperSet(array(new FormatterHelper()));
30  $dialog->setHelperSet($helperSet);
31 
32  $heroes = array('Superman', 'Batman', 'Spiderman');
33 
34  $dialog->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
35  $this->assertEquals('2', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, '2'));
36  $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
37  $this->assertEquals('1', $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes));
38  $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', false));
39 
40  rewind($output->getStream());
41  $this->assertContains('Input "Fabien" is not a superhero!', stream_get_contents($output->getStream()));
42 
43  try {
44  $this->assertEquals('1', $dialog->select($output = $this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, 1));
45  $this->fail();
46  } catch (\InvalidArgumentException $e) {
47  $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
48  }
49 
50  $this->assertEquals(array('1'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
51  $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
52  $this->assertEquals(array('0', '2'), $dialog->select($this->getOutputStream(), 'What is your favorite superhero?', $heroes, null, false, 'Input "%s" is not a superhero!', true));
53  $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));
54  $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));
55  }
56 
57  public function testAsk()
58  {
59  $dialog = new DialogHelper();
60 
61  $dialog->setInputStream($this->getInputStream("\n8AM\n"));
62 
63  $this->assertEquals('2PM', $dialog->ask($this->getOutputStream(), 'What time is it?', '2PM'));
64  $this->assertEquals('8AM', $dialog->ask($output = $this->getOutputStream(), 'What time is it?', '2PM'));
65 
66  rewind($output->getStream());
67  $this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
68  }
69 
70  public function testAskWithAutocomplete()
71  {
72  if (!$this->hasSttyAvailable()) {
73  $this->markTestSkipped('`stty` is required to test autocomplete functionality');
74  }
75 
76  // Acm<NEWLINE>
77  // Ac<BACKSPACE><BACKSPACE>s<TAB>Test<NEWLINE>
78  // <NEWLINE>
79  // <UP ARROW><UP ARROW><NEWLINE>
80  // <UP ARROW><UP ARROW><UP ARROW><UP ARROW><UP ARROW><TAB>Test<NEWLINE>
81  // <DOWN ARROW><NEWLINE>
82  // S<BACKSPACE><BACKSPACE><DOWN ARROW><DOWN ARROW><NEWLINE>
83  // F00<BACKSPACE><BACKSPACE>oo<TAB><NEWLINE>
84  $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");
85 
86  $dialog = new DialogHelper();
87  $dialog->setInputStream($inputStream);
88 
89  $bundles = array('AcmeDemoBundle', 'AsseticBundle', 'SecurityBundle', 'FooBundle');
90 
91  $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
92  $this->assertEquals('AsseticBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
93  $this->assertEquals('FrameworkBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
94  $this->assertEquals('SecurityBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
95  $this->assertEquals('FooBundleTest', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
96  $this->assertEquals('AcmeDemoBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
97  $this->assertEquals('AsseticBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
98  $this->assertEquals('FooBundle', $dialog->ask($this->getOutputStream(), 'Please select a bundle', 'FrameworkBundle', $bundles));
99  }
100 
104  public function testAskHiddenResponse()
105  {
106  if ('\\' === DIRECTORY_SEPARATOR) {
107  $this->markTestSkipped('This test is not supported on Windows');
108  }
109 
110  $dialog = new DialogHelper();
111 
112  $dialog->setInputStream($this->getInputStream("8AM\n"));
113 
114  $this->assertEquals('8AM', $dialog->askHiddenResponse($this->getOutputStream(), 'What time is it?'));
115  }
116 
117  public function testAskConfirmation()
118  {
119  $dialog = new DialogHelper();
120 
121  $dialog->setInputStream($this->getInputStream("\n\n"));
122  $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?'));
123  $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
124 
125  $dialog->setInputStream($this->getInputStream("y\nyes\n"));
126  $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
127  $this->assertTrue($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', false));
128 
129  $dialog->setInputStream($this->getInputStream("n\nno\n"));
130  $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
131  $this->assertFalse($dialog->askConfirmation($this->getOutputStream(), 'Do you like French fries?', true));
132  }
133 
134  public function testAskAndValidate()
135  {
136  $dialog = new DialogHelper();
137  $helperSet = new HelperSet(array(new FormatterHelper()));
138  $dialog->setHelperSet($helperSet);
139 
140  $question = 'What color was the white horse of Henry IV?';
141  $error = 'This is not a color!';
142  $validator = function ($color) use ($error) {
143  if (!in_array($color, array('white', 'black'))) {
144  throw new \InvalidArgumentException($error);
145  }
146 
147  return $color;
148  };
149 
150  $dialog->setInputStream($this->getInputStream("\nblack\n"));
151  $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
152  $this->assertEquals('black', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
153 
154  $dialog->setInputStream($this->getInputStream("green\nyellow\norange\n"));
155  try {
156  $this->assertEquals('white', $dialog->askAndValidate($this->getOutputStream(), $question, $validator, 2, 'white'));
157  $this->fail();
158  } catch (\InvalidArgumentException $e) {
159  $this->assertEquals($error, $e->getMessage());
160  }
161  }
162 
163  public function testNoInteraction()
164  {
165  $dialog = new DialogHelper();
166 
167  $input = new ArrayInput(array());
168  $input->setInteractive(false);
169 
170  $dialog->setInput($input);
171 
172  $this->assertEquals('not yet', $dialog->ask($this->getOutputStream(), 'Do you have a job?', 'not yet'));
173  }
174 
175  protected function getInputStream($input)
176  {
177  $stream = fopen('php://memory', 'r+', false);
178  fwrite($stream, $input);
179  rewind($stream);
180 
181  return $stream;
182  }
183 
184  protected function getOutputStream()
185  {
186  return new StreamOutput(fopen('php://memory', 'r+', false));
187  }
188 
189  private function hasSttyAvailable()
190  {
191  exec('stty 2>&1', $output, $exitcode);
192 
193  return $exitcode === 0;
194  }
195 }