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