10000 feature #39098 [PhpUnitBridge] Add log file option for deprecations (… · symfony/symfony@80bde1e · GitHub
[go: up one dir, main page]

Skip to content

Commit 80bde1e

Browse files
feature #39098 [PhpUnitBridge] Add log file option for deprecations (michaelKaefer)
This PR was merged into the 5.3-dev branch. Discussion ---------- [PhpUnitBridge] Add log file option for deprecations | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #37679 | License | MIT | Doc PR | symfony/symfony-docs#14558 Commits ------- 3ed22be [PhpUnitBridge] Add log file option for deprecations
2 parents c97d850 + 3ed22be commit 80bde1e

File tree

4 files changed

+110
-19
lines changed

4 files changed

+110
-19
lines changed

src/Symfony/Bridge/PhpUnit/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* bumped the minimum PHP version to 7.1.3
88
* bumped the minimum PHPUnit version to 7.5
99
* deprecated the `SetUpTearDownTrait` trait, use original methods with "void" return typehint.
10+
* added `logFile` option to write deprecations to a file instead of echoing them
1011

1112
5.1.0
1213
-----

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,23 +285,35 @@ private static function colorize($str, $red)
285285
* @param string[] $groups
286286
* @param Configuration $configuration
287287
* @param bool $isFailing
288+
*
289+
* @throws \InvalidArgumentException
288290
*/
289291
private function displayDeprecations($groups, $configuration, $isFailing)
290292
{
291293
$cmp = function ($a, $b) {
292294
return $b->count() - $a->count();
293295
};
294296

297+
if ($configuration->shouldWriteToLogFile()) {
298+
if (false === $handle = @fopen($file = $configuration->getLogFile(), 'a')) {
299+
throw new \InvalidArgumentException(sprintf('The configured log file "%s" is not writeable.', $file));
300+
}
301+
} else {
302+
$handle = fopen('php://output', 'w');
303+
}
304+
295305
foreach ($groups as $group) {
296306
if ($this->deprecationGroups[$group]->count()) {
297-
echo "\n", self::colorize(
298-
sprintf(
299-
'%s deprecation notices (%d)',
300-
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
301-
$this->deprecationGroups[$group]->count()
302-
),
303-
'legacy' !== $group && 'indirect' !== $group
304-
), "\n";
307+
$deprecationGroupMessage = sprintf(
308+
'%s deprecation notices (%d)',
309+
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
310+
$this->deprecationGroups[$group]->count()
311+
);
312+
if ($configuration->shouldWriteToLogFile()) {
313+
fwrite($handle, "\n$deprecationGroupMessage\n");
314+
} else {
315+
fwrite($handle, "\n".self::colorize($deprecationGroupMessage, 'legacy' !== $group && 'indirect' !== $group)."\n");
316+
}
305317

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

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

315327
$countsByCaller = $notice->getCountsByCaller();
316328
arsort($countsByCaller);
317329

318330
foreach ($countsByCaller as $method => $count) {
319331
if ('count' !== $method) {
320-
echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
332+
fwrite($handle, sprintf(" %dx in %s\n", $count, preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method)));
321333
}
322334
}
323335
}
324336
}
325337
}
326338

327339
if (!empty($notices)) {
328-
echo "\n";
340+
fwrite($handle, "\n");
329341
}
330342
}
331343

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ class Configuration
5252
private $baselineDeprecations = [];
5353

5454
/**
55-
* @param int[] $thresholds A hash associating groups to thresholds
56-
* @param string $regex Will be matched against messages, to decide whether to display a stack trace
57-
* @param bool[] $verboseOutput Keyed by groups
58-
* @param bool $generateBaseline Whether to generate or update the baseline file
59-
* @param string $baselineFile The path to the baseline file
55+
* @var string|null
6056
*/
61-
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = [], $generateBaseline = false, $baselineFile = '')
57+
private $logFile = null;
58+
59+
/**
60+
* @param int[] $thresholds A hash associating groups to thresholds
61+
* @param string $regex Will be matched against messages, to decide whether to display a stack trace
62+
* @param bool[] $verboseOutput Keyed by groups
63+
* @param bool $generateBaseline Whether to generate or update the baseline file
64+
* @param string $baselineFile The path to the baseline file
65+
* @param string|null $logFile The path to the log file
66+
*/
67+
private function __construct(array $thresholds = [], $regex = '', $verboseOutput = [], $generateBaseline = false, $baselineFile = '', $logFile = null)
6268
{
6369
$groups = ['total', 'indirect', 'direct', 'self'];
6470

@@ -119,6 +125,8 @@ private function __construct(array $thresholds = [], $regex = '', $verboseOutput
119125
throw new \InvalidArgumentException(sprintf('The baselineFile "%s" does not exist.', $this->baselineFile));
120126
}
121127
}
128+
129+
$this->logFile = $logFile;
122130
}
123131

124132
/**
@@ -238,6 +246,16 @@ public function verboseOutput($group)
238246
return $this->verboseOutput[$group];
239247
}
240248

249+
public function shouldWriteToLogFile()
250+
{
251+
return null !== $this->logFile;
252+
}
253+
254+
public function getLogFile()
255+
{
256+
return $this->logFile;
257+
}
258+
241259
/**
242260
* @param string $serializedConfiguration an encoded string, for instance
243261
* max[total]=1234&max[indirect]=42
@@ -248,7 +266,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
248266
{
249267
parse_str($serializedConfiguration, $normalizedConfiguration);
250268
foreach (array_keys($normalizedConfiguration) as $key) {
251-
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile'], true)) {
269+
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile', 'logFile'], true)) {
252270
throw new \InvalidArgumentException(sprintf('Unknown configuration option "%s".', $key));
253271
}
254272
}
@@ -260,6 +278,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
260278
'quiet' => [],
261279
'generateBaseline' => false,
262280
'baselineFile' => '',
281+
'logFile' => null,
263282
];
264283

265284
if ('' === $normalizedConfiguration['disabled'] || filter_var($normalizedConfiguration['disabled'], \FILTER_VALIDATE_BOOLEAN)) {
@@ -282,7 +301,8 @@ public static function fromUrlEncodedString($serializedConfiguration)
282301
'',
283302
$verboseOutput,
284303
filter_var($normalizedConfiguration['generateBaseline'], \FILTER_VALIDATE_BOOLEAN),
285-
$normalizedConfiguration['baselineFile']
304+
$normalizedConfiguration['baselineFile'],
305+
$normalizedConfiguration['logFile']
286306
);
287307
}
288308

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Test DeprecationErrorHandler with log file
3+
--FILE--
4+
<?php
5+
$filename = tempnam(sys_get_temp_dir(), 'sf-').uniqid();
6+
$k = 'SYMFONY_DEPRECATIONS_HELPER';
7+
putenv($k.'='.$_SERVER[$k] = $_ENV[$k] = 'logFile='.$filename);
8+
putenv('ANSICON');
9+
putenv('ConEmuANSI');
10+
putenv('TERM');
11+
12+
$vendor = __DIR__;
13+
while (!file_exists($vendor.'/vendor')) {
14+
$vendor = dirname($vendor);
15+
}
16+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
17+
require PHPUNIT_COMPOSER_INSTALL;
18+
require_once __DIR__.'/../../bootstrap.php';
19+
20+
class FooTestCase
21+
{
22+
public function testLegacyFoo()
23+
{
24+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
25+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
26+
}
27+
28+
public function testLegacyBar()
29+
{
30+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
31+
}
32+
}
33+
34+
@trigger_error('root deprecation', E_USER_DEPRECATED);
35+
36+
$foo = new FooTestCase();
37+
$foo->testLegacyFoo();
38+
$foo->testLegacyBar();
39+
40+
register_shutdown_function(function () use ($filename) {
41+
var_dump(file_get_contents($filename));
42+
});
43+
?>
44+
--EXPECTF--
45+
string(234) "
46+
Unsilenced deprecation notices (3)
47+
48+
2x: unsilenced foo deprecation
49+
2x in FooTestCase::testLegacyFoo
50+
51+
1x: unsilenced bar deprecation
52+
1x in FooTestCase::testLegacyBar
53+
54+
Other deprecation notices (1)
55+
56+
1x: root deprecation
57+
58+
"

0 commit comments

Comments
 (0)
0