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

Skip to content

Commit e11b3a3

Browse files
bug #47252 [PhpUnitBridge] Use triggering class to generate baseline for deprecation messages from DebugClassLoader (leongersen)
This PR was merged into the 5.4 branch. Discussion ---------- [PhpUnitBridge] Use triggering class to generate baseline for deprecation messages from DebugClassLoader | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #45943 | License | MIT | Doc PR | N/A Fixes #45943 by using the triggering class to generate the baseline for messages from `DebugClassLoader`. This would break currently generated baselines, but as #45943 describes, those are broken already when running tests in another order, or when running a sub-/superset of tests. I'll finish this PR by adding/updating tests if this approach is acceptable. Commits ------- 3987757 Use triggering class to generate baseline for deprecation messages from DebugClassLoader
2 parents 17f4fe4 + 3987757 commit e11b3a3

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ public function isBaselineDeprecation(Deprecation $deprecation)
200200
return false;
201201
}
202202

203-
if ($deprecation->originatesFromAnObject()) {
203+
if ($deprecation->originatesFromDebugClassLoader()) {
204+
$location = $deprecation->triggeringClass();
205+
} elseif ($deprecation->originatesFromAnObject()) {
204206
$location = $deprecation->originatingClass().'::'.$deprecation->originatingMethod();
205207
} else {
206208
$location = 'procedural code';

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Deprecation
4141
private $originClass;
4242
private $originMethod;
4343
private $triggeringFile;
44+
private $triggeringClass;
4445

4546
/** @var string[] Absolute paths to vendor directories */
4647
private static $vendors;
@@ -61,6 +62,10 @@ class Deprecation
6162
*/
6263
public function __construct($message, array $trace, $file, $languageDeprecation = false)
6364
{
65+
if (isset($trace[2]['class']) && \in_array($trace[2]['class'], [DebugClassLoader::class, LegacyDebugClassLoader::class], true)) {
66+
$this->triggeringClass = $trace[2]['args'][0];
67+
}
68+
6469
if (isset($trace[2]['function']) && 'trigger_deprecation' === $trace[2]['function']) {
6570
$file = $trace[2]['file'];
6671
array_splice($trace, 1, 1);
@@ -158,6 +163,26 @@ private function lineShouldBeSkipped(array $line)
158163
return 'ReflectionMethod' === $class || 0 === strpos($class, 'PHPUnit\\');
159164
}
160165

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

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

Lines changed: 44 additions & 6 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
{
@@ -356,7 +357,7 @@ public function testBaselineGenerationEmptyFile()
356357
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, '')));
357358
$configuration->writeBaseline();
358359
$this->assertEquals($filename, $configuration->getBaselineFile());
359-
$expected_baseline = [
360+
$expected = [
360361
[
361362
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
362363
'message' => 'Test message 1',
@@ -368,7 +369,7 @@ public function testBaselineGenerationEmptyFile()
368369
'count' => 1,
369370
],
370371
];
371-
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
372+
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
372373
}
373374

374375
public function testBaselineGenerationNoFile()
@@ -383,7 +384,7 @@ public function testBaselineGenerationNoFile()
383384
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, '')));
384385
$configuration->writeBaseline();
385386
$this->assertEquals($filename, $configuration->getBaselineFile());
386-
$expected_baseline = [
387+
$expected = [
387388
[
388389
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
389390
'message' => 'Test message 1',
@@ -395,7 +396,7 @@ public function testBaselineGenerationNoFile()
395396
'count' => 2,
396397
],
397398
];
398-
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
399+
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
399400
}
400401

401402
public function testExistingBaseline()
@@ -447,7 +448,7 @@ public function testExistingBaselineAndGeneration()
447448
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 3', $trace, '')));
448449
$configuration->writeBaseline();
449450
$this->assertEquals($filename, $configuration->getBaselineFile());
450-
$expected_baseline = [
451+
$expected = [
451452
[
452453
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
453454
'message' => 'Test message 2',
@@ -459,7 +460,44 @@ public function testExistingBaselineAndGeneration()
459460
'count' => 1,
460461
],
461462
];
462-
$this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
463+
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
464+
}
465+
466+
public function testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader()
467+
{
468+
$filename = $this->createFile();
469+
$configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile='.urlencode($filename));
470+
471+
$trace = debug_backtrace();
472+
$this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Regular deprecation', $trace, '')));
473+
474+
$trace[2] = [
475+
'class' => DebugClassLoader::class,
476+
'function' => 'testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader',
477+
'args' => [self::class]
478+
];
479+
480+
$deprecation = new Deprecation('Deprecation by debug class loader', $trace, '');
481+
482+
$this->assertTrue($deprecation->originatesFromDebugClassLoader());
483+
484+
$this->assertTrue($configuration->isBaselineDeprecation($deprecation));
485+
486+
$configuration->writeBaseline();
487+
$this->assertEquals($filename, $configuration->getBaselineFile());
488+
$expected = [
489+
[
490+
'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest',
491+
'message' => 'Regular deprecation',
492+
'count' => 1,
493+
],
494+
[
495+
'location' => self::class,
496+
'message' => 'Deprecation by debug class loader',
497+
'count' => 1,
498+
],
499+
];
500+
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
463501
}
464502

465503
public function testBaselineArgumentException()

0 commit comments

Comments
 (0)