diff --git a/composer.json b/composer.json index 52dd187bb2..571566f802 100644 --- a/composer.json +++ b/composer.json @@ -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/" }, diff --git a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php index 063723d32d..b06b66bf34 100644 --- a/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php +++ b/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php @@ -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); } diff --git a/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/default.phpt b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/default.phpt new file mode 100644 index 0000000000..fac5c53ae7 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/default.phpt @@ -0,0 +1,69 @@ +--TEST-- +Test DeprecationErrorHandler in default mode +--FILE-- +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 + diff --git a/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/weak.phpt b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/weak.phpt new file mode 100644 index 0000000000..9e78d96e70 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/weak.phpt @@ -0,0 +1,40 @@ +--TEST-- +Test DeprecationErrorHandler in weak mode +--FILE-- +testLegacyFoo(); + +?> +--EXPECTF-- +Unsilenced deprecation notices (1) + +Legacy deprecation notices (1) + +Other deprecation notices (1) + diff --git a/src/Symfony/Bridge/PhpUnit/TextUI/Command.php b/src/Symfony/Bridge/PhpUnit/TextUI/Command.php new file mode 100644 index 0000000000..620844c61a --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/TextUI/Command.php @@ -0,0 +1,46 @@ + + * + * 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); + } + } + } +} diff --git a/src/Symfony/Bridge/PhpUnit/phpunit.xml.dist b/src/Symfony/Bridge/PhpUnit/phpunit.xml.dist index 7f631b2ece..9b64b02947 100644 --- a/src/Symfony/Bridge/PhpUnit/phpunit.xml.dist +++ b/src/Symfony/Bridge/PhpUnit/phpunit.xml.dist @@ -13,6 +13,7 @@ ./Tests/ + ./Tests/DeprecationErrorHandler/