PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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