Skip to content

Commit 66aaab8

Browse files
Merge branch '6.4' into 7.4
* 6.4: [Console] Fix `ApplicationTester` ignoring `interactive` and `verbosity` options when `SHELL_VERBOSITY` is set
2 parents de4f40b + 49257c9 commit 66aaab8

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

Tester/ApplicationTester.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ public function run(array $input, array $options = []): int
5858

5959
$this->initOutput($options);
6060

61-
return $this->statusCode = $this->application->run($this->input, $this->output);
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+
}
6284
}
6385
}

Tests/Tester/ApplicationTesterTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Tests\Tester;
1313

14+
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Component\Console\Application;
1617
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -91,6 +92,46 @@ public function testGetStatusCode()
9192
$this->tester->assertCommandIsSuccessful('->getStatusCode() returns the status code');
9293
}
9394

95+
#[DataProvider('provideShellVerbositySources')]
96+
public function testShellVerbosityDoesNotOverrideInteractiveAndVerbosity(callable $setShellVerbosity, callable $cleanUp)
97+
{
98+
$setShellVerbosity();
99+
100+
try {
101+
$application = new Application();
102+
$application->setAutoExit(false);
103+
$application->register('foo')
104+
->setCode(static function ($input, $output) {
105+
$output->writeln('foo');
106+
})
107+
;
108+
109+
$tester = new ApplicationTester($application);
110+
$tester->run(['command' => 'foo'], ['interactive' => true]);
111+
112+
$this->assertTrue($tester->getInput()->isInteractive());
113+
$this->assertSame('foo'.\PHP_EOL, $tester->getDisplay());
114+
} finally {
115+
$cleanUp();
116+
}
117+
}
118+
119+
public static function provideShellVerbositySources(): iterable
120+
{
121+
yield 'putenv' => [
122+
static function () { putenv('SHELL_VERBOSITY=-1'); },
123+
static function () { putenv('SHELL_VERBOSITY'); },
124+
];
125+
yield '$_ENV' => [
126+
static function () { $_ENV['SHELL_VERBOSITY'] = '-1'; },
127+
static function () { unset($_ENV['SHELL_VERBOSITY']); },
128+
];
129+
yield '$_SERVER' => [
130+
static function () { $_SERVER['SHELL_VERBOSITY'] = '-1'; },
131+
static function () { unset($_SERVER['SHELL_VERBOSITY']); },
132+
];
133+
}
134+
94135
public function testErrorOutput()
95136
{
96137
$application = new Application();

0 commit comments

Comments
 (0)