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