Skip to content

[Bridge\PhpUnit] Display the stack trace of a deprecation on-demand #16709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,37 @@ public static function register($mode = false)
// No-op
}

if (0 !== error_reporting()) {
$group = 'unsilenced';
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
} elseif (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
$method = $trace[$i]['function'];

$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';
if (0 !== error_reporting()) {
$group = 'unsilenced';
} elseif (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)
) {
$group = 'legacy';
} else {
$group = 'remaining';
}

if (isset($mode[0]) && '/' === $mode[0] && preg_match($mode, $class.'::'.$method)) {
$e = new \Exception($msg);
$r = new \ReflectionProperty($e, 'trace');
$r->setAccessible(true);
$r->setValue($e, array_slice($trace, 1, $i));

echo "\n".ucfirst($group).' deprecation triggered by '.$class.'::'.$method.':';
echo "\n".$msg;
echo "\nStack trace:";
echo "\n".str_replace(' '.getcwd().DIRECTORY_SEPARATOR, ' ', $e->getTraceAsString());
echo "\n";

exit(1);
}
if ('legacy' !== $group && 'weak' !== $mode) {
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
Expand All @@ -78,7 +99,7 @@ public static function register($mode = false)
restore_error_handler();
self::register($mode);
}
} else {
} elseif (!isset($mode[0]) || '/' !== $mode[0]) {
self::$isRegistered = true;
if (self::hasColorSupport()) {
$colorize = function ($str, $red) {
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Bridge/PhpUnit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ It comes with the following features:
* disable the garbage collector;
* enforce a consistent `C` locale;
* auto-register `class_exists` to load Doctrine annotations;
* print a user deprecation notices summary at the end of the test suite.
* print a user deprecation notices summary at the end of the test suite;
* display the stack trace of a deprecation on-demand.

By default any non-legacy-tagged or any non-@-silenced deprecation notices will
make tests fail.
Expand Down Expand Up @@ -51,3 +52,9 @@ You have to decide either to:
* update your code to not use deprecated interfaces anymore, thus gaining better
forward compatibility;
* or move them to the **Legacy** section (by using one of the above way).

In you need to inspect the stack trace of a particular deprecation triggered by
one of your unit tests, you can set the `SYMFONY_DEPRECATIONS_HELPER` env var to
a regexp that matches this test case's `class::method` name. For example,
`SYMFONY_DEPRECATIONS_HELPER=/^MyTest::testMethod$/ phpunit` will stop your test
suite once a deprecation is triggered by the `MyTest::testMethod` test.
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
* ```
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated since version 2.8, to be removed in 3.0. Use ArrayChoiceList instead.
*/
class ArrayKeyChoiceList extends ArrayChoiceList
{
Expand Down