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