Skip to content

Commit 987f371

Browse files
committed
unit test error handler
1 parent 434cd5e commit 987f371

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

src/Runner/ErrorHandler.php

Lines changed: 7 additions & 9 deletions
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
@@ -305,7 +302,8 @@ private function trigger(TestMethod $test, bool $filterTrigger): IssueTrigger
305302
}
306303
}
307304

308-
if (isset($trace[1]['file']) &&
305+
if (
306+
isset($trace[1]['file']) &&
309307
($trace[1]['file'] === $test->file() ||
310308
SourceFilter::instance()->includes($trace[1]['file']))) {
311309
$triggerCalledFromFirstPartyCode = true;

src/TextUI/Application.php

Lines changed: 5 additions & 5 deletions
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

@@ -684,9 +682,11 @@ private function registerLogfileWriters(Configuration $configuration): void
684682
*/
685683
private function testDoxResultCollector(Configuration $configuration): ?TestDoxResultCollector
686684
{
687-
if ($configuration->hasLogfileTestdoxHtml() ||
685+
if (
686+
$configuration->hasLogfileTestdoxHtml() ||
688687
$configuration->hasLogfileTestdoxText() ||
689-
$configuration->outputIsTestDox()) {
688+
$configuration->outputIsTestDox()
689+
) {
690690
return new TestDoxResultCollector(
691691
EventFacade::instance(),
692692
new IssueFilter($configuration->source()),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of PHPUnit.
6+
*
7+
* (c) Sebastian Bergmann <[email protected]>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
namespace PHPUnit\Runner;
13+
14+
use const E_USER_DEPRECATED;
15+
use function trigger_error;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\Attributes\Small;
18+
use PHPUnit\Framework\TestCase;
19+
use ReflectionClass;
20+
21+
#[CoversClass(ErrorHandler::class)]
22+
#[Small]
23+
final class ErrorHandlerTest extends TestCase
24+
{
25+
public function testThrowsExceptionWhenUsingInvalidOrderOption(): void
26+
{
27+
$errorHandler = ErrorHandler::instance();
28+
$errorHandler->registerDeprecationHandler();
29+
trigger_error('deprecation', E_USER_DEPRECATED);
30+
$errorHandler->restoreDeprecationHandler();
31+
$refl = new ReflectionClass($errorHandler);
32+
$globalDeprecations = $refl->getProperty('globalDeprecations');
33+
$registeredDeprecations = $globalDeprecations->getValue($errorHandler);
34+
$this->assertCount(1, $registeredDeprecations);
35+
$this->assertSame('deprecation', $registeredDeprecations[0][1]);
36+
}
37+
}

0 commit comments

Comments
 (0)