Skip to content

Commit

Permalink
Closes #5428
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 27, 2023
1 parent 41c4b93 commit e676667
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog-10.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes of the PHPUnit 10.3 release series are documented in this fi

## [10.3.0] - 2023-08-04

### Added

* [#5428](https://github.com/sebastianbergmann/phpunit/issues/5428): Attribute `#[WithoutErrorHandler]` to disable PHPUnit's error handler for a test method

### Changed

* When a test case class inherits test methods from a parent class then, by default (when no test reordering is requested), the test methods from the class that is highest in the inheritance tree (instead of the class that is lowest in the inheritance tree) are now run first
Expand Down
13 changes: 12 additions & 1 deletion src/Framework/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public function run(TestCase $test): void
$risky = false;
$skipped = false;

ErrorHandler::instance()->enable();
if ($this->shouldErrorHandlerBeUsed($test)) {
ErrorHandler::instance()->enable();
}

$collectCodeCoverage = CodeCoverage::instance()->isActive() &&
$shouldCodeCoverageBeCollected;
Expand Down Expand Up @@ -452,4 +454,13 @@ private function saveConfigurationForChildProcess(): string

return $path;
}

private function shouldErrorHandlerBeUsed(TestCase $test): bool
{
if (MetadataRegistry::parser()->forMethod($test::class, $test->name())->isWithoutErrorHandler()->isNotEmpty()) {
return false;
}

return true;
}
}
62 changes: 62 additions & 0 deletions tests/end-to-end/event/error-handler-can-be-disabled.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--TEST--
The right events are emitted in the right order when PHPUnit's error handler is disabled
--SKIPIF--
<?php declare(strict_types=1);
if (DIRECTORY_SEPARATOR === '\\') {
print "skip: this test does not work on Windows / GitHub Actions\n";
}
--FILE--
<?php declare(strict_types=1);
$traceFile = tempnam(sys_get_temp_dir(), __FILE__);

$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-output';
$_SERVER['argv'][] = '--log-events-text';
$_SERVER['argv'][] = $traceFile;
$_SERVER['argv'][] = '--fail-on-notice';
$_SERVER['argv'][] = '--configuration';
$_SERVER['argv'][] = __DIR__ . '/_files/error-handler-can-be-disabled';

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);

print file_get_contents($traceFile);

unlink($traceFile);
--EXPECTF--
PHPUnit Started (PHPUnit %s using %s)
Test Runner Configured
Bootstrap Finished (%s/src/Foo.php)
Test Suite Loaded (2 tests)
Event Facade Sealed
Test Runner Started
Test Suite Sorted
Test Runner Execution Started (2 tests)
Test Suite Started (%s/phpunit.xml, 2 tests)
Test Suite Started (default, 2 tests)
Test Suite Started (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest, 2 tests)
Test Preparation Started (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
Test Prepared (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
Assertion Succeeded (Constraint: exception of type "Exception", Value: Exception Object #92 (
'message' => 'fopen(%s/missing/directory): Failed to open stream: No such file or directory'
'string' => ''
'code' => 0
'file' => '%s/src/Foo.php'
'line' => 26
'previous' => null
))
Assertion Succeeded (Constraint: exception message contains 'Failed to open stream', Value: 'fopen(%s/missing/directory): Failed to open stream: No such file or directory')
Test Passed (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
Test Finished (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
Test Preparation Started (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
Test Prepared (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
Assertion Succeeded (Constraint: is identical to 'Triggering', Value: 'Triggering')
Test Passed (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
Test Finished (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
Test Suite Finished (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest, 2 tests)
Test Suite Finished (default, 2 tests)
Test Suite Finished (%s/phpunit.xml, 2 tests)
Test Runner Execution Finished
Test Runner Finished
PHPUnit Finished (Shell Exit Code: 0)

0 comments on commit e676667

Please sign in to comment.