LegacyDialogHelperTest.php 8.8 KB

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