SymfonyQuestionHelperTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /**
  9. * @group tty
  10. */
  11. class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testAskChoice()
  14. {
  15. $questionHelper = new SymfonyQuestionHelper();
  16. $helperSet = new HelperSet(array(new FormatterHelper()));
  17. $questionHelper->setHelperSet($helperSet);
  18. $heroes = array('Superman', 'Batman', 'Spiderman');
  19. $questionHelper->setInputStream($this->getInputStream("\n1\n 1 \nFabien\n1\nFabien\n1\n0,2\n 0 , 2 \n\n\n"));
  20. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '2');
  21. $question->setMaxAttempts(1);
  22. // first answer is an empty answer, we're supposed to receive the default value
  23. $this->assertEquals('Spiderman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  24. $this->assertOutputContains('What is your favorite superhero? [Spiderman]', $output);
  25. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  26. $question->setMaxAttempts(1);
  27. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  28. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  29. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes);
  30. $question->setErrorMessage('Input "%s" is not a superhero!');
  31. $question->setMaxAttempts(2);
  32. $this->assertEquals('Batman', $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  33. $this->assertOutputContains('Input "Fabien" is not a superhero!', $output);
  34. try {
  35. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '1');
  36. $question->setMaxAttempts(1);
  37. $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question);
  38. $this->fail();
  39. } catch (\InvalidArgumentException $e) {
  40. $this->assertEquals('Value "Fabien" is invalid', $e->getMessage());
  41. }
  42. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, null);
  43. $question->setMaxAttempts(1);
  44. $question->setMultiselect(true);
  45. $this->assertEquals(array('Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  46. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  47. $this->assertEquals(array('Superman', 'Spiderman'), $questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question));
  48. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, '0,1');
  49. $question->setMaxAttempts(1);
  50. $question->setMultiselect(true);
  51. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  52. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  53. $question = new ChoiceQuestion('What is your favorite superhero?', $heroes, ' 0 , 1 ');
  54. $question->setMaxAttempts(1);
  55. $question->setMultiselect(true);
  56. $this->assertEquals(array('Superman', 'Batman'), $questionHelper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), $question));
  57. $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output);
  58. }
  59. protected function getInputStream($input)
  60. {
  61. $stream = fopen('php://memory', 'r+', false);
  62. fwrite($stream, $input);
  63. rewind($stream);
  64. return $stream;
  65. }
  66. protected function createOutputInterface()
  67. {
  68. $output = new StreamOutput(fopen('php://memory', 'r+', false));
  69. $output->setDecorated(false);
  70. return $output;
  71. }
  72. protected function createInputInterfaceMock($interactive = true)
  73. {
  74. $mock = $this->getMock('Symfony\Component\Console\Input\InputInterface');
  75. $mock->expects($this->any())
  76. ->method('isInteractive')
  77. ->will($this->returnValue($interactive));
  78. return $mock;
  79. }
  80. private function assertOutputContains($expected, StreamOutput $output)
  81. {
  82. rewind($output->getStream());
  83. $stream = stream_get_contents($output->getStream());
  84. $this->assertContains($expected, $stream);
  85. }
  86. }