Security update for Core, with self-updated composer
[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  * @author Robin Chalas <robin.chalas@gmail.com>
25  */
26 class CommandTester
27 {
28     private $command;
29     private $input;
30     private $output;
31     private $inputs = array();
32     private $statusCode;
33
34     /**
35      * Constructor.
36      *
37      * @param Command $command A Command instance to test
38      */
39     public function __construct(Command $command)
40     {
41         $this->command = $command;
42     }
43
44     /**
45      * Executes the command.
46      *
47      * Available execution options:
48      *
49      *  * interactive: Sets the input interactive flag
50      *  * decorated:   Sets the output decorated flag
51      *  * verbosity:   Sets the output verbosity flag
52      *
53      * @param array $input   An array of command arguments and options
54      * @param array $options An array of execution options
55      *
56      * @return int The command exit code
57      */
58     public function execute(array $input, array $options = array())
59     {
60         // set the command name automatically if the application requires
61         // this argument and no command name was passed
62         if (!isset($input['command'])
63             && (null !== $application = $this->command->getApplication())
64             && $application->getDefinition()->hasArgument('command')
65         ) {
66             $input = array_merge(array('command' => $this->command->getName()), $input);
67         }
68
69         $this->input = new ArrayInput($input);
70         if ($this->inputs) {
71             $this->input->setStream(self::createStream($this->inputs));
72         }
73
74         if (isset($options['interactive'])) {
75             $this->input->setInteractive($options['interactive']);
76         }
77
78         $this->output = new StreamOutput(fopen('php://memory', 'w', false));
79         $this->output->setDecorated(isset($options['decorated']) ? $options['decorated'] : false);
80         if (isset($options['verbosity'])) {
81             $this->output->setVerbosity($options['verbosity']);
82         }
83
84         return $this->statusCode = $this->command->run($this->input, $this->output);
85     }
86
87     /**
88      * Gets the display returned by the last execution of the command.
89      *
90      * @param bool $normalize Whether to normalize end of lines to \n or not
91      *
92      * @return string The display
93      */
94     public function getDisplay($normalize = false)
95     {
96         rewind($this->output->getStream());
97
98         $display = stream_get_contents($this->output->getStream());
99
100         if ($normalize) {
101             $display = str_replace(PHP_EOL, "\n", $display);
102         }
103
104         return $display;
105     }
106
107     /**
108      * Gets the input instance used by the last execution of the command.
109      *
110      * @return InputInterface The current input instance
111      */
112     public function getInput()
113     {
114         return $this->input;
115     }
116
117     /**
118      * Gets the output instance used by the last execution of the command.
119      *
120      * @return OutputInterface The current output instance
121      */
122     public function getOutput()
123     {
124         return $this->output;
125     }
126
127     /**
128      * Gets the status code returned by the last execution of the application.
129      *
130      * @return int The status code
131      */
132     public function getStatusCode()
133     {
134         return $this->statusCode;
135     }
136
137     /**
138      * Sets the user inputs.
139      *
140      * @param array An array of strings representing each input
141      *              passed to the command input stream.
142      *
143      * @return CommandTester
144      */
145     public function setInputs(array $inputs)
146     {
147         $this->inputs = $inputs;
148
149         return $this;
150     }
151
152     private static function createStream(array $inputs)
153     {
154         $stream = fopen('php://memory', 'r+', false);
155
156         fwrite($stream, implode(PHP_EOL, $inputs));
157         rewind($stream);
158
159         return $stream;
160     }
161 }