| 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\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 <fabien@symfony.com>
|
|
|
26 |
*/
|
|
|
27 |
class ApplicationTester
|
|
|
28 |
{
|
|
|
29 |
use TesterTrait;
|
|
|
30 |
|
|
|
31 |
private Application $application;
|
|
|
32 |
|
|
|
33 |
public function __construct(Application $application)
|
|
|
34 |
{
|
|
|
35 |
$this->application = $application;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Executes the application.
|
|
|
40 |
*
|
|
|
41 |
* Available options:
|
|
|
42 |
*
|
|
|
43 |
* * interactive: Sets the input interactive flag
|
|
|
44 |
* * decorated: Sets the output decorated flag
|
|
|
45 |
* * verbosity: Sets the output verbosity flag
|
|
|
46 |
* * capture_stderr_separately: Make output of stdOut and stdErr separately available
|
|
|
47 |
*
|
|
|
48 |
* @return int The command exit code
|
|
|
49 |
*/
|
|
|
50 |
public function run(array $input, array $options = []): int
|
|
|
51 |
{
|
|
|
52 |
$prevShellVerbosity = getenv('SHELL_VERBOSITY');
|
|
|
53 |
|
|
|
54 |
try {
|
|
|
55 |
$this->input = new ArrayInput($input);
|
|
|
56 |
if (isset($options['interactive'])) {
|
|
|
57 |
$this->input->setInteractive($options['interactive']);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if ($this->inputs) {
|
|
|
61 |
$this->input->setStream(self::createStream($this->inputs));
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$this->initOutput($options);
|
|
|
65 |
|
|
|
66 |
return $this->statusCode = $this->application->run($this->input, $this->output);
|
|
|
67 |
} finally {
|
|
|
68 |
// SHELL_VERBOSITY is set by Application::configureIO so we need to unset/reset it
|
|
|
69 |
// to its previous value to avoid one test's verbosity to spread to the following tests
|
|
|
70 |
if (false === $prevShellVerbosity) {
|
|
|
71 |
if (\function_exists('putenv')) {
|
|
|
72 |
@putenv('SHELL_VERBOSITY');
|
|
|
73 |
}
|
|
|
74 |
unset($_ENV['SHELL_VERBOSITY']);
|
|
|
75 |
unset($_SERVER['SHELL_VERBOSITY']);
|
|
|
76 |
} else {
|
|
|
77 |
if (\function_exists('putenv')) {
|
|
|
78 |
@putenv('SHELL_VERBOSITY='.$prevShellVerbosity);
|
|
|
79 |
}
|
|
|
80 |
$_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity;
|
|
|
81 |
$_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
}
|