LegacyDialogHelperTest.php 11 KB

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