Skip to content

Commit 3baace5

Browse files
Closes #5428
1 parent 959fed0 commit 3baace5

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

ChangeLog-10.3.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes of the PHPUnit 10.3 release series are documented in this fi
44

55
## [10.3.0] - 2023-08-04
66

7+
### Added
8+
9+
* [#5428](https://github.com/sebastianbergmann/phpunit/issues/5428): Attribute `#[WithoutErrorHandler]` to disable PHPUnit's error handler for a test method
10+
711
### Changed
812

913
* 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

src/Framework/TestRunner.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public function run(TestCase $test): void
8484
$risky = false;
8585
$skipped = false;
8686

87-
ErrorHandler::instance()->enable();
87+
if ($this->shouldErrorHandlerBeUsed($test)) {
88+
ErrorHandler::instance()->enable();
89+
}
8890

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

453455
return $path;
454456
}
457+
458+
private function shouldErrorHandlerBeUsed(TestCase $test): bool
459+
{
460+
if (MetadataRegistry::parser()->forMethod($test::class, $test->name())->isWithoutErrorHandler()->isNotEmpty()) {
461+
return false;
462+
}
463+
464+
return true;
465+
}
455466
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--TEST--
2+
The right events are emitted in the right order when PHPUnit's error handler is disabled
3+
--SKIPIF--
4+
<?php declare(strict_types=1);
5+
if (DIRECTORY_SEPARATOR === '\\') {
6+
print "skip: this test does not work on Windows / GitHub Actions\n";
7+
}
8+
--FILE--
9+
<?php declare(strict_types=1);
10+
$traceFile = tempnam(sys_get_temp_dir(), __FILE__);
11+
12+
$_SERVER['argv'][] = '--do-not-cache-result';
13+
$_SERVER['argv'][] = '--no-output';
14+
$_SERVER['argv'][] = '--log-events-text';
15+
$_SERVER['argv'][] = $traceFile;
16+
$_SERVER['argv'][] = '--fail-on-notice';
17+
$_SERVER['argv'][] = '--configuration';
18+
$_SERVER['argv'][] = __DIR__ . '/_files/error-handler-can-be-disabled';
19+
20+
require __DIR__ . '/../../bootstrap.php';
21+
22+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
23+
24+
print file_get_contents($traceFile);
25+
26+
unlink($traceFile);
27+
--EXPECTF--
28+
PHPUnit Started (PHPUnit %s using %s)
29+
Test Runner Configured
30+
Bootstrap Finished (%s/src/Foo.php)
31+
Test Suite Loaded (2 tests)
32+
Event Facade Sealed
33+
Test Runner Started
34+
Test Suite Sorted
35+
Test Runner Execution Started (2 tests)
36+
Test Suite Started (%s/phpunit.xml, 2 tests)
37+
Test Suite Started (default, 2 tests)
38+
Test Suite Started (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest, 2 tests)
39+
Test Preparation Started (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
40+
Test Prepared (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
41+
Assertion Succeeded (Constraint: exception of type "Exception", Value: Exception Object #92 (
42+
'message' => 'fopen(%s/missing/directory): Failed to open stream: No such file or directory'
43+
'string' => ''
44+
'code' => 0
45+
'file' => '%s/src/Foo.php'
46+
'line' => 26
47+
'previous' => null
48+
))
49+
Assertion Succeeded (Constraint: exception message contains 'Failed to open stream', Value: 'fopen(%s/missing/directory): Failed to open stream: No such file or directory')
50+
Test Passed (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
51+
Test Finished (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodA)
52+
Test Preparation Started (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
53+
Test Prepared (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
54+
Assertion Succeeded (Constraint: is identical to 'Triggering', Value: 'Triggering')
55+
Test Passed (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
56+
Test Finished (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest::testMethodB)
57+
Test Suite Finished (PHPUnit\TestFixture\Event\ErrorHandlerCanBeDisabled\FooTest, 2 tests)
58+
Test Suite Finished (default, 2 tests)
59+
Test Suite Finished (%s/phpunit.xml, 2 tests)
60+
Test Runner Execution Finished
61+
Test Runner Finished
62+
PHPUnit Finished (Shell Exit Code: 0)

0 commit comments

Comments
 (0)