Skip to content

Commit

Permalink
Revert "Check and restore error/exception global handlers"
Browse files Browse the repository at this point in the history
This reverts commit 93aa92b.
  • Loading branch information
sebastianbergmann committed Dec 27, 2023
1 parent 2b15cb6 commit 23c16c1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 272 deletions.
4 changes: 0 additions & 4 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,6 @@
<MissingThrowsDocblock>
<code>getMethod</code>
</MissingThrowsDocblock>
<PossiblyNullArgument>
<code><![CDATA[$this->backupGlobalErrorHandlers]]></code>
<code><![CDATA[$this->backupGlobalExceptionHandlers]]></code>
</PossiblyNullArgument>
<PropertyNotSetInConstructor>
<code>$outputBufferingLevel</code>
</PropertyNotSetInConstructor>
Expand Down
151 changes: 9 additions & 142 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use const PHP_URL_PATH;
use function array_keys;
use function array_merge;
use function array_reverse;
use function array_values;
use function assert;
use function basename;
Expand All @@ -29,7 +28,6 @@
use function clearstatcache;
use function count;
use function defined;
use function error_clear_last;
use function explode;
use function getcwd;
use function implode;
Expand All @@ -50,10 +48,6 @@
use function parse_url;
use function pathinfo;
use function preg_replace;
use function restore_error_handler;
use function restore_exception_handler;
use function set_error_handler;
use function set_exception_handler;
use function setlocale;
use function sprintf;
use function str_contains;
Expand Down Expand Up @@ -132,24 +126,14 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
*/
private array $backupStaticPropertiesExcludeList = [];
private ?Snapshot $snapshot = null;

/**
* @psalm-var list<callable>
*/
private ?array $backupGlobalErrorHandlers = null;

/**
* @psalm-var list<callable>
*/
private ?array $backupGlobalExceptionHandlers = null;
private ?bool $runClassInSeparateProcess = null;
private ?bool $runTestInSeparateProcess = null;
private bool $preserveGlobalState = false;
private bool $inIsolation = false;
private ?string $expectedException = null;
private ?string $expectedExceptionMessage = null;
private ?string $expectedExceptionMessageRegExp = null;
private null|int|string $expectedExceptionCode = null;
private ?bool $runClassInSeparateProcess = null;
private ?bool $runTestInSeparateProcess = null;
private bool $preserveGlobalState = false;
private bool $inIsolation = false;
private ?string $expectedException = null;
private ?string $expectedExceptionMessage = null;
private ?string $expectedExceptionMessageRegExp = null;
private null|int|string $expectedExceptionCode = null;

/**
* @psalm-var list<ExecutionOrderDependency>
Expand Down Expand Up @@ -633,16 +617,13 @@ final public function runBare(): void
{
$emitter = Event\Facade::emitter();

error_clear_last();
clearstatcache();

$emitter->testPreparationStarted(
$this->valueObjectForEvents(),
);

$this->snapshotGlobalState();
$this->snapshotGlobalErrorExceptionHandlers();
$this->startOutputBuffering();
clearstatcache();

$hookMethods = (new HookMethods)->hookMethods(static::class);
$hasMetRequirements = false;
Expand Down Expand Up @@ -794,7 +775,6 @@ final public function runBare(): void
chdir($currentWorkingDirectory);
}

$this->restoreGlobalErrorExceptionHandlers();
$this->restoreGlobalState();
$this->unregisterCustomComparators();
$this->cleanupIniSettings();
Expand Down Expand Up @@ -1702,119 +1682,6 @@ private function stopOutputBuffering(): bool
return true;
}

private function snapshotGlobalErrorExceptionHandlers(): void
{
$this->backupGlobalErrorHandlers = $this->getActiveErrorHandlers();
$this->backupGlobalExceptionHandlers = $this->getActiveExceptionHandlers();
}

/**
* @throws MoreThanOneDataSetFromDataProviderException
*/
private function restoreGlobalErrorExceptionHandlers(): void
{
$activeErrorHandlers = $this->getActiveErrorHandlers();
$activeExceptionHandlers = $this->getActiveExceptionHandlers();

$message = null;

if ($activeErrorHandlers !== $this->backupGlobalErrorHandlers) {
if (count($activeErrorHandlers) > count($this->backupGlobalErrorHandlers)) {
$message = 'Test code or tested code did not remove its own error handlers';
} else {
$message = 'Test code or tested code removed error handlers other than its own';
}

foreach ($activeErrorHandlers as $handler) {
restore_error_handler();
}

foreach ($this->backupGlobalErrorHandlers as $handler) {
set_error_handler($handler);
}
}

if ($activeExceptionHandlers !== $this->backupGlobalExceptionHandlers) {
if (count($activeExceptionHandlers) > count($this->backupGlobalExceptionHandlers)) {
$message = 'Test code or tested code did not remove its own exception handlers';
} else {
$message = 'Test code or tested code removed exception handlers other than its own';
}

foreach ($activeExceptionHandlers as $handler) {
restore_exception_handler();
}

foreach ($this->backupGlobalExceptionHandlers as $handler) {
set_exception_handler($handler);
}
}

$this->backupGlobalErrorHandlers = null;
$this->backupGlobalExceptionHandlers = null;

if ($message !== null) {
Event\Facade::emitter()->testConsideredRisky(
$this->valueObjectForEvents(),
$message,
);

$this->status = TestStatus::risky($message);
}
}

/**
* @return list<callable>
*/
private function getActiveErrorHandlers(): array
{
$res = [];

while (true) {
$previousHandler = set_error_handler(static fn () => false);
restore_error_handler();

if ($previousHandler === null) {
break;
}
$res[] = $previousHandler;
restore_error_handler();
}
$res = array_reverse($res);

foreach ($res as $handler) {
set_error_handler($handler);
}

return $res;
}

/**
* @return list<callable>
*/
private function getActiveExceptionHandlers(): array
{
$res = [];

while (true) {
$previousHandler = set_exception_handler(static fn () => null);
restore_exception_handler();

if ($previousHandler === null) {
break;
}
$res[] = $previousHandler;
restore_exception_handler();
}
$res = array_reverse($res);

foreach ($res as $handler) {
set_exception_handler($handler);
}

return $res;
}

private function snapshotGlobalState(): void
{
if ($this->runTestInSeparateProcess || $this->inIsolation ||
Expand Down
3 changes: 3 additions & 0 deletions src/Framework/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function assert;
use function class_exists;
use function defined;
use function error_clear_last;
use function extension_loaded;
use function get_include_path;
use function hrtime;
Expand Down Expand Up @@ -82,6 +83,8 @@ public function run(TestCase $test): void
$risky = false;
$skipped = false;

error_clear_last();

if ($this->shouldErrorHandlerBeUsed($test)) {
ErrorHandler::instance()->enable();
}
Expand Down
69 changes: 0 additions & 69 deletions tests/end-to-end/regression/5592.phpt

This file was deleted.

57 changes: 0 additions & 57 deletions tests/end-to-end/regression/5592/Issue5592Test.php

This file was deleted.

0 comments on commit 23c16c1

Please sign in to comment.