58eb8103fcc9aabdd0f8a0518ee8647c7accf643
[yaffs-website] / vendor / symfony / console / Tests / Tester / CommandTesterTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Tests\Tester;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Console\Application;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Helper\HelperSet;
18 use Symfony\Component\Console\Helper\QuestionHelper;
19 use Symfony\Component\Console\Output\Output;
20 use Symfony\Component\Console\Question\Question;
21 use Symfony\Component\Console\Style\SymfonyStyle;
22 use Symfony\Component\Console\Tester\CommandTester;
23
24 class CommandTesterTest extends TestCase
25 {
26     protected $command;
27     protected $tester;
28
29     protected function setUp()
30     {
31         $this->command = new Command('foo');
32         $this->command->addArgument('command');
33         $this->command->addArgument('foo');
34         $this->command->setCode(function ($input, $output) { $output->writeln('foo'); });
35
36         $this->tester = new CommandTester($this->command);
37         $this->tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE));
38     }
39
40     protected function tearDown()
41     {
42         $this->command = null;
43         $this->tester = null;
44     }
45
46     public function testExecute()
47     {
48         $this->assertFalse($this->tester->getInput()->isInteractive(), '->execute() takes an interactive option');
49         $this->assertFalse($this->tester->getOutput()->isDecorated(), '->execute() takes a decorated option');
50         $this->assertEquals(Output::VERBOSITY_VERBOSE, $this->tester->getOutput()->getVerbosity(), '->execute() takes a verbosity option');
51     }
52
53     public function testGetInput()
54     {
55         $this->assertEquals('bar', $this->tester->getInput()->getArgument('foo'), '->getInput() returns the current input instance');
56     }
57
58     public function testGetOutput()
59     {
60         rewind($this->tester->getOutput()->getStream());
61         $this->assertEquals('foo'.PHP_EOL, stream_get_contents($this->tester->getOutput()->getStream()), '->getOutput() returns the current output instance');
62     }
63
64     public function testGetDisplay()
65     {
66         $this->assertEquals('foo'.PHP_EOL, $this->tester->getDisplay(), '->getDisplay() returns the display of the last execution');
67     }
68
69     public function testGetStatusCode()
70     {
71         $this->assertSame(0, $this->tester->getStatusCode(), '->getStatusCode() returns the status code');
72     }
73
74     public function testCommandFromApplication()
75     {
76         $application = new Application();
77         $application->setAutoExit(false);
78
79         $command = new Command('foo');
80         $command->setCode(function ($input, $output) { $output->writeln('foo'); });
81
82         $application->add($command);
83
84         $tester = new CommandTester($application->find('foo'));
85
86         // check that there is no need to pass the command name here
87         $this->assertEquals(0, $tester->execute(array()));
88     }
89
90     public function testCommandWithInputs()
91     {
92         $questions = array(
93             'What\'s your name?',
94             'How are you?',
95             'Where do you come from?',
96         );
97
98         $command = new Command('foo');
99         $command->setHelperSet(new HelperSet(array(new QuestionHelper())));
100         $command->setCode(function ($input, $output) use ($questions, $command) {
101             $helper = $command->getHelper('question');
102             $helper->ask($input, $output, new Question($questions[0]));
103             $helper->ask($input, $output, new Question($questions[1]));
104             $helper->ask($input, $output, new Question($questions[2]));
105         });
106
107         $tester = new CommandTester($command);
108         $tester->setInputs(array('Bobby', 'Fine', 'France'));
109         $tester->execute(array());
110
111         $this->assertEquals(0, $tester->getStatusCode());
112         $this->assertEquals(implode('', $questions), $tester->getDisplay(true));
113     }
114
115     /**
116      * @expectedException \RuntimeException
117      * @expectedMessage   Aborted
118      */
119     public function testCommandWithWrongInputsNumber()
120     {
121         $questions = array(
122             'What\'s your name?',
123             'How are you?',
124             'Where do you come from?',
125         );
126
127         $command = new Command('foo');
128         $command->setHelperSet(new HelperSet(array(new QuestionHelper())));
129         $command->setCode(function ($input, $output) use ($questions, $command) {
130             $helper = $command->getHelper('question');
131             $helper->ask($input, $output, new Question($questions[0]));
132             $helper->ask($input, $output, new Question($questions[1]));
133             $helper->ask($input, $output, new Question($questions[2]));
134         });
135
136         $tester = new CommandTester($command);
137         $tester->setInputs(array('Bobby', 'Fine'));
138         $tester->execute(array());
139     }
140
141     public function testSymfonyStyleCommandWithInputs()
142     {
143         $questions = array(
144             'What\'s your name?',
145             'How are you?',
146             'Where do you come from?',
147         );
148
149         $command = new Command('foo');
150         $command->setCode(function ($input, $output) use ($questions, $command) {
151             $io = new SymfonyStyle($input, $output);
152             $io->ask($questions[0]);
153             $io->ask($questions[1]);
154             $io->ask($questions[2]);
155         });
156
157         $tester = new CommandTester($command);
158         $tester->setInputs(array('Bobby', 'Fine', 'France'));
159         $tester->execute(array());
160
161         $this->assertEquals(0, $tester->getStatusCode());
162     }
163 }