Skip to content

Commit 233632d

Browse files
committed
guess deprecation frame before checking for ignored baseline
1 parent fdd7967 commit 233632d

File tree

9 files changed

+146
-4
lines changed

9 files changed

+146
-4
lines changed

src/Runner/ErrorHandler.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
100100

101101
$test = Event\Code\TestMethodBuilder::fromCallStack();
102102

103+
if ($errorNumber === E_USER_DEPRECATED) {
104+
$deprecationFrame = $this->guessDeprecationFrame();
105+
$errorFile = $deprecationFrame['file'] ?? $errorFile;
106+
$errorLine = $deprecationFrame['line'] ?? $errorLine;
107+
}
108+
103109
$ignoredByBaseline = $this->ignoredByBaseline($errorFile, $errorLine, $errorString);
104110
$ignoredByTest = $test->metadata()->isIgnoreDeprecations()->isNotEmpty();
105111

@@ -167,13 +173,11 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
167173
break;
168174

169175
case E_USER_DEPRECATED:
170-
$deprecationFrame = $this->guessDeprecationFrame();
171-
172176
Event\Facade::emitter()->testTriggeredDeprecation(
173177
$test,
174178
$errorString,
175-
$deprecationFrame['file'] ?? $errorFile,
176-
$deprecationFrame['line'] ?? $errorLine,
179+
$errorFile,
180+
$errorLine,
177181
$suppressed,
178182
$ignoredByBaseline,
179183
$ignoredByTest,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<files version="1">
3+
<file path="vendor/ThirdPartyClass.php">
4+
<line number="4" hash="67f46da0ae62f1f93b33314db1c7a53dd56f4f49">
5+
<issue><![CDATA[deprecation in third-party code]]></issue>
6+
</line>
7+
</file>
8+
</files>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 ignoreSuppressionOfDeprecations="true" ignoreIndirectDeprecations="false" baseline="baseline.xml">
13+
<include>
14+
<directory>src</directory>
15+
</include>
16+
17+
<deprecationTrigger>
18+
<function>PHPUnit\TestFixture\BaselineIgnoreDeprecation\trigger_deprecation</function>
19+
</deprecationTrigger>
20+
</source>
21+
</phpunit>
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 <[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\TestFixture\BaselineIgnoreDeprecation;
11+
12+
final class FirstPartyClass
13+
{
14+
public function method(): true
15+
{
16+
(new ThirdPartyClass)->method();
17+
18+
return true;
19+
}
20+
}
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 <[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\TestFixture\BaselineIgnoreDeprecation;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class FirstPartyClassTest extends TestCase
15+
{
16+
public function testOne(): void
17+
{
18+
$this->assertTrue((new ThirdPartyClass)->anotherMethod());
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types=1);
2+
namespace PHPUnit\TestFixture\BaselineIgnoreDeprecation;
3+
4+
trigger_deprecation('deprecation in third-party code');
5+
6+
final class ThirdPartyClass
7+
{
8+
public function anotherMethod(): true
9+
{
10+
return true;
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
require __DIR__ . '/trigger_deprecation.php';
3+
4+
spl_autoload_register(function ($class) {
5+
$parts = explode('\\', $class);
6+
$file = end($parts) . '.php';
7+
8+
match ($file) {
9+
'FirstPartyClass.php' => require __DIR__ . '/../src/' . $file,
10+
'ThirdPartyClass.php' => require __DIR__ . '/' . $file,
11+
default => throw new LogicException
12+
};
13+
});
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
namespace PHPUnit\TestFixture\BaselineIgnoreDeprecation;
3+
4+
function trigger_deprecation(string $message): void
5+
{
6+
@trigger_error($message, E_USER_DEPRECATED);
7+
}
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 runs code which 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-baseline-ignore';
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
13+
--EXPECTF--
14+
PHPUnit Started (%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\BaselineIgnoreDeprecation\FirstPartyClassTest, 1 test)
25+
Test Preparation Started (PHPUnit\TestFixture\BaselineIgnoreDeprecation\FirstPartyClassTest::testOne)
26+
Test Prepared (PHPUnit\TestFixture\BaselineIgnoreDeprecation\FirstPartyClassTest::testOne)
27+
Test Triggered Deprecation (PHPUnit\TestFixture\BaselineIgnoreDeprecation\FirstPartyClassTest::testOne, issue triggered by third-party code, suppressed using operator, ignored by baseline) in %s:%d
28+
deprecation in third-party code
29+
Test Passed (PHPUnit\TestFixture\BaselineIgnoreDeprecation\FirstPartyClassTest::testOne)
30+
Test Finished (PHPUnit\TestFixture\BaselineIgnoreDeprecation\FirstPartyClassTest::testOne)
31+
Test Suite Finished (PHPUnit\TestFixture\BaselineIgnoreDeprecation\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)