Debian Codegraph @

vendor/symfony/console/Tester/ApplicationTester.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\Application;
15 use Symfony\Component\Console\Input\ArrayInput;
16
17 /**
18 * Eases the testing of console applications.
19 *
20 * When testing an application, don't forget to disable the auto exit flag:
21 *
22 * $application = new Application();
23 * $application->setAutoExit(false);
24 *
25 * @author Fabien Potencier <[email protected]>
26 */
27 class ApplicationTester
28 {
29 use TesterTrait;
30
31 public function __construct(
32 private Application $application,
33 ) {
34 }
35
36 /**
37 * Executes the application.
38 *
39 * Available 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 * @return int The command exit code
47 */
48 public function run(array $input, array $options = []): int
49 {
50 $this->input = new ArrayInput($input);
51 if (isset($options['interactive'])) {
52 $this->input->setInteractive($options['interactive']);
53 }
54
55 if ($this->inputs) {
56 $this->input->setStream(self::createStream($this->inputs));
57 }
58
59 $this->initOutput($options);
60
61 // Temporarily clear SHELL_VERBOSITY to prevent Application::configureIO
62 // from overriding the interactive and verbosity settings set above
63 $prevShellVerbosity = [getenv('SHELL_VERBOSITY'), $_ENV['SHELL_VERBOSITY'] ?? false, $_SERVER['SHELL_VERBOSITY'] ?? false];
64 if (\function_exists('putenv')) {
65 @putenv('SHELL_VERBOSITY');
66 }
67 unset($_ENV['SHELL_VERBOSITY'], $_SERVER['SHELL_VERBOSITY']);
68
69 try {
70 return $this->statusCode = $this->application->run($this->input, $this->output);
71 } finally {
72 if (false !== $prevShellVerbosity[0]) {
73 if (\function_exists('putenv')) {
74 @putenv('SHELL_VERBOSITY='.$prevShellVerbosity[0]);
75 }
76 }
77 if (false !== $prevShellVerbosity[1]) {
78 $_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity[1];
79 }
80 if (false !== $prevShellVerbosity[2]) {
81 $_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity[2];
82 }
83 }
84 }
85 }