Skip to content

Commit

Permalink
[Bridge\PhpUnit] Display the stack trace of a deprecation on-demand
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 27, 2015
1 parent 9610602 commit 5a88fb6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
35 changes: 28 additions & 7 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
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
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
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

0 comments on commit 5a88fb6

Please sign in to comment.