DialogHelperTest.php 8.3 KB

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