10000 [PhpUnitBridge] Use triggering class to generate baseline for deprecation messages from DebugClassLoader by leongersen · Pull Request #47252 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PhpUnitBridge] Use triggering class to generate baseline for deprecation messages from DebugClassLoader #47252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ public function isBaselineDeprecation(Deprecation $deprecation)
return false;
}

if ($deprecation->originatesFromAnObject()) {
if ($deprecation->originatesFromDebugClassLoader()) {
$location = $deprecation->triggeringClass();
} elseif ($deprecation->originatesFromAnObject()) {
$location = $deprecation->originatingClass().'::'.$deprecation->originatingMethod();
} else {
$location = 'procedural code';
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Deprecation
private $originClass;
private $originMethod;
private $triggeringFile;
private $triggeringClass;

/** @var string[] Absolute paths to vendor directories */
private static $vendors;
Expand All @@ -61,6 +62,10 @@ class Deprecation
*/
public function __construct($message, array $trace, $file, $languageDeprecation = false)
{
if (isset($trace[2]['class']) && \in_array($trace[2]['class'], [DebugClassLoader::class, LegacyDebugClassLoader::class], true)) {
$this->triggeringClass = $trace[2]['args'][0];
}

if (isset($trace[2]['function']) && 'trigger_deprecation' === $trace[2]['function']) {
$file = $trace[2]['file'];
array_splice($trace, 1, 1);
Expand Down Expand Up @@ -158,6 +163,26 @@ private function lineShouldBeSkipped(array $line)
return 'ReflectionMethod' === $class || 0 === strpos($class, 'PHPUnit\\');
}

/**
* @return bool
*/
public function originatesFromDebugClassLoader()
{
return isset($this->triggeringClass);
}

/**
* @return string
*/
public function triggeringClass()
{
if (null === $this->triggeringClass) {
throw new \LogicException('Check with originatesFromDebugClassLoader() before calling this method.');
}

return $this->triggeringClass;
}

/**
* @return bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\DeprecationGroup;
use Symfony\Component\ErrorHandler\DebugClassLoader;

class ConfigurationTest extends TestCase
{
Expand Down Expand Up @@ -356,7 +357,7 @@ public function testBaselineGenerationEmptyFile()
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, '')));
$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected_baseline = [
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Test message 1',
Expand All @@ -368,7 +369,7 @@ public function testBaselineGenerationEmptyFile()
'count' => 1,
],
];
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testBaselineGenerationNoFile()
Expand All @@ -383,7 +384,7 @@ public function testBaselineGenerationNoFile()
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, '')));
$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected_baseline = [
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Test message 1',
Expand All @@ -395,7 +396,7 @@ public function testBaselineGenerationNoFile()
'count' => 2,
],
];
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testExistingBaseline()
Expand Down Expand Up @@ -447,7 +448,7 @@ public function testExistingBaselineAndGeneration()
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 3', $trace, '')));
$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected_baseline = [
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Test message 2',
Expand All @@ -459,7 +460,44 @@ public function testExistingBaselineAndGeneration()
'count' => 1,
],
];
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader()
{
$filename = $this->createFile();
$configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile='.urlencode($filename));

$trace = debug_backtrace();
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Regular deprecation', $trace, '')));

$trace[2] = [
'class' => DebugClassLoader::class,
'function' => 'testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader',
'args' => [self::class]
];

$deprecation = new Deprecation('Deprecation by debug class loader', $trace, '');

$this->assertTrue($deprecation->originatesFromDebugClassLoader());

$this->assertTrue($configuration->isBaselineDeprecation($deprecation));

$configuration->writeBaseline();
$this->assertEquals($filename, $configuration->getBaselineFile());
$expected = [
[
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
'message' => 'Regular deprecation',
'count' => 1,
],
[
'location' => self::class,
'message' => 'Deprecation by debug class loader',
'count' => 1,
],
];
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testBaselineArgumentException()
Expand Down
0