SymfonyQuestionHelperTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Symfony\Component\Console\Tests\Helper;
  3. use Symfony\Component\Console\Helper\FormatterHelper;
  4. use Symfony\Component\Console\Helper\HelperSet;
  5. use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
  6. use Symfony\Component\Console\Output\StreamOutput;
  7. use Symfony\Component\Console\Question\ChoiceQuestion;
  8. use Symfony\Component\Console\Question\Question;
  9. /**
  10. * @group tty
  11. */
  12. class SymfonyQuestionHelperTest extends AbstractQuestionHelperTest
  13. {
  14. public function testAskChoice()
  15. {
  16. $questionHelper = new SymfonyQuestionHelper();
  17. $helperSet = new HelperSet([new FormatterHelper()]);
  18. $questionHelper->setHelperSet($helperSet);
  19. $heroes = ['Superman', 'Batman', 'Spiderman'];
  20. $inputStream = $this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n");
  21. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  22. $question->setMaxAttempts(1);
  23. // first answer is an empty answer, we're supposed to receive the default value
  24. $this->assertEquals('Spiderman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  25. $this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
  26. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  27. $question->setMaxAttempts(1);
  28. $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  29. $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  30. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  31. $question->setErrorMessage('Input "%s" is not a superhero!');
  32. $question->setMaxAttempts(2);
  33. $this->assertEquals('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  34. $this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
  35. try {
  36. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  37. $question->setMaxAttempts(1);
  38. $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question);
  39. $this->fail();
  40. } catch (\InvalidArgumentException $e) {
  41. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  42. }
  43. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  44. $question->setMaxAttempts(1);
  45. $question->setMultiselect(true);
  46. $this->assertEquals(['Batman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  47. $this->assertEquals(['Superman', 'Spiderman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  48. $this->assertEquals(['Superman', 'Spiderman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question));
  49. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  50. $question->setMaxAttempts(1);
  51. $question->setMultiselect(true);
  52. $this->assertEquals(['Superman', 'Batman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  53. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  54. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  55. $question->setMaxAttempts(1);
  56. $question->setMultiselect(true);
  57. $this->assertEquals(['Superman', 'Batman'], $questionHelper->ask($this->createStreamableInputInterfaceMock($inputStream), $output = $this->createOutputInterface(), $question));
  58. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  59. }
  60. public function testAskChoiceWithChoiceValueAsDefault()
  61. {
  62. $questionHelper = new SymfonyQuestionHelper();
  63. $helperSet = new HelperSet([new FormatterHelper()]);
  64. $questionHelper->setHelperSet($helperSet);
  65. $question = new ChoiceQuestion('What is your favorite superhero?', ['Superman', 'Batman', 'Spiderman'], 'Batman');
  66. $question->setMaxAttempts(1);
  67. $this->assertSame('Batman', $questionHelper->ask($this->createStreamableInputInterfaceMock($this->getInputStream("Batman\n")), $output = $this->createOutputInterface(), $question));
  68. $this->assertOutputContains('What is your favorite superhero? [Batman]', $output);
  69. }
  70. public function testAskReturnsNullIfValidatorAllowsIt()
  71. {
  72. $questionHelper = new SymfonyQuestionHelper();
  73. $question = new Question('What is your favorite superhero?');
  74. $question->setValidator(function ($value) { return $value; });
  75. $input = $this->createStreamableInputInterfaceMock($this->getInputStream("\n"));
  76. $this->assertNull($questionHelper->ask($input, $this->createOutputInterface(), $question));
  77. }
  78. public function testAskEscapeDefaultValue()
  79. {
  80. $helper = new SymfonyQuestionHelper();
  81. $input = $this->createStreamableInputInterfaceMock($this->getInputStream('\\'));
  82. $helper->ask($input, $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\'));
  83. $this->assertOutputContains('Can I have a backslash? [\]', $output);
  84. }
  85. public function testAskEscapeAndFormatLabel()
  86. {
  87. $helper = new SymfonyQuestionHelper();
  88. $input = $this->createStreamableInputInterfaceMock($this->getInputStream('Foo\\Bar'));
  89. $helper->ask($input, $output = $this->createOutputInterface(), new Question('Do you want to use Foo\\Bar <comment>or</comment> Foo\\Baz\\?', 'Foo\\Baz'));
  90. $this->assertOutputContains('Do you want to use Foo\\Bar or Foo\\Baz\\? [Foo\\Baz]:', $output);
  91. }
  92. public function testLabelTrailingBackslash()
  93. {
  94. $helper = new SymfonyQuestionHelper();
  95. $input = $this->createStreamableInputInterfaceMock($this->getInputStream('sure'));
  96. $helper->ask($input, $output = $this->createOutputInterface(), new Question('Question with a trailing \\'));
  97. $this->assertOutputContains('Question with a trailing \\', $output);
  98. }
  99. public function testAskThrowsExceptionOnMissingInput()
  100. {
  101. $this->expectException('Symfony\Component\Console\Exception\RuntimeException');
  102. $this->expectExceptionMessage('Aborted.');
  103. $dialog = new SymfonyQuestionHelper();
  104. $dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
  105. }
  106. protected function getInputStream($input)
  107. {
  108. $stream = fopen('php://memory', 'r+', false);
  109. fwrite($stream, $input);
  110. rewind($stream);
  111. return $stream;
  112. }
  113. protected function createOutputInterface()
  114. {
  115. $output = new StreamOutput(fopen('php://memory', 'r+', false));
  116. $output->setDecorated(false);
  117. return $output;
  118. }
  119. protected function createInputInterfaceMock($interactive = true)
  120. {
  121. $mock = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock();
  122. $mock->expects($this->any())
  123. ->method('isInteractive')
  124. ->willReturn($interactive);
  125. return $mock;
  126. }
  127. private function assertOutputContains($expected, StreamOutput $output)
  128. {
  129. rewind($output->getStream());
  130. $stream = stream_get_contents($output->getStream());
  131. $this->assertStringContainsString($expected, $stream);
  132. }
  133. }