Skip to content

Commit b28be9f

Browse files
committed
Mute deprecations triggered from phpunit
1 parent 88575f0 commit b28be9f

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ public function handleError($type, $msg, $file, $line, $context = [])
128128
}
129129

130130
$deprecation = new Deprecation($msg, debug_backtrace(), $file);
131+
if ($deprecation->isMuted()) {
132+
return;
133+
}
131134
$group = 'other';
132135

133136
if ($deprecation->originatesFromAnObject()) {

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ class Deprecation
4747
*/
4848
private $originMethod;
4949

50-
/**
51-
* @var string one of the PATH_TYPE_* constants
52-
*/
53-
private $triggeringFilePathType;
50+
private $triggeringFile;
5451

5552
/** @var string[] absolute paths to vendor directories */
5653
private static $vendors;
@@ -76,7 +73,7 @@ public function __construct($message, array $trace, $file)
7673
// No-op
7774
}
7875
$line = $trace[$i];
79-
$this->triggeringFilePathType = $this->getPathType($file);
76+
$this->triggeringFile = $file;
8077
if (isset($line['object']) || isset($line['class'])) {
8178
if (isset($line['class']) && 0 === strpos($line['class'], SymfonyTestsListenerFor::class)) {
8279
$parsedMsg = unserialize($this->message);
@@ -88,7 +85,7 @@ public function __construct($message, array $trace, $file)
8885
// then we need to use the serialized information to determine
8986
// if the error has been triggered from vendor code.
9087
if (isset($parsedMsg['triggering_file'])) {
91-
$this->triggeringFilePathType = $this->getPathType($parsedMsg['triggering_file']);
88+
$this->triggeringFile = $parsedMsg['triggering_file'];
9289
}
9390

9491
return;
@@ -169,6 +166,26 @@ public function isLegacy($utilPrefix)
169166
|| \in_array('legacy', $test::getGroups($class, $method), true);
170167
}
171168

169+
/**
170+
* @return bool
171+
*/
172+
public function isMuted()
173+
{
174+
if (!\in_array($this->message, [
175+
'Function ReflectionType::__toString() is deprecated',
176+
], true)) {
177+
return false;
178+
}
179+
if (isset($this->trace[1]['class'])) {
180+
return 0 === strpos($this->trace[1]['class'], 'PHPUnit\\');
181+
}
182+
183+
return false !== strpos(
184+
$this->triggeringFile,
185+
DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'phpunit' . DIRECTORY_SEPARATOR
186+
);
187+
}
188+
172189
/**
173190
* Tells whether both the calling package and the called package are vendor
174191
* packages.
@@ -177,10 +194,11 @@ public function isLegacy($utilPrefix)
177194
*/
178195
public function getType()
179196
{
180-
if (self::PATH_TYPE_SELF === $this->triggeringFilePathType) {
197+
$triggeringFilePathType = $this->getPathType($this->triggeringFile);
198+
if (self::PATH_TYPE_SELF === $triggeringFilePathType) {
181199
return self::TYPE_SELF;
182200
}
183-
if (self::PATH_TYPE_UNDETERMINED === $this->triggeringFilePathType) {
201+
if (self::PATH_TYPE_UNDETERMINED === $triggeringFilePathType) {
184202
return self::TYPE_UNDETERMINED;
185203
}
186204
$erroringFile = $erroringPackage = null;

src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
1516
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
1617

1718
class DeprecationTest extends TestCase
@@ -55,6 +56,80 @@ public function testItRulesOutFilesOutsideVendorsAsIndirect()
5556
$this->assertNotSame(Deprecation::TYPE_INDIRECT, $deprecation->getType());
5657
}
5758

59+
/**
60+
* @param bool $muted
61+
* @param string $callingClass
62+
* @param string $message
63+
*
64+
* @dataProvider mutedProvider
65+
*/
66+
public function testItMutesOnlySpecificErrorMessagesWhenTheCallingCodeIsInPhpunit(
67+
$muted,
68+
$callingClass,
69+
$message
70+
) {
71+
$trace = $this->debugBacktrace();
72+
array_unshift($trace, ['class' => $callingClass]);
73+
array_unshift($trace, ['class' => DeprecationErrorHandler::class]);
74+
$deprecation = new Deprecation(
75+
$message,
76+
$trace,
77+
'should_not_matter.php'
78+
);
79+
$this->assertSame($muted, $deprecation->isMuted());
80+
}
81+
82+
public function mutedProvider()
83+
{
84+
yield 'not from phpunit, and not a whitelisted message' => [
85+
false,
86+
\My\Source\Code::class,
87+
'Self deprecating humor is deprecated by itself'
88+
];
89+
yield 'from phpunit, but not a whitelisted message' => [
90+
false,
91+
\PHPUnit\Random\Piece\Of\Code::class,
92+
'Self deprecating humor is deprecated by itself'
93+
];
94+
yield 'whitelisted message, but not from phpunit' => [
95+
false,
96+
\My\Source\Code::class,
97+
'Function ReflectionType::__toString() is deprecated',
98+
];
99+
yield 'from phpunit and whitelisted message' => [
100+
true,
101+
\PHPUnit\Random\Piece\Of\Code::class,
102+
'Function ReflectionType::__toString() is deprecated',
103+
];
104+
}
105+
106+
public function testNotMutedIfNotCalledFromAClassButARandomFile()
107+
{
108+
$deprecation = new Deprecation(
109+
'Function ReflectionType::__toString() is deprecated',
110+
[
111+
['file' => 'should_not_matter.php'],
112+
['file' => 'should_not_matter_either.php'],
113+
],
114+
'my-procedural-controller.php'
115+
);
116+
$this->assertFalse($deprecation->isMuted());
117+
}
118+
119+
public function testItTakesMutesDeprecationFromPhpUnitFiles()
120+
{
121+
$deprecation = new Deprecation(
122+
'Function ReflectionType::__toString() is deprecated',
123+
[
124+
['file' => 'should_not_matter.php'],
125+
['file' => 'should_not_matter_either.php'],
126+
],
127+
'random_path' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'phpunit' . DIRECTORY_SEPARATOR . 'whatever.php'
128+
);
129+
$this->assertTrue($deprecation->isMuted());
130+
}
131+
132+
58133
/**
59134
* This method is here to simulate the extra level from the piece of code
60135
* triggering an error to the error handler

0 commit comments

Comments
 (0)