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

Skip to content

Commit eb547fd

Browse files
committed
Use triggering class to generate baseline for deprecation messages from DebugClassLoader
1 parent f4d2370 commit eb547fd

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

.github/workflows/unit-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
# To run a PR with a patched phpunit-bridge, first submit the patch for the
7777
# phpunit-bridge as a separate PR against the next feature-branch then
7878
# uncomment and update the following line with that PR number
79-
#SYMFONY_PHPUNIT_BRIDGE_PR=32886
79+
SYMFONY_PHPUNIT_BRIDGE_PR=47252
8080
8181
if [[ $SYMFONY_PHPUNIT_BRIDGE_PR ]]; then
8282
git fetch --depth=2 origin refs/pull/$SYMFONY_PHPUNIT_BRIDGE_PR/head

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ public function isBaselineDeprecation(Deprecation $deprecation)
174174
return false;
175175
}
176176

177-
if ($deprecation->originatesFromAnObject()) {
177+
if ($deprecation->originatesFromDebugClassLoader()) {
178+
$location = $deprecation->triggeringClass();
179+
} elseif ($deprecation->originatesFromAnObject()) {
178180
$location = $deprecation->originatingClass().'::'.$deprecation->originatingMethod();
179181
} else {
180182
$location = 'procedural code';

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Deprecation
4040
private $originClass;
4141
private $originMethod;
4242
private $triggeringFile;
43+
private $triggeringClass;
4344

4445
/** @var string[] Absolute paths to vendor directories */
4546
private static $vendors;
@@ -59,6 +60,10 @@ class Deprecation
5960
*/
6061
public function __construct($message, array $trace, $file)
6162
{
63+
if (isset($trace[2]['class']) && DebugClassLoader::class === $trace[2]['class']) {
64+
$this->triggeringClass = $trace[2]['args'][0];
65+
}
66+
6267
if (isset($trace[2]['function']) && 'trigger_deprecation' === $trace[2]['function']) {
6368
$file = $trace[2]['file'];
6469
array_splice($trace, 1, 1);
@@ -155,6 +160,26 @@ private function lineShouldBeSkipped(array $line)
155160
return 'ReflectionMethod' === $class || 0 === strpos($class, 'PHPUnit\\');
156161
}
157162

163+
/**
164+
* @return bool
165+
*/
166+
public function originatesFromDebugClassLoader()
167+
{
168+
return isset($this->triggeringClass);
169+
}
170+
171+
/**
172+
* @return string
173+
*/
174+
public function triggeringClass()
175+
{
176+
if (null === $this->triggeringClass) {
177+
throw new \LogicException('Check with originatesFromDebugClassLoader() before calling this method.');
178+
}
179+
180+
return $this->triggeringClass;
181+
}
182+
158183
/**
159184
* @return bool
160185
*/

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
1616
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
1717
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\DeprecationGroup;
18+
use Symfony\Component\ErrorHandler\DebugClassLoader;
1819

1920
class ConfigurationTest extends TestCase
2021
{
@@ -365,6 +366,43 @@ public function testExistingBaselineAndGeneration()
365366
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
366367
}
367368

369+
public function testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader()
370+
{
371+
$filename = $this->createFile();
372+
$configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile='.urlencode($filename));
373+
374+
$trace = debug_backtrace();
375+
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Regular deprecation', $trace, '')));
376+
377+
$trace[2] = [
378+
'class' => DebugClassLoader::class,
379+
'function' => 'testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader',
380+
'args' => [self::class]
381+
];
382+
383+
$deprecation = new Deprecation('Deprecation by debug class loader', $trace, '');
384+
385+
$this->assertTrue($deprecation->originatesFromDebugClassLoader());
386+
387+
$this->assertTrue($configuration->isBaselineDeprecation($deprecation));
388+
389+
$configuration->writeBaseline();
390+
$this->assertEquals($filename, $configuration->getBaselineFile());
391+
$expected_baseline = [
392+
[
393+
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
394+
'message' => 'Regular deprecation',
395+
'count' => 1,
396+
],
397+
[
398+
'location' => self::class,
399+
'message' => 'Deprecation by debug class loader',
400+
'count' => 1,
401+
],
402+
];
403+
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
404+
}
405+
368406
public function testBaselineArgumentException()
369407
{
370408
$this->expectException(\InvalidArgumentException::class);

0 commit comments

Comments
 (0)