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