Skip to content

Commit b9ddb6f

Browse files
Merge branch '4.4'
* 4.4: [Lock] feature: lock split interface fix post-merge review Mute deprecations triggered from phpunit
2 parents a5d3bc8 + d27c566 commit b9ddb6f

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

DeprecationErrorHandler.php

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

125125
$deprecation = new Deprecation($msg, debug_backtrace(), $file);
126+
if ($deprecation->isMuted()) {
127+
return;
128+
}
126129
$group = 'other';
127130

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

DeprecationErrorHandler/Deprecation.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class Deprecation
4848
private $originMethod;
4949

5050
/**
51-
* @var string one of the PATH_TYPE_* constants
51+
* @var string
5252
*/
53-
private $triggeringFilePathType;
53+
private $triggeringFile;
5454

5555
/** @var string[] absolute paths to vendor directories */
5656
private static $vendors;
@@ -76,7 +76,7 @@ public function __construct($message, array $trace, $file)
7676
// No-op
7777
}
7878
$line = $trace[$i];
79-
$this->triggeringFilePathType = $this->getPathType($file);
79+
$this->triggeringFile = $file;
8080
if (isset($line['object']) || isset($line['class'])) {
8181
if (isset($line['class']) && 0 === strpos($line['class'], SymfonyTestsListenerFor::class)) {
8282
$parsedMsg = unserialize($this->message);
@@ -88,7 +88,7 @@ public function __construct($message, array $trace, $file)
8888
// then we need to use the serialized information to determine
8989
// if the error has been triggered from vendor code.
9090
if (isset($parsedMsg['triggering_file'])) {
91-
$this->triggeringFilePathType = $this->getPathType($parsedMsg['triggering_file']);
91+
$this->triggeringFile = $parsedMsg['triggering_file'];
9292
}
9393

9494
return;
@@ -169,6 +169,21 @@ public function isLegacy($utilPrefix)
169169
|| \in_array('legacy', $test::getGroups($class, $method), true);
170170
}
171171

172+
/**
173+
* @return bool
174+
*/
175+
public function isMuted()
176+
{
177+
if ('Function ReflectionType::__toString() is deprecated' !== $this->message) {
178+
return false;
179+
}
180+
if (isset($this->trace[1]['class'])) {
181+
return 0 === strpos($this->trace[1]['class'], 'PHPUnit\\');
182+
}
183+
184+
return false !== strpos($this->triggeringFile, \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR.'phpunit'.\DIRECTORY_SEPARATOR);
185+
}
186+
172187
/**
173188
* Tells whether both the calling package and the called package are vendor
174189
* packages.
@@ -177,10 +192,11 @@ public function isLegacy($utilPrefix)
177192
*/
178193
public function getType()
179194
{
180-
if (self::PATH_TYPE_SELF === $this->triggeringFilePathType) {
195+
$triggeringFilePathType = $this->getPathType($this->triggeringFile);
196+
if (self::PATH_TYPE_SELF === $triggeringFilePathType) {
181197
return self::TYPE_SELF;
182198
}
183-
if (self::PATH_TYPE_UNDETERMINED === $this->triggeringFilePathType) {
199+
if (self::PATH_TYPE_UNDETERMINED === $triggeringFilePathType) {
184200
return self::TYPE_UNDETERMINED;
185201
}
186202
$erroringFile = $erroringPackage = null;

Tests/DeprecationErrorHandler/DeprecationTest.php

Lines changed: 63 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,68 @@ public function testItRulesOutFilesOutsideVendorsAsIndirect()
5556
$this->assertNotSame(Deprecation::TYPE_INDIRECT, $deprecation->getType());
5657
}
5758

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

0 commit comments

Comments
 (0)