Skip to content

[PhpunitBridge][HttpKernel]Always display container deprecations in simple phpunit #29519

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

Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function handleError($type, $msg, $file, $line, $context = [])
if ($deprecation->originatesFromAnObject()) {
$class = $deprecation->originatingClass();
$method = $deprecation->originatingMethod();
$msg = $deprecation->getMessage();

if (0 !== error_reporting()) {
$group = 'unsilenced';
Expand Down
39 changes: 36 additions & 3 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bridge\PhpUnit\DeprecationErrorHandler;

use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* @internal
Expand Down Expand Up @@ -43,6 +44,11 @@ class Deprecation
*/
private $self;

/**
* @var bool
*/
private $isContainerDeprecation;

/** @var string[] absolute paths to vendor directories */
private static $vendors;

Expand All @@ -60,18 +66,23 @@ public function __construct($message, array $trace, $file)
}
$line = $trace[$i];
$this->self = !$this->pathOriginatesFromVendor($file);
$this->isContainerDeprecation = false;
if (isset($line['object']) || isset($line['class'])) {
if (isset($line['class']) && 0 === strpos($line['class'], SymfonyTestsListenerFor::class)) {
if (
isset($line['class'])
&& ($this->isFromTestListeners($line) || ($fromKernelTestCase = $this->isFromKernelTestCase($line)))
) {
$parsedMsg = unserialize($this->message);
$this->isContainerDeprecation = isset($fromKernelTestCase) && $fromKernelTestCase && $parsedMsg;
$this->message = $parsedMsg['deprecation'];
$this->originClass = $parsedMsg['class'];
$this->originMethod = $parsedMsg['method'];
// If the deprecation has been triggered via
// \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest()
// then we need to use the serialized information to determine
// if the error has been triggered from vendor code.
$this->self = isset($parsedMsg['triggering_file'])
&& $this->pathOriginatesFromVendor($parsedMsg['triggering_file']);
$this->self = !isset($parsedMsg['triggering_file'])
|| !$this->pathOriginatesFromVendor($parsedMsg['triggering_file']);

return;
}
Expand All @@ -80,6 +91,18 @@ public function __construct($message, array $trace, $file)
}
}

private function isFromTestListeners(array $line): bool
{
return 0 === strpos($line['class'], SymfonyTestsListenerFor::class);
}

private function isFromKernelTestCase(array $line): bool
{
return 0 === strpos($line['class'], KernelTestCase::class)
&& isset($line['function'])
&& 'tearDownAfterClass' == $line['function'];
}

/**
* @return bool
*/
Expand Down Expand Up @@ -133,6 +156,11 @@ public function originatingMethod()
return $this->originMethod;
}

public function getMessage(): string
{
return $this->message;
}

/**
* @param string $utilPrefix
*
Expand Down Expand Up @@ -188,6 +216,11 @@ public function isIndirect()
return false;
}

public function isContainerDeprecation(): bool
{
return $this->isContainerDeprecation;
}

/**
* pathOriginatesFromVendor() should always be called prior to calling this method.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ abstract class KernelTestCase extends TestCase
*/
protected static $container;

private static $compilerDeprecationsTriggered = false;

protected function doTearDown(): void
{
static::ensureKernelShutdown();
Expand Down Expand Up @@ -132,4 +134,18 @@ protected static function ensureKernelShutdown()
}
static::$container = null;
}

public static function tearDownAfterClass()
{
if (!self::$compilerDeprecationsTriggered) {
$compilerDeprecated = getenv('SYMFONY_COMPILER_DEPRECATIONS');
if ($compilerDeprecated && file_exists($compilerDeprecated)) {
foreach (unserialize(file_get_contents($compilerDeprecated)) as $log) {
@trigger_error(serialize($log), E_USER_DEPRECATED);
}
self::$compilerDeprecationsTriggered = true;
}
}
parent::tearDownAfterClass();
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Marked `Response` final.
* Deprecated `Response::buildHeader()`
* Deprecated `Response::getStatus()`, use `Response::getStatusCode()` instead
* Allowed to always display container deprecations

4.2.0
-----
Expand Down
51 changes: 36 additions & 15 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ protected function initializeContainer()
$cacheDir = $this->warmupDir ?: $this->getCacheDir();
$cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug);
$oldContainer = null;
$deprecationsFilename = $cacheDir.'/'.$class.'Deprecations.log';
if ($fresh = $cache->isFresh()) {
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
$errorLevel = error_reporting(\E_ALL ^ \E_WARNING);
Expand All @@ -501,13 +502,16 @@ protected function initializeContainer()
}

if ($fresh) {
if ($this->debug) {
putenv("SYMFONY_COMPILER_DEPRECATIONS=$deprecationsFilename");
}

return;
}

if ($this->debug) {
$collectedLogs = [];
$previousHandler = \defined('PHPUNIT_COMPOSER_INSTALL');
$previousHandler = $previousHandler ?: set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
$previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
return $previousHandler ? $previousHandler($type, $message, $file, $line) : false;
}
Expand All @@ -518,27 +522,44 @@ protected function initializeContainer()
return;
}

$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
// Clean the trace by removing first frames added by the error handler itself.
for ($i = 0; isset($backtrace[$i]); ++$i) {
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
$backtrace = \array_slice($backtrace, 1 + $i);
break;
if (\defined('PHPUNIT_COMPOSER_INSTALL')) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$i = \count($backtrace);
while (1 < $i && (!isset($backtrace[--$i]['class']) || ('ReflectionMethod' === $backtrace[$i]['class'] || 0 === strpos($backtrace[$i]['class'], 'PHPUnit_') || 0 === strpos($backtrace[$i]['class'], 'PHPUnit\\')))) {
// No-op
}
}
// Remove frames added by DebugClassLoader.
for ($i = \count($backtrace) - 2; 0 < $i; --$i) {
if (DebugClassLoader::class === ($backtrace[$i]['class'] ?? null)) {
$backtrace = [$backtrace[$i + 1]];
break;
$backtrace = \array_slice($backtrace, $i);
} else {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
// Clean the trace by removing first frames added by the error handler itself.
for ($i = 0; isset($backtrace[$i]); ++$i) {
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
$backtrace = \array_slice($backtrace, 1 + $i);
break;
}
}

// Remove frames added by DebugClassLoader.
for ($i = \count($backtrace) - 2; 0 < $i; --$i) {
if (DebugClassLoader::class === ($backtrace[$i]['class'] ?? null)) {
$backtrace = [$backtrace[$i + 1]];
break;
}
}
}

$class = $backtrace[0]['class'];
$function = $backtrace[0]['function'];

$collectedLogs[$message] = [
'type' => $type,
'message' => $message,
'deprecation' => $message,
'file' => $file,
'triggering_file' => $file,
'line' => $line,
'class' => $class,
'method' => $function,
'trace' => [$backtrace[0]],
'count' => 1,
];
Expand All @@ -553,7 +574,7 @@ protected function initializeContainer()
if ($this->debug && true !== $previousHandler) {
restore_error_handler();

file_put_contents($cacheDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs)));
file_put_contents($deprecationsFilename, serialize(array_values($collectedLogs)));
file_put_contents($cacheDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
}
}
Expand Down