Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bf698e0

Browse filesBrowse files
committed
37679 Add log file option for deprecations
1 parent c82c997 commit bf698e0
Copy full SHA for bf698e0

File tree

4 files changed

+129
-14
lines changed
Filter options

4 files changed

+129
-14
lines changed

‎src/Symfony/Bridge/PhpUnit/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* added log file option
8+
49
5.1.0
510
-----
611

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
+37-11Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -291,23 +291,42 @@ private static function colorize($str, $red)
291291
* @param string[] $groups
292292
* @param Configuration $configuration
293293
* @param bool $isFailing
294+
*
295+
* @throws InvalidArgumentException
294296
*/
295297
private function displayDeprecations($groups, $configuration, $isFailing)
296298
{
297299
$cmp = function ($a, $b) {
298300
return $b->count() - $a->count();
299301
};
300302

303+
if ($configuration->shouldWriteToLogFile()) {
304+
if (false === $handle = @fopen($file = $configuration->getLogFile(), 'a')) {
305+
throw new \InvalidArgumentException(sprintf('The configured log file "%s" is not writeable.', $file));
306+
}
307+
} else {
308+
$handle = \STDOUT;
309+
}
310+
301311
foreach ($groups as $group) {
302312
if ($this->deprecationGroups[$group]->count()) {
303-
echo "\n", self::colorize(
304-
sprintf(
305-
'%s deprecation notices (%d)',
306-
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
307-
$this->deprecationGroups[$group]->count()
308-
),
309-
'legacy' !== $group && 'indirect' !== $group
310-
), "\n";
313+
$deprecationGroupMessage = sprintf(
314+
'%s deprecation notices (%d)',
315+
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
316+
$this->deprecationGroups[$group]->count()
317+
);
318+
if ($configuration->shouldWriteToLogFile()) {
319+
echo "\n", self::colorize(
320+
$deprecationGroupMessage,
321+
'legacy' !== $group && 'indirect' !== $group
322+
), "\n";
323+
} else {
324+
$deprecationGroupMessage = self::colorize(
325+
$deprecationGroupMessage,
326+
'legacy' !== $group && 'indirect' !== $group
327+
);
328+
}
329+
fwrite($handle, "\n$deprecationGroupMessage\n");
311330

312331
if ('legacy' !== $group && !$configuration->verboseOutput($group) && !$isFailing) {
313332
continue;
@@ -316,22 +335,29 @@ private function displayDeprecations($groups, $configuration, $isFailing)
316335
uasort($notices, $cmp);
317336

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

321340
$countsByCaller = $notice->getCountsByCaller();
322341
arsort($countsByCaller);
323342

324343
foreach ($countsByCaller as $method => $count) {
325344
if ('count' !== $method) {
326-
echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
345+
fwrite($handle, sprintf(" %dx in %s\n",
346+
$count,
347+
preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method))
348+
);
327349
}
328350
}
329351
}
330352
}
331353
}
332354

333355
if (!empty($notices)) {
334-
echo "\n";
356+
fwrite($handle, "\n");
357+
}
358+
359+
if ($configuration->shouldWriteToLogFile()) {
360+
@fclose($handle);
335361
}
336362
}
337363

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler/Configuration.php
+26-3Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,20 @@ class Configuration
5151
*/
5252
private $baselineDeprecations = [];
5353

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

@@ -119,6 +125,11 @@ private function __construct(array $thresholds = [], $regex = '', $verboseOutput
119125
throw new \InvalidArgumentException(sprintf('The baselineFile "%s" does not exist.', $this->baselineFile));
120126
}
121127
}
128+
129+
if (file_exists($logFile)) {
130+
throw new \InvalidArgumentException(sprintf('The log file "%s" already exists.', $logFile));
131+
}
132+
$this->logFile = $logFile;
122133
}
123134

124135
/**
@@ -238,6 +249,16 @@ public function verboseOutput($group)
238249
return $this->verboseOutput[$group];
239250
}
240251

252+
public function shouldWriteToLogFile()
253+
{
254+
return null !== $this->logFile;
255+
}
256+
257+
public function getLogFile()
258+
{
259+
return $this->logFile;
260+
}
261+
241262
/**
242263
* @param string $serializedConfiguration an encoded string, for instance
243264
* max[total]=1234&max[indirect]=42
@@ -248,7 +269,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
248269
{
249270
parse_str($serializedConfiguration, $normalizedConfiguration);
250271
foreach (array_keys($normalizedConfiguration) as $key) {
251-
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile'], true)) {
272+
if (!\in_array($key, ['max', 'disabled', 'verbose', 'quiet', 'generateBaseline', 'baselineFile', 'logFile'], true)) {
252273
throw new \InvalidArgumentException(sprintf('Unknown configuration option "%s".', $key));
253274
}
254275
}
@@ -260,6 +281,7 @@ public static function fromUrlEncodedString($serializedConfiguration)
260281
'quiet' => [],
261282
'generateBaseline' => false,
262283
'baselineFile' => '',
284+
'logFile' => null,
263285
];
264286

265287
if ('' === $normalizedConfiguration['disabled'] || filter_var($normalizedConfiguration['disabled'], \FILTER_VALIDATE_BOOLEAN)) {
@@ -282,7 +304,8 @@ public static function fromUrlEncodedString($serializedConfiguration)
282304
'',
283305
$verboseOutput,
284306
filter_var($normalizedConfiguration['generateBaseline'], \FILTER_VALIDATE_BOOLEAN),
285-
$normalizedConfiguration['baselineFile']
307+
$normalizedConfiguration['baselineFile'],
308+
$normalizedConfiguration['logFile']
286309
);
287310
}
288311

+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
Unsilenced deprecation notices (3)
46+
47+
Other deprecation notices (1)
48+
string(234) "
49+
Unsilenced deprecation notices (3)
50+
51+
2x: unsilenced foo deprecation
52+
2x in FooTestCase::testLegacyFoo
53+
54+
1x: unsilenced bar deprecation
55+
1x in FooTestCase::testLegacyBar
56+
57+
Other deprecation notices (1)
58+
59+
1x: root deprecation
60+
61+
"

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.