summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Tester/ApplicationTester.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/console/Tester/ApplicationTester.php')
-rw-r--r--vendor/symfony/console/Tester/ApplicationTester.php83
1 files changed, 83 insertions, 0 deletions
diff --git a/vendor/symfony/console/Tester/ApplicationTester.php b/vendor/symfony/console/Tester/ApplicationTester.php
new file mode 100644
index 0000000..cebb6f8
--- /dev/null
+++ b/vendor/symfony/console/Tester/ApplicationTester.php
@@ -0,0 +1,83 @@
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
12namespace Symfony\Component\Console\Tester;
13
14use Symfony\Component\Console\Application;
15use 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 */
27class 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 $prevShellVerbosity = getenv('SHELL_VERBOSITY');
51
52 try {
53 $this->input = new ArrayInput($input);
54 if (isset($options['interactive'])) {
55 $this->input->setInteractive($options['interactive']);
56 }
57
58 if ($this->inputs) {
59 $this->input->setStream(self::createStream($this->inputs));
60 }
61
62 $this->initOutput($options);
63
64 return $this->statusCode = $this->application->run($this->input, $this->output);
65 } finally {
66 // SHELL_VERBOSITY is set by Application::configureIO so we need to unset/reset it
67 // to its previous value to avoid one test's verbosity to spread to the following tests
68 if (false === $prevShellVerbosity) {
69 if (\function_exists('putenv')) {
70 @putenv('SHELL_VERBOSITY');
71 }
72 unset($_ENV['SHELL_VERBOSITY']);
73 unset($_SERVER['SHELL_VERBOSITY']);
74 } else {
75 if (\function_exists('putenv')) {
76 @putenv('SHELL_VERBOSITY='.$prevShellVerbosity);
77 }
78 $_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity;
79 $_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity;
80 }
81 }
82 }
83}