Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 923759e

Browse filesBrowse files
l-vogreg0ire
authored andcommitted
[PhpUnitBridge] Use serialized trace when using serialized deprecation
1 parent 38fb0a8 commit 923759e
Copy full SHA for 923759e

File tree

3 files changed

+129
-9
lines changed
Filter options

3 files changed

+129
-9
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
+13-1Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,19 @@ public static function collectDeprecations($outputFile)
106106
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
107107
}
108108

109-
$deprecations[] = [error_reporting(), $msg, $file];
109+
$trace = debug_backtrace();
110+
$filesStack = [];
111+
foreach ($trace as $line) {
112+
if (\in_array($line['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
113+
continue;
114+
}
115+
116+
if (isset($line['file'])) {
117+
$filesStack[] = $line['file'];
118+
}
119+
}
120+
121+
$deprecations[] = [error_reporting(), $msg, $file, $filesStack];
110122

111123
return null;
112124
});

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php
+23-8Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Deprecation
4343
*/
4444
private static $internalPaths = [];
4545

46+
private $originalFilesStack;
47+
4648
/**
4749
* @param string $message
4850
* @param string $file
@@ -63,6 +65,7 @@ public function __construct($message, array $trace, $file)
6365
$this->message = $parsedMsg['deprecation'];
6466
$this->originClass = $parsedMsg['class'];
6567
$this->originMethod = $parsedMsg['method'];
68+
$this->originalFilesStack = $parsedMsg['files_stack'];
6669
// If the deprecation has been triggered via
6770
// \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest()
6871
// then we need to use the serialized information to determine
@@ -164,6 +167,24 @@ public function isMuted()
164167
return false !== strpos($this->triggeringFile, \DIRECTORY_SEPARATOR.'vendor'.\DIRECTORY_SEPARATOR.'phpunit'.\DIRECTORY_SEPARATOR);
165168
}
166169

170+
private function getOriginalFilesStack(): array
171+
{
172+
if (null === $this->originalFilesStack) {
173+
$this->originalFilesStack = [];
174+
foreach ($this->trace as $line) {
175+
if (\in_array($line['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
176+
continue;
177+
}
178+
if (!isset($line['file'])) {
179+
continue;
180+
}
181+
$this->originalFilesStack[] = $line['file'];
182+
}
183+
}
184+
185+
return $this->originalFilesStack;
186+
}
187+
167188
/**
168189
* Tells whether both the calling package and the called package are vendor
169190
* packages.
@@ -180,14 +201,8 @@ public function getType()
180201
return self::TYPE_UNDETERMINED;
181202
}
182203
$erroringFile = $erroringPackage = null;
183-
foreach ($this->trace as $line) {
184-
if (\in_array($line['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
185-
continue;
186-
}
187-
if (!isset($line['file'])) {
188-
continue;
189-
}
190-
$file = $line['file'];
204+
205+
foreach ($this->getOriginalFilesStack() as $file) {
191206
if ('-' === $file || 'Standard input code' === $file || !realpath($file)) {
192207
continue;
193208
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/DeprecationTest.php
+93Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,32 @@
1111

1212
namespace Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler;
1313

14+
use Composer\Autoload\ClassLoader;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
1617
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
1718
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerForV5;
1819

1920
class DeprecationTest extends TestCase
2021
{
22+
public static function setUpBeforeClass(): void
23+
{
24+
$vendorDir = self::getVendorDir();
25+
26+
mkdir($vendorDir.'/myfakevendor/myfakepackage1', 0777, true);
27+
mkdir($vendorDir.'/myfakevendor/myfakepackage2');
28+
touch($vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php');
29+
touch($vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile2.php');
30+
touch($vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php');
31+
}
32+
33+
private static function getVendorDir(): string
34+
{
35+
$reflection = new \ReflectionClass(ClassLoader::class);
36+
37+
return \dirname($reflection->getFileName(), 2);
38+
}
39+
2140
public function testItCanDetermineTheClassWhereTheDeprecationHappened()
2241
{
2342
$deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
@@ -170,6 +189,62 @@ public function testIsSelf(bool $expectedIsSelf, string $message, string $traceC
170189
$this->assertEquals($expectedIsSelf, $deprecation->getType() === Deprecation::TYPE_SELF);
171190
}
172191

192+
public function providerIsIndirectUsesRightTrace(): array
193+
{
194+
$vendorDir = self::getVendorDir();
195+
196+
return [
197+
'no_file_in_stack' => [false, '', [['function' => 'myfunc1'], ['function' => 'myfunc2']]],
198+
'files_in_stack_from_various_packages' => [
199+
true,
200+
'',
201+
[
202+
['function' => 'myfunc1', 'file' => $vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php'],
203+
['function' => 'myfunc2', 'file' => $vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php'],
204+
],
205+
],
206+
'serialized_stack_files_from_same_package' => [
207+
false,
208+
serialize([
209+
'deprecation' => 'My deprecation message',
210+
'class' => 'MyClass',
211+
'method' => 'myMethod',
212+
'files_stack' => [
213+
$vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php',
214+
$vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile2.php',
215+
],
216+
]),
217+
[['function' => 'myfunc1'], ['class' => SymfonyTestsListenerForV5::class, 'method' => 'mymethod']],
218+
],
219+
'serialized_stack_files_from_various_packages' => [
220+
true,
221+
serialize([
222+
'deprecation' => 'My deprecation message',
223+
'class' => 'MyClass',
224+
'method' => 'myMethod',
225+
'files_stack' => [
226+
$vendorDir.'/myfakevendor/myfakepackage1/MyFakeFile1.php',
227+
$vendorDir.'/myfakevendor/myfakepackage2/MyFakeFile.php',
228+
],
229+
]),
230+
[['function' => 'myfunc1'], ['class' => SymfonyTestsListenerForV5::class, 'method' => 'mymethod']],
231+
],
232+
];
233+
}
234+
235+
/**
236+
* @dataProvider providerIsIndirectUsesRightTrace
237+
*/
238+
public function testIsIndirectUsesRightTrace(bool $expectedIsIndirect, string $message, array $trace): void
239+
{
240+
$deprecation = new Deprecation(
241+
$message,
242+
$trace,
243+
self::getVendorDir().'/myfakevendor/myfakepackage2/MyFakeFile.php'
244+
);
245+
$this->assertEquals($expectedIsIndirect, $deprecation->getType() === Deprecation::TYPE_INDIRECT);
246+
}
247+
173248
/**
174249
* This method is here to simulate the extra level from the piece of code
175250
* triggering an error to the error handler.
@@ -178,4 +253,22 @@ public function debugBacktrace(): array
178253
{
179254
return debug_backtrace();
180255
}
256+
257+
private static function removeDir($dir): void
258+
{
259+
$files = glob($dir.'/*');
260+
foreach ($files as $file) {
261+
if (is_file($file)) {
262+
unlink($file);
263+
} else {
264+
self::removeDir($file);
265+
}
266+
}
267+
rmdir($dir);
268+
}
269+
270+
public static function tearDownAfterClass(): void
271+
{
272+
self::removeDir(self::getVendorDir().'/myfakevendor');
273+
}
181274
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.