Skip to content

Commit 49257c9

Browse files
[Console] Fix ApplicationTester ignoring interactive and verbosity options when SHELL_VERBOSITY is set
1 parent 4d5318a commit 49257c9

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

Tester/ApplicationTester.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(Application $application)
4949
*/
5050
public function run(array $input, array $options = []): int
5151
{
52-
$prevShellVerbosity = getenv('SHELL_VERBOSITY');
52+
$prevShellVerbosity = [getenv('SHELL_VERBOSITY'), $_ENV['SHELL_VERBOSITY'] ?? false, $_SERVER['SHELL_VERBOSITY'] ?? false];
5353

5454
try {
5555
$this->input = new ArrayInput($input);
@@ -63,22 +63,35 @@ public function run(array $input, array $options = []): int
6363

6464
$this->initOutput($options);
6565

66+
// Temporarily clear SHELL_VERBOSITY to prevent Application::configureIO
67+
// from overriding the interactive and verbosity settings set above
68+
if (\function_exists('putenv')) {
69+
@putenv('SHELL_VERBOSITY');
70+
}
71+
unset($_ENV['SHELL_VERBOSITY'], $_SERVER['SHELL_VERBOSITY']);
72+
6673
return $this->statusCode = $this->application->run($this->input, $this->output);
6774
} finally {
6875
// SHELL_VERBOSITY is set by Application::configureIO so we need to unset/reset it
6976
// to its previous value to avoid one test's verbosity to spread to the following tests
70-
if (false === $prevShellVerbosity) {
77+
if (false === $prevShellVerbosity[0]) {
7178
if (\function_exists('putenv')) {
7279
@putenv('SHELL_VERBOSITY');
7380
}
74-
unset($_ENV['SHELL_VERBOSITY']);
75-
unset($_SERVER['SHELL_VERBOSITY']);
7681
} else {
7782
if (\function_exists('putenv')) {
78-
@putenv('SHELL_VERBOSITY='.$prevShellVerbosity);
83+
@putenv('SHELL_VERBOSITY='.$prevShellVerbosity[0]);
7984
}
80-
$_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity;
81-
$_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity;
85+
}
86+
if (false === $prevShellVerbosity[1]) {
87+
unset($_ENV['SHELL_VERBOSITY']);
88+
} else {
89+
$_ENV['SHELL_VERBOSITY'] = $prevShellVerbosity[1];
90+
}
91+
if (false === $prevShellVerbosity[2]) {
92+
unset($_SERVER['SHELL_VERBOSITY']);
93+
} else {
94+
$_SERVER['SHELL_VERBOSITY'] = $prevShellVerbosity[2];
8295
}
8396
}
8497
}

Tests/Tester/ApplicationTesterTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,48 @@ public function testGetStatusCode()
8585
$this->tester->assertCommandIsSuccessful('->getStatusCode() returns the status code');
8686
}
8787

88+
/**
89+
* @dataProvider provideShellVerbositySources
90+
*/
91+
public function testShellVerbosityDoesNotOverrideInteractiveAndVerbosity(callable $setShellVerbosity, callable $cleanUp)
92+
{
93+
$setShellVerbosity();
94+
95+
try {
96+
$application = new Application();
97+
$application->setAutoExit(false);
98+
$application->register('foo')
99+
->setCode(static function ($input, $output) {
100+
$output->writeln('foo');
101+
})
102+
;
103+
104+
$tester = new ApplicationTester($application);
105+
$tester->run(['command' => 'foo'], ['interactive' => true]);
106+
107+
$this->assertTrue($tester->getInput()->isInteractive());
108+
$this->assertSame('foo'.\PHP_EOL, $tester->getDisplay());
109+
} finally {
110+
$cleanUp();
111+
}
112+
}
113+
114+
public static function provideShellVerbositySources(): iterable
115+
{
116+
yield 'putenv' => [
117+
static function () { putenv('SHELL_VERBOSITY=-1'); },
118+
static function () { putenv('SHELL_VERBOSITY'); },
119+
];
120+
yield '$_ENV' => [
121+
static function () { $_ENV['SHELL_VERBOSITY'] = '-1'; },
122+
static function () { unset($_ENV['SHELL_VERBOSITY']); },
123+
];
124+
yield '$_SERVER' => [
125+
static function () { $_SERVER['SHELL_VERBOSITY'] = '-1'; },
126+
static function () { unset($_SERVER['SHELL_VERBOSITY']); },
127+
];
128+
}
129+
88130
public function testErrorOutput()
89131
{
90132
$application = new Application();

0 commit comments

Comments
 (0)