| 148 |
lars |
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 |
|
|
|
17 |
/**
|
|
|
18 |
* Eases the testing of console commands.
|
|
|
19 |
*
|
|
|
20 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
21 |
* @author Robin Chalas <robin.chalas@gmail.com>
|
|
|
22 |
*/
|
|
|
23 |
class CommandTester
|
|
|
24 |
{
|
|
|
25 |
use TesterTrait;
|
|
|
26 |
|
|
|
27 |
private Command $command;
|
|
|
28 |
|
|
|
29 |
public function __construct(Command $command)
|
|
|
30 |
{
|
|
|
31 |
$this->command = $command;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Executes the command.
|
|
|
36 |
*
|
|
|
37 |
* Available execution options:
|
|
|
38 |
*
|
|
|
39 |
* * interactive: Sets the input interactive flag
|
|
|
40 |
* * decorated: Sets the output decorated flag
|
|
|
41 |
* * verbosity: Sets the output verbosity flag
|
|
|
42 |
* * capture_stderr_separately: Make output of stdOut and stdErr separately available
|
|
|
43 |
*
|
|
|
44 |
* @param array $input An array of command arguments and options
|
|
|
45 |
* @param array $options An array of execution options
|
|
|
46 |
*
|
|
|
47 |
* @return int The command exit code
|
|
|
48 |
*/
|
|
|
49 |
public function execute(array $input, array $options = []): int
|
|
|
50 |
{
|
|
|
51 |
// set the command name automatically if the application requires
|
|
|
52 |
// this argument and no command name was passed
|
|
|
53 |
if (!isset($input['command'])
|
|
|
54 |
&& (null !== $application = $this->command->getApplication())
|
|
|
55 |
&& $application->getDefinition()->hasArgument('command')
|
|
|
56 |
) {
|
|
|
57 |
$input = array_merge(['command' => $this->command->getName()], $input);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$this->input = new ArrayInput($input);
|
|
|
61 |
// Use an in-memory input stream even if no inputs are set so that QuestionHelper::ask() does not rely on the blocking STDIN.
|
|
|
62 |
$this->input->setStream(self::createStream($this->inputs));
|
|
|
63 |
|
|
|
64 |
if (isset($options['interactive'])) {
|
|
|
65 |
$this->input->setInteractive($options['interactive']);
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
if (!isset($options['decorated'])) {
|
|
|
69 |
$options['decorated'] = false;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
$this->initOutput($options);
|
|
|
73 |
|
|
|
74 |
return $this->statusCode = $this->command->run($this->input, $this->output);
|
|
|
75 |
}
|
|
|
76 |
}
|