Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1d3db3d

Browse files
Merge branch '4.3' into 4.4
* 4.3: Mute deprecations triggered from phpunit
2 parents e80d405 + 4b15acf commit 1d3db3d

File tree

4 files changed

+92
-13
lines changed

4 files changed

+92
-13
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: 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;

src/Symfony/Bridge/PhpUnit/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

src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public function testRegister()
7070

7171
public function testErrorGetLast()
7272
{
73-
$handler = ErrorHandler::register();
7473
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
74+
$handler = ErrorHandler::register();
7575
$handler->setDefaultLogger($logger);
7676
$handler->screamAt(E_ALL);
7777

@@ -143,9 +143,8 @@ public function testConstruct()
143143
public function testDefaultLogger()
144144
{
145145
try {
146-
$handler = ErrorHandler::register();
147-
148146
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
147+
$handler = ErrorHandler::register();
149148

150149
$handler->setDefaultLogger($logger, E_NOTICE);
151150
$handler->setDefaultLogger($logger, [E_USER_NOTICE => LogLevel::CRITICAL]);
@@ -331,12 +330,11 @@ public function testHandleDeprecation()
331330
public function testHandleException()
332331
{
333332
try {
333+
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
334334
$handler = ErrorHandler::register();
335335

336336
$exception = new \Exception('foo');
337337

338-
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
339-
340338
$logArgCheck = function ($level, $message, $context) {
341339
$this->assertSame('Uncaught Exception: foo', $message);
342340
$this->assertArrayHasKey('exception', $context);
@@ -442,6 +440,7 @@ public function testSettingLoggerWhenExceptionIsBuffered()
442440
public function testHandleFatalError()
443441
{
444442
try {
443+
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
445444
$handler = ErrorHandler::register();
446445

447446
$error = [
@@ -451,8 +450,6 @@ public function testHandleFatalError()
451450
'line' => 123,
452451
];
453452

454-
$logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
455-
456453
$logArgCheck = function ($level, $message, $context) {
457454
$this->assertEquals('Fatal Parse Error: foo', $message);
458455
$this->assertArrayHasKey('exception', $context);

0 commit comments

Comments
 (0)