Skip to content

Commit 3a95483

Browse files
Merge branch '10.5'
2 parents ef80a8d + c1cf480 commit 3a95483

20 files changed

+239
-54
lines changed

src/Framework/TestRunner.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function assert;
1414
use function class_exists;
1515
use function defined;
16+
use function error_clear_last;
1617
use function extension_loaded;
1718
use function get_include_path;
1819
use function hrtime;
@@ -78,6 +79,8 @@ public function run(TestCase $test): void
7879
$risky = false;
7980
$skipped = false;
8081

82+
error_clear_last();
83+
8184
if ($this->shouldErrorHandlerBeUsed($test)) {
8285
ErrorHandler::instance()->enable();
8386
}

src/Runner/ErrorHandler.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@
99
*/
1010
namespace PHPUnit\Runner;
1111

12+
use const E_COMPILE_ERROR;
13+
use const E_COMPILE_WARNING;
14+
use const E_CORE_ERROR;
15+
use const E_CORE_WARNING;
1216
use const E_DEPRECATED;
17+
use const E_ERROR;
1318
use const E_NOTICE;
19+
use const E_PARSE;
20+
use const E_RECOVERABLE_ERROR;
1421
use const E_STRICT;
1522
use const E_USER_DEPRECATED;
23+
use const E_USER_ERROR;
1624
use const E_USER_NOTICE;
1725
use const E_USER_WARNING;
1826
use const E_WARNING;
@@ -30,9 +38,12 @@
3038
*/
3139
final class ErrorHandler
3240
{
33-
private static ?self $instance = null;
34-
private ?Baseline $baseline = null;
35-
private bool $enabled = false;
41+
private const UNHANDLEABLE_LEVELS = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING;
42+
private const INSUPPRESSIBLE_LEVELS = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
43+
private static ?self $instance = null;
44+
private ?Baseline $baseline = null;
45+
private bool $enabled = false;
46+
private ?int $originalErrorReportingLevel = null;
3647

3748
public static function instance(): self
3849
{
@@ -44,7 +55,7 @@ public static function instance(): self
4455
*/
4556
public function __invoke(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
4657
{
47-
$suppressed = !($errorNumber & error_reporting());
58+
$suppressed = (error_reporting() & ~self::INSUPPRESSIBLE_LEVELS) === 0;
4859

4960
if ($suppressed && (new ExcludeList)->isExcluded($errorFile)) {
5061
return false;
@@ -140,13 +151,13 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
140151
$suppressed,
141152
);
142153

143-
break;
154+
throw new ErrorException('E_USER_ERROR was triggered');
144155

145156
default:
146157
return false;
147158
}
148159

149-
return true;
160+
return false;
150161
}
151162

152163
public function enable(): void
@@ -155,15 +166,12 @@ public function enable(): void
155166
return;
156167
}
157168

158-
$oldErrorHandler = set_error_handler($this);
169+
set_error_handler($this);
159170

160-
if ($oldErrorHandler !== null) {
161-
restore_error_handler();
171+
$this->enabled = true;
172+
$this->originalErrorReportingLevel = error_reporting();
162173

163-
return;
164-
}
165-
166-
$this->enabled = true;
174+
error_reporting($this->originalErrorReportingLevel & self::UNHANDLEABLE_LEVELS);
167175
}
168176

169177
public function disable(): void
@@ -174,7 +182,10 @@ public function disable(): void
174182

175183
restore_error_handler();
176184

177-
$this->enabled = false;
185+
error_reporting(error_reporting() | $this->originalErrorReportingLevel);
186+
187+
$this->enabled = false;
188+
$this->originalErrorReportingLevel = null;
178189
}
179190

180191
public function use(Baseline $baseline): void
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Runner;
11+
12+
use Error;
13+
14+
/**
15+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
16+
*/
17+
final class ErrorException extends Error implements Exception
18+
{
19+
}

tests/end-to-end/event/_files/DeprecatedFeatureTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHPUnit\TestFixture\Event;
1111

1212
use const E_USER_DEPRECATED;
13+
use function error_get_last;
1314
use function trigger_error;
1415
use PHPUnit\Framework\TestCase;
1516

@@ -22,4 +23,11 @@ public function testDeprecatedFeature(): void
2223

2324
$this->assertTrue(true);
2425
}
26+
27+
public function testDeprecatedSuppressedErrorGetLast(): void
28+
{
29+
$this->assertNull(error_get_last());
30+
@trigger_error('message', E_USER_DEPRECATED);
31+
$this->assertIsArray(error_get_last());
32+
}
2533
}

tests/end-to-end/event/_files/IgnoreDeprecationsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\TestFixture\Event;
1111

12+
use function error_get_last;
1213
use function trigger_error;
1314
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1415
use PHPUnit\Framework\TestCase;
@@ -29,4 +30,19 @@ public function testTwo(): void
2930

3031
$this->assertTrue(true);
3132
}
33+
34+
#[IgnoreDeprecations]
35+
public function testOneErrorGetLast(): void
36+
{
37+
$this->assertNull(error_get_last());
38+
trigger_error('message', E_USER_DEPRECATED);
39+
$this->assertIsArray(error_get_last());
40+
}
41+
42+
public function testTwoErrorGetLast(): void
43+
{
44+
$this->assertNull(error_get_last());
45+
trigger_error('message', E_USER_DEPRECATED);
46+
$this->assertIsArray(error_get_last());
47+
}
3248
}

tests/end-to-end/event/_files/SuppressedUserNoticeTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\TestFixture\Event;
1111

12+
use function error_get_last;
1213
use function trigger_error;
1314
use PHPUnit\Framework\TestCase;
1415

@@ -20,4 +21,11 @@ public function testSuppressedUserNotice(): void
2021

2122
@trigger_error('message', E_USER_NOTICE);
2223
}
24+
25+
public function testSuppressedUserNoticeErrorGetLast(): void
26+
{
27+
$this->assertNull(error_get_last());
28+
@trigger_error('message', E_USER_NOTICE);
29+
$this->assertIsArray(error_get_last());
30+
}
2331
}

tests/end-to-end/event/_files/SuppressedUserWarningTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\TestFixture\Event;
1111

12+
use function error_get_last;
1213
use function trigger_error;
1314
use PHPUnit\Framework\TestCase;
1415

@@ -20,4 +21,11 @@ public function testSuppressedUserWarning(): void
2021

2122
@trigger_error('message', E_USER_WARNING);
2223
}
24+
25+
public function testSuppressedUserWarningErrorGetLast(): void
26+
{
27+
$this->assertNull(error_get_last());
28+
@trigger_error('message', E_USER_WARNING);
29+
$this->assertIsArray(error_get_last());
30+
}
2331
}

tests/end-to-end/event/_files/UserErrorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ public function testUserError(): void
2020

2121
trigger_error('message', E_USER_ERROR);
2222
}
23+
24+
public function testUserErrorMustAbortExecution(): void
25+
{
26+
trigger_error('message', E_USER_ERROR);
27+
$this->assertTrue(false);
28+
}
2329
}

tests/end-to-end/event/_files/UserNoticeTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\TestFixture\Event;
1111

12+
use function error_get_last;
1213
use function trigger_error;
1314
use PHPUnit\Framework\TestCase;
1415

@@ -20,4 +21,11 @@ public function testUserNotice(): void
2021

2122
trigger_error('message', E_USER_NOTICE);
2223
}
24+
25+
public function testUserNoticeErrorGetLast(): void
26+
{
27+
$this->assertNull(error_get_last());
28+
trigger_error('message', E_USER_NOTICE);
29+
$this->assertIsArray(error_get_last());
30+
}
2331
}

tests/end-to-end/event/_files/UserWarningTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\TestFixture\Event;
1111

12+
use function error_get_last;
1213
use function trigger_error;
1314
use PHPUnit\Framework\TestCase;
1415

@@ -20,4 +21,11 @@ public function testUserWarning(): void
2021

2122
trigger_error('message', E_USER_WARNING);
2223
}
24+
25+
public function testUserWarningErrorGetLast(): void
26+
{
27+
$this->assertNull(error_get_last());
28+
trigger_error('message', E_USER_WARNING);
29+
$this->assertIsArray(error_get_last());
30+
}
2331
}

0 commit comments

Comments
 (0)