Skip to content
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

Be more forgiving with error handlers that do not respect error suppression #5765

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion src/Runner/PhptTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,10 @@
$coverage = RawCodeCoverageData::fromXdebugWithoutPathCoverage([]);
$files = $this->getCoverageFiles();

$buffer = @file_get_contents($files['coverage']);
$buffer = false;
if (is_file($files['coverage'])) {
$buffer = @file_get_contents($files['coverage']);

Check warning on line 643 in src/Runner/PhptTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Runner/PhptTestCase.php#L641-L643

Added lines #L641 - L643 were not covered by tests
}

if ($buffer !== false) {
$coverage = @unserialize($buffer);
Expand Down
5 changes: 4 additions & 1 deletion src/Runner/ResultCache/DefaultResultCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public function time(string $id): float

public function load(): void
{
$contents = @file_get_contents($this->cacheFilename);
if (!is_file($this->cacheFilename)) {
return;
}
$contents = file_get_contents($this->cacheFilename);

if ($contents === false) {
return;
Expand Down
8 changes: 6 additions & 2 deletions src/TextUI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,22 +516,26 @@
private function registerLogfileWriters(Configuration $configuration): void
{
if ($configuration->hasLogEventsText()) {
@unlink($configuration->logEventsText());
if (is_file($configuration->logEventsText())) {
unlink($configuration->logEventsText());
}

EventFacade::instance()->registerTracer(
new EventLogger(
$configuration->logEventsText(),

Check failure on line 525 in src/TextUI/Application.php

View workflow job for this annotation

GitHub Actions / Type Checker

MissingThrowsDocblock

src/TextUI/Application.php:525:37: MissingThrowsDocblock: PHPUnit\TextUI\Configuration\LoggingNotConfiguredException is thrown but not caught - please either catch or add a @throws annotation (see https://psalm.dev/169)
false,
),
);
}

if ($configuration->hasLogEventsVerboseText()) {
@unlink($configuration->logEventsVerboseText());
if (is_file($configuration->logEventsVerboseText())) {
unlink($configuration->logEventsVerboseText());
}

EventFacade::instance()->registerTracer(
new EventLogger(
$configuration->logEventsVerboseText(),

Check failure on line 538 in src/TextUI/Application.php

View workflow job for this annotation

GitHub Actions / Type Checker

MissingThrowsDocblock

src/TextUI/Application.php:538:37: MissingThrowsDocblock: PHPUnit\TextUI\Configuration\LoggingNotConfiguredException is thrown but not caught - please either catch or add a @throws annotation (see https://psalm.dev/169)
true,
),
);
Expand Down
8 changes: 3 additions & 5 deletions src/Util/PHP/AbstractPhpProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,11 @@ public function runTestJob(string $job, Test $test, string $processResultFile):
{
$_result = $this->runJob($job);

$processResult = @file_get_contents($processResultFile);

if ($processResult !== false) {
$processResult = '';

if (file_exists($processResultFile)) {
$processResult = file_get_contents($processResultFile);
@unlink($processResultFile);
} else {
$processResult = '';
}

$this->processChildResult(
Expand Down
21 changes: 21 additions & 0 deletions tests/end-to-end/regression/5764/5764.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/5764
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/';

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

Runtime: %s
Configuration: %s

There was 1 PHPUnit test runner warning:

1) No tests found in class "PHPUnit\TestFixture\Issue5764\Issue5764Test".

No tests executed!
7 changes: 7 additions & 0 deletions tests/end-to-end/regression/5764/error-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

// be forgiving with error handlers which do not suppress `@` prefixed lines
set_error_handler(function(int $err_lvl, string $err_msg, string $err_file, int $err_line): bool {
throw new \ErrorException($err_msg, 0, $err_lvl, $err_file, $err_line);
});

11 changes: 11 additions & 0 deletions tests/end-to-end/regression/5764/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
bootstrap="error-handler.php"
>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
17 changes: 17 additions & 0 deletions tests/end-to-end/regression/5764/tests/Issue5764Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Issue5764;

use Exception;
use PHPUnit\Framework\TestCase;

final class Issue5764Test extends TestCase
{
}
Loading