Skip to content

Commit 9882813

Browse files
committed
added Environment::print(), in CLI outputs to STDOUT to bypass output buffering
1 parent 40a8d36 commit 9882813

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/Framework/Environment.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class Environment
4747

4848
public static bool $checkAssertions = false;
4949
public static bool $useColors;
50-
private static int $obLevel;
5150
private static int $exitCode = 0;
5251

5352

@@ -58,7 +57,6 @@ public static function setup(): void
5857
{
5958
self::setupErrors();
6059
self::setupColors();
61-
self::$obLevel = ob_get_level();
6260

6361
class_exists(Runner\Job::class);
6462
class_exists(Dumper::class);
@@ -131,15 +129,13 @@ public static function setupErrors(): void
131129
register_shutdown_function(function () use ($error): void {
132130
if (in_array($error['type'] ?? null, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], true)) {
133131
if (($error['type'] & error_reporting()) !== $error['type']) { // show fatal errors hidden by @shutup
134-
self::removeOutputBuffers();
135-
echo "\n", Dumper::color('white/red', "Fatal error: $error[message] in $error[file] on line $error[line]"), "\n";
132+
self::print("\n" . Dumper::color('white/red', "Fatal error: $error[message] in $error[file] on line $error[line]"));
136133
}
137134
} elseif (self::$checkAssertions && !Assert::$counter) {
138-
self::removeOutputBuffers();
139-
echo "\n", Dumper::color('white/red', 'Error: This test forgets to execute an assertion.'), "\n";
135+
self::print("\n" . Dumper::color('white/red', 'Error: This test forgets to execute an assertion.'));
140136
self::exit(Runner\Job::CodeFail);
141137
} elseif (!getenv(self::VariableRunner) && self::$exitCode !== Runner\Job::CodeSkip) {
142-
echo "\n", (self::$exitCode ? Dumper::color('white/red', 'FAILURE') : Dumper::color('white/green', 'OK')), "\n";
138+
self::print("\n" . (self::$exitCode ? Dumper::color('white/red', 'FAILURE') : Dumper::color('white/green', 'OK')));
143139
}
144140
});
145141
});
@@ -151,9 +147,8 @@ public static function setupErrors(): void
151147
*/
152148
public static function handleException(\Throwable $e): void
153149
{
154-
self::removeOutputBuffers();
155150
self::$checkAssertions = false;
156-
echo Dumper::dumpException($e);
151+
self::print(Dumper::dumpException($e));
157152
self::exit($e instanceof AssertException ? Runner\Job::CodeFail : Runner\Job::CodeError);
158153
}
159154

@@ -164,7 +159,7 @@ public static function handleException(\Throwable $e): void
164159
public static function skip(string $message = ''): void
165160
{
166161
self::$checkAssertions = false;
167-
echo "\nSkipped:\n$message\n";
162+
self::print("\nSkipped:\n$message");
168163
self::exit(Runner\Job::CodeSkip);
169164
}
170165

@@ -248,15 +243,20 @@ public static function loadData(): array
248243
}
249244

250245

251-
private static function removeOutputBuffers(): void
246+
public static function exit(int $code = 0): void
252247
{
253-
while (ob_get_level() > self::$obLevel && @ob_end_flush()); // @ may be not removable
248+
self::$exitCode = $code;
249+
exit($code);
254250
}
255251

256252

257-
public static function exit(int $code = 0): void
253+
public static function print(string $s): void
258254
{
259-
self::$exitCode = $code;
260-
exit($code);
255+
$s = $s === '' || str_ends_with($s, "\n") ? $s : $s . "\n";
256+
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
257+
fwrite(STDOUT, self::$useColors ? $s : Dumper::removeColors($s));
258+
} else {
259+
echo $s;
260+
}
261261
}
262262
}

0 commit comments

Comments
 (0)