Skip to content

Commit bf78d74

Browse files
greg0irenicolas-grekas
authored andcommitted
[Bridge\PhpUnit] Exit as late as possible
People might want to register other shutdown functions that should be able to control the exit code themselves, without the deprecation error handler taking over. The php manual says: > If you call exit() within one registered shutdown function, processing > will stop completely and no other registered shutdown functions will be > called. See https://secure.php.net/manual/en/function.register-shutdown-function.php
1 parent 0924c00 commit bf78d74

File tree

3 files changed

+133
-16
lines changed

3 files changed

+133
-16
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

+37-16
Original file line numberDiff line numberDiff line change
@@ -127,32 +127,53 @@ 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
}
148-
}
149-
if (!empty($notices)) {
150-
echo "\n";
151-
}
150+
if (!empty($notices)) {
151+
echo "\n";
152+
}
153+
};
154+
155+
$displayDeprecations($deprecations);
152156

153-
if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
154-
exit(1);
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+
$deprecations = is_int($arrayOrInt) ? 0 : array();
155163
}
164+
165+
register_shutdown_function(function () use (&$deprecations, $isFailing, $displayDeprecations, $mode) {
166+
foreach ($deprecations as $group => $arrayOrInt) {
167+
if (0 < (is_int($arrayOrInt) ? $arrayOrInt : count($arrayOrInt))) {
168+
echo "Shutdown-time deprecations:\n";
169+
break;
170+
}
171+
}
172+
$displayDeprecations($deprecations);
173+
if ($isFailing || 'weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
174+
exit(1);
175+
}
176+
});
156177
});
157178
}
158179
}

src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/default.phpt

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ $foo = new FooTestCase();
5959
$foo->testLegacyFoo();
6060
$foo->testNonLegacyBar();
6161

62+
register_shutdown_function(function () {
63+
exit('I get precedence over any exit statements inside the deprecation error handler.');
64+
});
65+
6266
?>
6367
--EXPECTF--
6468
Unsilenced deprecation notices (3)
@@ -80,3 +84,4 @@ Other deprecation notices (1)
8084

8185
1x: root deprecation
8286

87+
I get precedence over any exit statements inside the deprecation error handler.
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+
Shutdown-time deprecations:
88+
89+
Other deprecation notices (1)
90+
91+
1x: root deprecation during shutdown

0 commit comments

Comments
 (0)