8000 [Bridge\PhpUnit] Display the stack trace of a deprecation on-demand · symfony/symfony@b00598d · GitHub
[go: up one dir, main page]

Skip to content

Commit b00598d

Browse files
[Bridge\PhpUnit] Display the stack trace of a deprecation on-demand
1 parent 9610602 commit b00598d

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,37 @@ public static function register($mode = false)
4747
// No-op
4848
}
4949

50-
if (0 !== error_reporting()) {
51-
$group = 'unsilenced';
52-
$ref = &$deprecations[$group][$msg]['count'];
53-
++$ref;
54-
} elseif (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
50+
if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
5551
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
5652
$method = $trace[$i]['function'];
5753

58-
$group = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\Legacy') || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true) ? 'legacy' : 'remaining';
54+
if (0 !== error_reporting()) {
55+
$group = 'unsilenced';
56+
} elseif (0 === strpos($method, 'testLegacy')
57+
|| 0 === strpos($method, 'provideLegacy')
58+
|| 0 === strpos($method, 'getLegacy')
59+
|| strpos($class, '\Legacy')
60+
|| in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true)
61+
) {
62+
$group = 'legacy';
63+
} else {
64+
$group = 'remaining';
65+
}
66+
67+
if (isset($mode[0]) && '/' === $mode[0] && preg_match($mode, $class.'::'.$method)) {
68+
$e = new \Exception($msg);
69+
$r = new \ReflectionProperty($e, 'trace');
70+
$r->setAccessible(true);
71+
$r->setValue($e, array_slice($trace, 1, $i));
5972

73+
echo "\n".ucfirst($group).' deprecation triggered by '.$class.'::'.$method.':';
74+
echo "\n".$msg;
75+
echo "\nStack trace:";
76+
echo "\n".str_replace(' '.getcwd().DIRECTORY_SEPARATOR, ' ', $e->getTraceAsString());
77+
echo "\n";
78+
79+
exit(1);
80+
}
6081
if ('legacy' !== $group && 'weak' !== $mode) {
6182
$ref = &$deprecations[$group][$msg]['count'];
6283
++$ref;
@@ -78,7 +99,7 @@ public static function register($mode = false)
7899
restore_error_handler();
79100
self::register($mode);
80101
}
81-
} else {
102+
} elseif (!isset($mode[0]) || '/' !== $mode[0]) {
82103
self::$isRegistered = true;
83104
if (self::hasColorSupport()) {
84105
$colorize = function ($str, $red) {

src/Symfony/Bridge/PhpUnit/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ It comes with the following features:
88
* disable the garbage collector;
99
* enforce a consistent `C` locale;
1010
* auto-register `class_exists` to load Doctrine annotations;
11-
* print a user deprecation notices summary at the end of the test suite.
11+
* print a user deprecation notices summary at the end of the test suite;
12+
* display the stack trace of a deprecation on-demand.
1213

1314
By default any non-legacy-tagged or any non-@-silenced deprecation notices will
1415
make tests fail.
@@ -51,3 +52,9 @@ You have to decide either to:
5152
* update your code to not use deprecated interfaces anymore, thus gaining better
5253
forward compatibility;
5354
* or move them to the **Legacy** section (by using one of the above way).
55+
56+
In you need to inspect the stack trace of a particular deprecation triggered by
57+
one of your unit tests, you can set the `SYMFONY_DEPRECATIONS_HELPER` env var to
58+
a regexp that matches this test case's `class::method` name. For example,
59+
`SYMFONY_DEPRECATIONS_HELPER=/^MyTest::testMethod$/ phpunit` will stop your test
60+
suite once a deprecation is triggered by the `MyTest::testMethod` test.

0 commit comments

Comments
 (0)
0