10000 Catch deprecations during shutdown · symfony/symfony@07fe27a · GitHub
[go: up one dir, main page]

Skip to content

Commit 07fe27a

Browse files
committed
Catch deprecations during shutdown
1 parent 5d4deab commit 07fe27a

File tree

2 files changed

+129
-17
lines changed

2 files changed

+129
-17
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,34 +127,55 @@ public static function register($mode = false)
127127
return $b['count'] - $a['count'];
128128
};
129129

130-
foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
131-
if ($deprecations[$group.'Count']) {
132-
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";
130+
$displayDeprecations = function ($deprecations) use ($colorize, $cmp) {
131+
foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
132+
if ($deprecations[$group.'Count']) {
133+
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";
133134

134-
uasort($deprecations[$group], $cmp);
135+
uasort($deprecations[$group], $cmp);
135136

136-
foreach ($deprecations[$group] as $msg => $notices) {
137-
echo "\n ", $notices['count'], 'x: ', $msg, "\n";
137+
foreach ($deprecations[$group] as $msg => $notices) {
138+
echo "\n ", $notices['count'], 'x: ', $msg, "\n";
138139

139-
arsort($notices);
140+
arsort($notices);
140141

141-
foreach ($notices as $method => $count) {
142-
if ('count' !== $method) {
143-
echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
142+
foreach ($notices as $method => $count) {
143+
if ('count' !== $method) {
144+
echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
145+
}
144146
}
145147
}
146148
}
147149
}
150+
if (!empty($notices)) {
151+
echo "\n";
152+
}
153+
};
154+
155+
$displayDeprecations($deprecations);
156+
157+
// store failing status
158+
$isFailing = 'weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other']);
159+
160+
// reset deprecations array
161+
foreach ($deprecations as $key => $arrayOrInt) {
162+
if (is_int($arrayOrInt)) {
163+
$deprecations[$key] = 0;
164+
} else {
165+
$deprecations[$key] = array();
166+
}
148167
}
149-
if (!empty($notices)) {
150-
echo "\n";
151-
}
152168

153-
if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
154-
register_shutdown_function(function () {
169+
register_shutdown_function(function () use (&$deprecations, $isFailing, $displayDeprecations, $mode) {
170+
171+
if (array_sum(array_filter($deprecations, function ($key) { return false !== strpos($key, 'Count'); }, ARRAY_FILTER_USE_KEY)) > 0) {
172+
echo "Deprecations during shutdown\n";
173+
}
174+
$displayDeprecations($deprecations);
175+
if ($isFailing || 'weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
155176
exit(1);
156-
});
157-
}
177+
}
178+
});
158179
});
159180
}
160181
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in default mode
3+
--FILE--
4+
<?php
5+
6+
putenv('SYMFONY_DEPRECATIONS_HELPER');
7+
putenv('ANSICON');
8+
putenv('ConEmuANSI');
9+
putenv('TERM');
10+
11+
$vendor = __DIR__;
12+
while (!file_exists($vendor.'/vendor')) {
13+
$vendor = dirname($vendor);
14+
}
15+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
16+
require PHPUNIT_COMPOSER_INSTALL;
17+
require_once __DIR__.'/../../bootstrap.php';
18+
19+
@trigger_error('root deprecation', E_USER_DEPRECATED);
20+
21+
eval(<<<'EOPHP'
22+
namespace PHPUnit\Util;
23+
24+
class Test
25+
{
26+
public static function getGroups()
27+
{
28+
return array();
29+
}
30+
}
31+
EOPHP
32+
);
33+
34+
class PHPUnit_Util_Test
35+
{
36+
public static function getGroups()
37+
{
38+
return array();
39+
}
40+
}
41+
42+
class FooTestCase
43+
{
44+
public function testLegacyFoo()
45+
{
46+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
47+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
48+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
49+
}
50+
51+
public function testNonLegacyBar()
52+
{
53+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
54+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
55+
}
56+
}
57+
58+
$foo = new FooTestCase();
59+
$foo->testLegacyFoo();
60+
$foo->testNonLegacyBar();
61+
62+
register_shutdown_function(function () {
63+
@trigger_error('root deprecation during shutdown', E_USER_DEPRECATED);
64+
});
65+
66+
?>
67+
--EXPECTF--
68+
Unsilenced deprecation notices (3)
69+
70+
2x: unsilenced foo deprecation
71+
2x in FooTestCase::testLegacyFoo
72+
73+
1x: unsilenced bar deprecation
74+
1x in FooTestCase::testNonLegacyBar
75+
76+
Remaining deprecation notices (1)
77+
78+
1x: silenced bar deprecation
79+
1x in FooTestCase::testNonLegacyBar
80+
81+
Legacy deprecation notices (1)
82+
83+
Other deprecation notices (1)
84+
85+
1x: root deprecation
86+
87+
Deprecations during shutdown
88+
89+
Other deprecation notices (1)
90+
91+
1x: root deprecation during shutdown

0 commit comments

Comments
 (0)
0