Skip to content

Commit 3a94e60

Browse files
committed
collect autoloading deprecations before running testCase
1 parent dd3bb4b commit 3a94e60

File tree

10 files changed

+143
-3
lines changed

10 files changed

+143
-3
lines changed

src/Runner/ErrorHandler.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ final class ErrorHandler
5959
private ?int $originalErrorReportingLevel = null;
6060
private readonly Source $source;
6161

62+
/**
63+
* @var list<array{int, string, string, int}>
64+
*/
65+
private array $globalDeprecations = [];
66+
6267
/**
6368
* @var ?array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
6469
*/
@@ -197,6 +202,18 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
197202
return false;
198203
}
199204

205+
public function deprecationHandler(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
206+
{
207+
$this->globalDeprecations[] = [$errorNumber, $errorString, $errorFile, $errorLine];
208+
209+
return true;
210+
}
211+
212+
public function restoreDeprecationHandler(): void
213+
{
214+
restore_error_handler();
215+
}
216+
200217
public function enable(): void
201218
{
202219
if ($this->enabled) {
@@ -213,6 +230,7 @@ public function enable(): void
213230

214231
$this->enabled = true;
215232
$this->originalErrorReportingLevel = error_reporting();
233+
$this->triggerGlobalDeprecations();
216234

217235
error_reporting($this->originalErrorReportingLevel & self::UNHANDLEABLE_LEVELS);
218236
}
@@ -244,6 +262,14 @@ public function useDeprecationTriggers(array $deprecationTriggers): void
244262
$this->deprecationTriggers = $deprecationTriggers;
245263
}
246264

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+
247273
/**
248274
* @param non-empty-string $file
249275
* @param positive-int $line
@@ -422,4 +448,11 @@ private function stackTrace(): string
422448

423449
return $buffer;
424450
}
451+
452+
private function triggerGlobalDeprecations(): void
453+
{
454+
foreach ($this->globalDeprecations ?? [] as $d) {
455+
$this->__invoke(...$d);
456+
}
457+
}
425458
}

src/TextUI/Application.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace PHPUnit\TextUI;
1111

12+
use const E_USER_DEPRECATED;
1213
use const PHP_EOL;
1314
use const PHP_VERSION;
1415
use function assert;
@@ -20,6 +21,7 @@
2021
use function method_exists;
2122
use function printf;
2223
use function realpath;
24+
use function set_error_handler;
2325
use function sprintf;
2426
use function str_contains;
2527
use function trim;
@@ -115,6 +117,8 @@ public function run(array $argv): int
115117
$xmlConfiguration,
116118
);
117119

120+
set_error_handler([ErrorHandler::instance(), 'deprecationHandler'], E_USER_DEPRECATED);
121+
118122
(new PhpHandler)->handle($configuration->php());
119123

120124
if ($configuration->hasBootstrap()) {
@@ -177,9 +181,10 @@ public function run(array $argv): int
177181
$baselineGenerator = $this->configureBaseline($configuration);
178182

179183
EventFacade::instance()->seal();
180-
181184
$testSuite = $this->buildTestSuite($configuration);
182185

186+
ErrorHandler::instance()->restoreDeprecationHandler();
187+
183188
$this->executeCommandsThatRequireTheTestSuite($configuration, $cliConfiguration, $testSuite);
184189

185190
if ($testSuite->isEmpty() && !$configuration->hasCliArguments() && $configuration->testSuite()->isEmpty()) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="../../../../../phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
>
6+
<testsuites>
7+
<testsuite name="default">
8+
<directory>tests</directory>
9+
</testsuite>
10+
</testsuites>
11+
12+
<source>
13+
<include>
14+
<directory>src</directory>
15+
</include>
16+
</source>
17+
</phpunit>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\TestFixture\SelfDirectIndirect;
11+
12+
final class FirstPartyClass
13+
{
14+
public function method(): true
15+
{
16+
return ThirdPartyClass::A;
17+
}
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
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\TestFixture\SelfDirectIndirect;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class FirstPartyClassTest extends TestCase
15+
{
16+
public function testOne(): void
17+
{
18+
$this->assertTrue((new FirstPartyClass)->method());
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php declare(strict_types=1);
2+
namespace PHPUnit\TestFixture\SelfDirectIndirect;
3+
4+
@trigger_error('This class is deprecated', E_USER_DEPRECATED);
5+
6+
final class ThirdPartyClass
7+
{
8+
const A = true;
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php declare(strict_types=1);
2+
require __DIR__ . '/../src/FirstPartyClass.php';
3+
require __DIR__ . '/ThirdPartyClass.php';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
The right events are emitted in the right order for a test that loads a class that triggers E_USER_DEPRECATED
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--debug';
7+
$_SERVER['argv'][] = '--configuration';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/deprecation-trigger-class';
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit Started (PHPUnit %s using %s)
15+
Test Runner Configured
16+
Bootstrap Finished (%sautoload.php)
17+
Event Facade Sealed
18+
Test Suite Loaded (1 test)
19+
Test Runner Started
20+
Test Suite Sorted
21+
Test Runner Execution Started (1 test)
22+
Test Suite Started (%sphpunit.xml, 1 test)
23+
Test Suite Started (default, 1 test)
24+
Test Suite Started (PHPUnit\TestFixture\SelfDirectIndirect\FirstPartyClassTest, 1 test)
25+
Test Triggered Deprecation (PHPUnit\TestFixture\SelfDirectIndirect\FirstPartyClassTest::testOne, issue triggered by third-party code) in %s:%d
26+
This class is deprecated
27+
Test Preparation Started (PHPUnit\TestFixture\SelfDirectIndirect\FirstPartyClassTest::testOne)
28+
Test Prepared (PHPUnit\TestFixture\SelfDirectIndirect\FirstPartyClassTest::testOne)
29+
Test Passed (PHPUnit\TestFixture\SelfDirectIndirect\FirstPartyClassTest::testOne)
30+
Test Finished (PHPUnit\TestFixture\SelfDirectIndirect\FirstPartyClassTest::testOne)
31+
Test Suite Finished (PHPUnit\TestFixture\SelfDirectIndirect\FirstPartyClassTest, 1 test)
32+
Test Suite Finished (default, 1 test)
33+
Test Suite Finished (%sphpunit.xml, 1 test)
34+
Test Runner Execution Finished
35+
Test Runner Finished
36+
PHPUnit Finished (Shell Exit Code: 0)

tests/end-to-end/regression/5844/Issue5844Test.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ final class Issue5844Test extends TestCase
1515
{
1616
public function testOne(): void
1717
{
18+
new CustomErrorHandler;
1819
$this->assertTrue(true);
1920
}
2021
}

tests/end-to-end/regression/5844/bootstrap.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ private function handleError(): void
2222
{
2323
}
2424
}
25-
26-
new CustomErrorHandler;

0 commit comments

Comments
 (0)