Version 1
[yaffs-website] / vendor / symfony / console / Tester / CommandTester.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\Tester;
13
14 use Symfony\Component\Console\Command\Command;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Output\StreamOutput;
17 use Symfony\Component\Console\Input\InputInterface;
18 use Symfony\Component\Console\Output\OutputInterface;
19
20 /**
21  * Eases the testing of console commands.
22  *
23  * @author Fabien Potencier <fabien@symfony.com>
24  */
25 class CommandTester
26 {
27     private $command;
28     private $input;
29     private $output;
30     private $statusCode;
31
32     /**
33      * Constructor.
34      *
35      * @param Command $command A Command instance to test
36      */
37     public function __construct(Command $command)
38     {
39         $this->command = $command;
40     }
41
42     /**
43      * Executes the command.
44      *
45      * Available execution options:
46      *
47      *  * interactive: Sets the input interactive flag
48      *  * decorated:   Sets the output decorated flag
49      *  * verbosity:   Sets the output verbosity flag
50      *
51      * @param array $input   An array of command arguments and options
52      * @param array $options An array of execution options
53      *
54      * @return int The command exit code
55      */
56     public function execute(array $input, array $options = array())
57     {
58         // set the command name automatically if the application requires
59         // this argument and no command name was passed
60         if (!isset($input['command'])
61             && (null !== $application = $this->command->getApplication())
62             && $application->getDefinition()->hasArgument('command')
63         ) {
64             $input = array_merge(array('command' => $this->command->getName()), $input);
65         }
66
67         $this->input = new ArrayInput($input);
68         if (isset($options['interactive'])) {
69             $this->input->setInteractive($options['interactive']);
70         }
71
72         $this->output = new StreamOutput(fopen('php://memory', 'w', false));
73         $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
74         if (isset($options['verbosity'])) {
75             $this->output->setVerbosity($options['verbosity']);
76         }
77
78         return $this->statusCode = $this->command->run($this->input, $this->output);
79     }
80
81     /**
82      * Gets the display returned by the last execution of the command.
83      *
84      * @param bool $normalize Whether to normalize end of lines to \n or not
85      *
86      * @return string The display
87      */
88     public function getDisplay($normalize = false)
89     {
90         rewind($this->output->getStream());
91
92         $display = stream_get_contents($this->output->getStream());
93
94         if ($normalize) {
95             $display = str_replace(PHP_EOL, "\n", $display);
96         }
97
98         return $display;
99     }
100
101     /**
102      * Gets the input instance used by the last execution of the command.
103      *
104      * @return InputInterface The current input instance
105      */
106     public function getInput()
107     {
108         return $this->input;
109     }
110
111     /**
112      * Gets the output instance used by the last execution of the command.
113      *
114      * @return OutputInterface The current output instance
115      */
116     public function getOutput()
117     {
118         return $this->output;
119     }
120
121     /**
122      * Gets the status code returned by the last execution of the application.
123      *
124      * @return int The status code
125      */
126     public function getStatusCode()
127     {
128         return $this->statusCode;
129     }
130 }