Debian Codegraph @

vendor/symfony/console/Tester/CommandTester.php

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