PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/console/Symfony/Component/Console/Tests/Helper/DialogHelperTest.php

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