[Bridge\PhpUnit] Workaround old phpunit bug, no colors in weak mode, add tests

This commit is contained in:
Nicolas Grekas 2016-03-23 13:13:39 +01:00
parent f4c18db19a
commit 4ea9548e23
6 changed files with 196 additions and 17 deletions

View File

@ -82,11 +82,7 @@
},
"autoload": {
"psr-4": {
"Symfony\\Bridge\\Doctrine\\": "src/Symfony/Bridge/Doctrine/",
"Symfony\\Bridge\\Monolog\\": "src/Symfony/Bridge/Monolog/",
"Symfony\\Bridge\\ProxyManager\\": "src/Symfony/Bridge/ProxyManager/",
"Symfony\\Bridge\\Swiftmailer\\": "src/Symfony/Bridge/Swiftmailer/",
"Symfony\\Bridge\\Twig\\": "src/Symfony/Bridge/Twig/",
"Symfony\\Bridge\\": "src/Symfony/Bridge/",
"Symfony\\Bundle\\": "src/Symfony/Bundle/",
"Symfony\\Component\\": "src/Symfony/Component/"
},

View File

@ -25,6 +25,20 @@ class DeprecationErrorHandler
if (self::$isRegistered) {
return;
}
$getMode = function () use ($mode) {
static $memoizedMode = false;
if (false !== $memoizedMode) {
return $memoizedMode;
}
if (false === $mode) {
$mode = getenv('SYMFONY_DEPRECATIONS_HELPER') ?: '';
}
return $memoizedMode = $mode;
};
$deprecations = array(
'unsilencedCount' => 0,
'remainingCount' => 0,
@ -35,27 +49,36 @@ class DeprecationErrorHandler
'legacy' => array(),
'other' => array(),
);
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $mode) {
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode) {
if (E_USER_DEPRECATED !== $type) {
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
}
$trace = debug_backtrace(PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT : true);
$mode = $getMode();
$trace = debug_backtrace(true);
$group = 'other';
$i = count($trace);
while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) {
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_')))) {
// 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 ('legacy' !== $group && 'weak' !== $mode) {
$ref = &$deprecations[$group][$msg]['count'];
@ -63,8 +86,7 @@ class DeprecationErrorHandler
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
++$ref;
}
} else {
$group = 'other';
} elseif ('weak' !== $mode) {
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
}
@ -89,10 +111,14 @@ class DeprecationErrorHandler
} else {
$colorize = function ($str) {return $str;};
}
register_shutdown_function(function () use ($mode, &$deprecations, $deprecationHandler, $colorize) {
register_shutdown_function(function () use ($getMode, &$deprecations, $deprecationHandler, $colorize) {
$mode = $getMode();
$currErrorHandler = set_error_handler('var_dump');
restore_error_handler();
if ('weak' === $mode) {
$colorize = function ($str) {return $str;};
}
if ($currErrorHandler !== $deprecationHandler) {
echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
}
@ -123,6 +149,7 @@ class DeprecationErrorHandler
if (!empty($notices)) {
echo "\n";
}
if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
exit(1);
}

View File

@ -0,0 +1,69 @@
--TEST--
Test DeprecationErrorHandler in default mode
--FILE--
<?php
putenv('SYMFONY_DEPRECATIONS_HELPER');
putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');
$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
require_once __DIR__.'/../../bootstrap.php';
@trigger_error('root deprecation', E_USER_DEPRECATED);
class PHPUnit_Util_Test
{
public static function getGroups()
{
return array();
}
}
class FooTestCase
{
public function testLegacyFoo()
{
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
}
public function testNonLegacyBar()
{
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
}
}
$foo = new FooTestCase();
$foo->testLegacyFoo();
$foo->testNonLegacyBar();
?>
--EXPECTF--
Unsilenced deprecation notices (3)
unsilenced foo deprecation: 2x
2x in FooTestCase::testLegacyFoo
unsilenced bar deprecation: 1x
1x in FooTestCase::testNonLegacyBar
Remaining deprecation notices (1)
silenced bar deprecation: 1x
1x in FooTestCase::testNonLegacyBar
Legacy deprecation notices (1)
Other deprecation notices (1)
root deprecation: 1x

View File

@ -0,0 +1,40 @@
--TEST--
Test DeprecationErrorHandler in weak mode
--FILE--
<?php
putenv('SYMFONY_DEPRECATIONS_HELPER=weak');
putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');
$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;
require_once __DIR__.'/../../bootstrap.php';
@trigger_error('root deprecation', E_USER_DEPRECATED);
class FooTestCase
{
public function testLegacyFoo()
{
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
}
}
$foo = new FooTestCase();
$foo->testLegacyFoo();
?>
--EXPECTF--
Unsilenced deprecation notices (1)
Legacy deprecation notices (1)
Other deprecation notices (1)

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PhpUnit\TextUI;
/**
* {@inheritdoc}
*/
class Command extends \PHPUnit_TextUI_Command
{
/**
* {@inheritdoc}
*/
protected function createRunner()
{
return new TestRunner($this->arguments['loader']);
}
/**
* {@inheritdoc}
*/
protected function handleBootstrap($filename)
{
parent::handleBootstrap($filename);
// By default, we want PHPUnit's autoloader before Symfony's one
if (!getenv('SYMFONY_PHPUNIT_OVERLOAD')) {
$filename = realpath(stream_resolve_include_path($filename));
$symfonyLoader = realpath(dirname(PHPUNIT_COMPOSER_INSTALL).'/../../../vendor/autoload.php');
if ($filename === $symfonyLoader) {
$symfonyLoader = require $symfonyLoader;
$symfonyLoader->unregister();
$symfonyLoader->register(false);
}
}
}
}

View File

@ -13,6 +13,7 @@
<testsuites>
<testsuite name="Symfony PHPUnit Bridge Test Suite">
<directory>./Tests/</directory>
<directory suffix=".phpt">./Tests/DeprecationErrorHandler/</directory>
</testsuite>
</testsuites>