Skip to content

[PhpUnitBridge] Add log file option for deprecations #39098

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
Jan 5, 2021
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
1 change: 1 addition & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* bumped the minimum PHP version to 7.1.3
* bumped the minimum PHPUnit version to 7.5
* deprecated the `SetUpTearDownTrait` trait, use original methods with "void" return typehint.
* added `logFile` option to write deprecations to a file instead of echoing them

5.1.0
-----
Expand Down
34 changes: 23 additions & 11 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,23 +285,35 @@ private static function colorize($str, $red)
* @param string[] $groups
* @param Configuration $configuration
* @param bool $isFailing
*
* @throws \InvalidArgumentException
*/
private function displayDeprecations($groups, $configuration, $isFailing)
{
$cmp = function ($a, $b) {
return $b->count() - $a->count();
};

if ($configuration->shouldWriteToLogFile()) {
if (false === $handle = @fopen($file = $configuration->getLogFile(), 'a')) {
throw new \InvalidArgumentException(sprintf('The configured log file "%s" is not writeable.', $file));
}
} else {
$handle = fopen('php://output', 'w');
}

foreach ($groups as $group) {
if ($this->deprecationGroups[$group]->count()) {
echo "\n", self::colorize(
sprintf(
'%s deprecation notices (%d)',
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
$this->deprecationGroups[$group]->count()
),
'legacy' !== $group && 'indirect' !== $group
), "\n";
$deprecationGroupMessage = sprintf(
'%s deprecation notices (%d)',
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
$this->deprecationGroups[$group]->count()
);
if ($configuration->shouldWriteToLogFile()) {
fwrite($handle, "\n$deprecationGroupMessage\n");
} else {
fwrite($handle, "\n".self::colorize($deprecationGroupMessage, 'legacy' !== $group && 'indirect' !== $group)."\n");
}

if ('legacy' !== $group && !$configuration->verboseOutput($group) && !$isFailing) {
continue;
Expand All @@ -310,22 +322,22 @@ private function displayDeprecations($groups, $configuration, $isFailing)
uasort($notices, $cmp);

foreach ($notices as $msg => $notice) {
echo "\n ", $notice->count(), 'x: ', $msg, "\n";
fwrite($handle, sprintf("\n %sx: %s\n", $notice->count(), $msg));

$countsByCaller = $notice->getCountsByCaller();
arsort($countsByCaller);

foreach ($countsByCaller as $method => $count) {
if ('count' !== $method) {
echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
fwrite($handle, sprintf(" %dx in %s\n", $count, preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method)));
}
}
}
}
}

if (!empty($notices)) {
echo "\n";
fwrite($handle, "\n");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ class Configuration
private $baselineDeprecations = [];

/**
* @param int[] $thresholds A hash associating groups to thresholds
* @param string $regex Will be matched against messages, to decide whether to display a stack trace
* @param bool[] $verboseOutput Keyed by groups
* @param bool $generateBaseline Whether to generate or update the baseline file
* @param string $baselineFile The path to the baseline file
* @var string|null
*/
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = [], $generateBaseline = false, $baselineFile = '')
private $logFile = null;

/**
* @param int[] $thresholds A hash associating groups to thresholds
* @param string $regex Will be matched against messages, to decide whether to display a stack trace
* @param bool[] $verboseOutput Keyed by groups
* @param bool $generateBaseline Whether to generate or update the baseline file
* @param string $baselineFile The path to the baseline file
* @param string|null $logFile The path to the log file
*/
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = [], $generateBaseline = false, $baselineFile = '', $logFile = null)
{
$groups = ['total', 'indirect', 'direct', 'self'];

Expand Down Expand Up @@ -119,6 +125,8 @@ private function __construct(array $thresholds = [], $regex = '', $verboseOutput
throw new \InvalidArgumentException(sprintf('The baselineFile "%s" does not exist.', $this->baselineFile));
}
}

$this->logFile = $logFile;
}

/**
Expand Down Expand Up @@ -238,6 +246,16 @@ public function verboseOutput($group)
return $this->verboseOutput[$group];
}

public function shouldWriteToLogFile()
{
return null !== $this->logFile;
}

public function getLogFile()
{
return $this->logFile;
}

/**
* @param string $serializedConfiguration an encoded string, for instance
* max[total]=1234&max[indirect]=42
Expand All @@ -248,7 +266,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
{
parse_str($serializedConfiguration, $normalizedConfiguration);
foreach (array_keys($normalizedConfiguration) as $key) {
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile'], true)) {
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile', 'logFile'], true)) {
throw new \InvalidArgumentException(sprintf('Unknown configuration option "%s".', $key));
}
}
Expand All @@ -260,6 +278,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
'quiet' => [],
'generateBaseline' => false,
'baselineFile' => '',
'logFile' => null,
];

if ('' === $normalizedConfiguration['disabled'] || filter_var($normalizedConfiguration['disabled'], \FILTER_VALIDATE_BOOLEAN)) {
Expand All @@ -282,7 +301,8 @@ public static function fromUrlEncodedString($serializedConfiguration)
'',
$verboseOutput,
filter_var($normalizedConfiguration['generateBaseline'], \FILTER_VALIDATE_BOOLEAN),
$normalizedConfiguration['baselineFile']
$normalizedConfiguration['baselineFile'],
$normalizedConfiguration['logFile']
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
Test DeprecationErrorHandler with log file
--FILE--
<?php
$filename = tempnam(sys_get_temp_dir(), 'sf-').uniqid();
$k = 'SYMFONY_DEPRECATIONS_HELPER';
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'logFile='.$filename);
putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
require_once __DIR__.'/../../bootstrap.php';

class FooTestCase
{
public function testLegacyFoo()
{
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
}

public function testLegacyBar()
{
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
}
}

@trigger_error('root deprecation', E_USER_DEPRECATED);

$foo = new FooTestCase();
$foo->testLegacyFoo();
$foo->testLegacyBar();

register_shutdown_function(function () use ($filename) {
var_dump(file_get_contents($filename));
});
?>
--EXPECTF--
string(234) "
Unsilenced deprecation notices (3)

2x: unsilenced foo deprecation
2x in FooTestCase::testLegacyFoo

1x: unsilenced bar deprecation
1x in FooTestCase::testLegacyBar

Other deprecation notices (1)

1x: root deprecation

"
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy