Skip to content

Commit d02f1ab

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

File tree

8 files changed

+138
-1
lines changed

8 files changed

+138
-1
lines changed

src/Runner/ErrorHandler.php

Lines changed: 21 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 = null;
66+
6267
/**
6368
* @var ?array{functions: list<non-empty-string>, methods: list<array{className: class-string, methodName: non-empty-string}>}
6469
*/
@@ -213,6 +218,7 @@ public function enable(): void
213218

214219
$this->enabled = true;
215220
$this->originalErrorReportingLevel = error_reporting();
221+
$this->triggerGlobalDeprecations();
216222

217223
error_reporting($this->originalErrorReportingLevel & self::UNHANDLEABLE_LEVELS);
218224
}
@@ -244,6 +250,14 @@ public function useDeprecationTriggers(array $deprecationTriggers): void
244250
$this->deprecationTriggers = $deprecationTriggers;
245251
}
246252

253+
/**
254+
* @param list<array{int, string, string, int}> $deprecations
255+
*/
256+
public function collectGlobalDeprecations(array $deprecations): void
257+
{
258+
$this->globalDeprecations = $deprecations;
259+
}
260+
247261
/**
248262
* @param non-empty-string $file
249263
* @param positive-int $line
@@ -422,4 +436,11 @@ private function stackTrace(): string
422436

423437
return $buffer;
424438
}
439+
440+
private function triggerGlobalDeprecations(): void
441+
{
442+
foreach ($this->globalDeprecations ?? [] as $d) {
443+
$this->__invoke(...$d);
444+
}
445+
}
425446
}

src/TextUI/Application.php

Lines changed: 14 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,8 @@
2021
use function method_exists;
2122
use function printf;
2223
use function realpath;
24+
use function restore_error_handler;
25+
use function set_error_handler;
2326
use function sprintf;
2427
use function str_contains;
2528
use function trim;
@@ -115,6 +118,14 @@ public function run(array $argv): int
115118
$xmlConfiguration,
116119
);
117120

121+
$deprecations = [];
122+
set_error_handler(static function (int $errorNumber, string $errorString, string $errorFile, int $errorLine) use (&$deprecations): bool
123+
{
124+
$deprecations[] = [$errorNumber, $errorString, $errorFile, $errorLine];
125+
126+
return true;
127+
}, E_USER_DEPRECATED);
128+
118129
(new PhpHandler)->handle($configuration->php());
119130

120131
if ($configuration->hasBootstrap()) {
@@ -177,9 +188,11 @@ public function run(array $argv): int
177188
$baselineGenerator = $this->configureBaseline($configuration);
178189

179190
EventFacade::instance()->seal();
180-
181191
$testSuite = $this->buildTestSuite($configuration);
182192

193+
restore_error_handler();
194+
ErrorHandler::instance()->collectGlobalDeprecations($deprecations);
195+
183196
$this->executeCommandsThatRequireTheTestSuite($configuration, $cliConfiguration, $testSuite);
184197

185198
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 first-party code calling into third-party code, suppressed using operator) 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)

0 commit comments

Comments
 (0)