Skip to content

Commit

Permalink
Use triggering class to generate baseline for deprecation messages fr…
Browse files Browse the repository at this point in the history
…om DebugClassLoader
  • Loading branch information
leongersen committed Oct 25, 2022
1 parent f4d2370 commit eb547fd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Expand Up @@ -76,7 +76,7 @@ jobs:
# To run a PR with a patched phpunit-bridge, first submit the patch for the
# phpunit-bridge as a separate PR against the next feature-branch then
# uncomment and update the following line with that PR number
#SYMFONY_PHPUNIT_BRIDGE_PR=32886
SYMFONY_PHPUNIT_BRIDGE_PR=47252
if [[ $SYMFONY_PHPUNIT_BRIDGE_PR ]]; then
git fetch --depth=2 origin refs/pull/$SYMFONY_PHPUNIT_BRIDGE_PR/head
Expand Down
Expand Up @@ -174,7 +174,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
Expand Up @@ -40,6 +40,7 @@ class Deprecation
private $originClass;
private $originMethod;
private $triggeringFile;
private $triggeringClass;

/** @var string[] Absolute paths to vendor directories */
private static $vendors;
Expand All @@ -59,6 +60,10 @@ class Deprecation
*/
public function __construct($message, array $trace, $file)
{
if (isset($trace[2]['class']) && DebugClassLoader::class === $trace[2]['class']) {
$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 @@ -155,6 +160,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
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 @@ -365,6 +366,43 @@ public function testExistingBaselineAndGeneration()
$this->assertEquals(json_encode($expected_baseline, \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_baseline = [
[
'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_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

public function testBaselineArgumentException()
{
$this->expectException(\InvalidArgumentException::class);
Expand Down

0 comments on commit eb547fd

Please sign in to comment.