Skip to content

Commit 452fedf

Browse files
committed
unit test error handler
1 parent 434cd5e commit 452fedf

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/Runner/ErrorHandler.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ public function deprecationHandler(int $errorNumber, string $errorString, string
209209
return true;
210210
}
211211

212+
public function registerDeprecationHandler(): void
213+
{
214+
set_error_handler([self::$instance, 'deprecationHandler'], E_USER_DEPRECATED);
215+
}
216+
212217
public function restoreDeprecationHandler(): void
213218
{
214219
restore_error_handler();
@@ -262,14 +267,6 @@ public function useDeprecationTriggers(array $deprecationTriggers): void
262267
$this->deprecationTriggers = $deprecationTriggers;
263268
}
264269

265-
/**
266-
* @param list<array{int, string, string, int}> $deprecations
267-
*/
268-
public function collectGlobalDeprecations(array $deprecations): void
269-
{
270-
$this->globalDeprecations = $deprecations;
271-
}
272-
273270
/**
274271
* @param non-empty-string $file
275272
* @param positive-int $line

src/TextUI/Application.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
namespace PHPUnit\TextUI;
1111

12-
use const E_USER_DEPRECATED;
1312
use const PHP_EOL;
1413
use const PHP_VERSION;
1514
use function assert;
@@ -21,7 +20,6 @@
2120
use function method_exists;
2221
use function printf;
2322
use function realpath;
24-
use function set_error_handler;
2523
use function sprintf;
2624
use function str_contains;
2725
use function trim;
@@ -180,7 +178,7 @@ public function run(array $argv): int
180178

181179
EventFacade::instance()->seal();
182180

183-
set_error_handler([ErrorHandler::instance(), 'deprecationHandler'], E_USER_DEPRECATED);
181+
ErrorHandler::instance()->registerDeprecationHandler();
184182

185183
$testSuite = $this->buildTestSuite($configuration);
186184

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 const E_USER_DEPRECATED;
13+
use function trigger_error;
14+
use PHPUnit\Framework\Attributes\CoversClass;
15+
use PHPUnit\Framework\Attributes\Small;
16+
use PHPUnit\Framework\TestCase;
17+
use ReflectionClass;
18+
19+
#[CoversClass(ErrorHandler::class)]
20+
#[Small]
21+
final class ErrorHandlerTest extends TestCase
22+
{
23+
public function testThrowsExceptionWhenUsingInvalidOrderOption(): void
24+
{
25+
$errorHandler = ErrorHandler::instance();
26+
$errorHandler->registerDeprecationHandler();
27+
trigger_error('deprecation', E_USER_DEPRECATED);
28+
$errorHandler->restoreDeprecationHandler();
29+
$refl = new ReflectionClass($errorHandler);
30+
$globalDeprecations = $refl->getProperty('globalDeprecations');
31+
$registeredDeprecations = $globalDeprecations->getValue($errorHandler);
32+
$this->assertCount(1, $registeredDeprecations);
33+
$this->assertSame('deprecation', $registeredDeprecations[0][1]);
34+
}
35+
}

0 commit comments

Comments
 (0)