Skip to content

PHPT: Trigger runner warning on parse-error in CLEAN #6313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions src/Runner/Phpt/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,23 +350,7 @@ private function shouldTestBeSkipped(array $sections, array $settings): bool
$output = $this->runCodeInLocalSandbox($skipIfCode);
}

if (str_contains($output, 'Parse error:')) {
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
sprintf(
'SKIPIF section triggered a parse error: %s',
$output,
),
);
}

if (str_contains($output, 'Fatal error:')) {
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
sprintf(
'SKIPIF section triggered a fatal error: %s',
$output,
),
);
}
$this->triggerRunnerWarningOnPhpErrors('SKIPIF', $output);

if (strncasecmp('skip', ltrim($output), 4) === 0) {
$message = '';
Expand Down Expand Up @@ -448,19 +432,21 @@ private function runClean(array $sections, bool $collectCoverage): void
$cleanCode = (new Renderer)->render($this->filename, $sections['CLEAN']);

if ($this->shouldRunInSubprocess($sections, $cleanCode)) {
$result = JobRunnerRegistry::run(
$jobResult = JobRunnerRegistry::run(
new Job(
$cleanCode,
$this->settings($collectCoverage),
),
);

EventFacade::emitter()->childProcessFinished($result->stdout(), $result->stderr());
$output = $jobResult->stdout();

return;
EventFacade::emitter()->childProcessFinished($jobResult->stdout(), $jobResult->stderr());
} else {
$output = $this->runCodeInLocalSandbox($cleanCode);
}

$this->runCodeInLocalSandbox($cleanCode);
$this->triggerRunnerWarningOnPhpErrors('CLEAN', $output);
}

/**
Expand Down Expand Up @@ -698,4 +684,27 @@ private function settings(bool $collectCoverage): array

return $settings;
}

private function triggerRunnerWarningOnPhpErrors(string $section, string $output): void
{
if (str_contains($output, 'Parse error:')) {
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
sprintf(
'%s section triggered a parse error: %s',
$section,
$output,
),
);
}

if (str_contains($output, 'Fatal error:')) {
EventFacade::emitter()->testRunnerTriggeredPhpunitWarning(
sprintf(
'%s section triggered a fatal error: %s',
$section,
$output,
),
);
}
}
}
11 changes: 11 additions & 0 deletions tests/end-to-end/_files/phpt-clean-parse-error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5991
--CLEAN--
<?php
if (rand(0,1)) // intentional PHP Parse error (missing opening curly brace)
}
--FILE--
<?php declare(strict_types=1);
echo 'hello world';
--EXPECTF--
hello world
28 changes: 28 additions & 0 deletions tests/end-to-end/phpt/phpt-clean-parse-error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5991
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = __DIR__ . '/../_files/phpt-clean-parse-error.phpt';

require_once __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

. 1 / 1 (100%)

Time: %s, Memory: %s

There was 1 PHPUnit test runner warning:

1) CLEAN section triggered a parse error:
Parse error: Unmatched '}' in Standard input code on line 3


OK, but there were issues!
Tests: 1, Assertions: 1, PHPUnit Warnings: 1.
Loading