Skip to content

Commit ccc3e20

Browse files
committed
unit test error handler
1 parent af0dc23 commit ccc3e20

File tree

3 files changed

+65
-21
lines changed

3 files changed

+65
-21
lines changed

src/Runner/ErrorHandler.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24
/*
35
* This file is part of PHPUnit.
46
*
@@ -209,6 +211,11 @@ public function deprecationHandler(int $errorNumber, string $errorString, string
209211
return true;
210212
}
211213

214+
public function registerDeprecationHandler(): void
215+
{
216+
set_error_handler([self::$instance, 'deprecationHandler'], E_USER_DEPRECATED);
217+
}
218+
212219
public function restoreDeprecationHandler(): void
213220
{
214221
restore_error_handler();
@@ -262,14 +269,6 @@ public function useDeprecationTriggers(array $deprecationTriggers): void
262269
$this->deprecationTriggers = $deprecationTriggers;
263270
}
264271

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-
273272
/**
274273
* @param non-empty-string $file
275274
* @param positive-int $line
@@ -305,9 +304,11 @@ private function trigger(TestMethod $test, bool $filterTrigger): IssueTrigger
305304
}
306305
}
307306

308-
if (isset($trace[1]['file']) &&
307+
if (
308+
isset($trace[1]['file']) &&
309309
($trace[1]['file'] === $test->file() ||
310-
SourceFilter::instance()->includes($trace[1]['file']))) {
310+
SourceFilter::instance()->includes($trace[1]['file']))
311+
) {
311312
$triggerCalledFromFirstPartyCode = true;
312313
}
313314

src/TextUI/Application.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24
/*
35
* This file is part of PHPUnit.
46
*
@@ -9,7 +11,6 @@
911
*/
1012
namespace PHPUnit\TextUI;
1113

12-
use const E_USER_DEPRECATED;
1314
use const PHP_EOL;
1415
use const PHP_VERSION;
1516
use function assert;
@@ -21,7 +22,6 @@
2122
use function method_exists;
2223
use function printf;
2324
use function realpath;
24-
use function set_error_handler;
2525
use function sprintf;
2626
use function str_contains;
2727
use function trim;
@@ -180,7 +180,7 @@ public function run(array $argv): int
180180

181181
EventFacade::instance()->seal();
182182

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

185185
$testSuite = $this->buildTestSuite($configuration);
186186

@@ -227,8 +227,10 @@ public function run(array $argv): int
227227
$testDoxResult = $testDoxResultCollector->testMethodsGroupedByClass();
228228
}
229229

230-
if ($testDoxResult !== null &&
231-
$configuration->hasLogfileTestdoxHtml()) {
230+
if (
231+
$testDoxResult !== null &&
232+
$configuration->hasLogfileTestdoxHtml()
233+
) {
232234
try {
233235
OutputFacade::printerFor($configuration->logfileTestdoxHtml())->print(
234236
(new TestDoxHtmlRenderer)->render($testDoxResult),
@@ -244,8 +246,10 @@ public function run(array $argv): int
244246
}
245247
}
246248

247-
if ($testDoxResult !== null &&
248-
$configuration->hasLogfileTestdoxText()) {
249+
if (
250+
$testDoxResult !== null &&
251+
$configuration->hasLogfileTestdoxText()
252+
) {
249253
try {
250254
OutputFacade::printerFor($configuration->logfileTestdoxText())->print(
251255
(new TestDoxTextRenderer)->render($testDoxResult),
@@ -684,9 +688,11 @@ private function registerLogfileWriters(Configuration $configuration): void
684688
*/
685689
private function testDoxResultCollector(Configuration $configuration): ?TestDoxResultCollector
686690
{
687-
if ($configuration->hasLogfileTestdoxHtml() ||
691+
if (
692+
$configuration->hasLogfileTestdoxHtml() ||
688693
$configuration->hasLogfileTestdoxText() ||
689-
$configuration->outputIsTestDox()) {
694+
$configuration->outputIsTestDox()
695+
) {
690696
return new TestDoxResultCollector(
691697
EventFacade::instance(),
692698
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 <sebastian@phpunit.de>
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)