Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

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
Mar 23, 2016
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
6 changes: 1 addition & 5 deletions 6 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
},
Expand Down
51 changes: 39 additions & 12 deletions 51 src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ public static function register($mode = false)
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,
Expand All @@ -35,36 +49,44 @@ public static function register($mode = false)
'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'];
++$ref;
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
++$ref;
}
} else {
$group = 'other';
} elseif ('weak' !== $mode) {
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
}
Expand All @@ -89,10 +111,14 @@ public static function register($mode = false)
} 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";
}
Expand Down Expand Up @@ -123,6 +149,7 @@ public static function register($mode = false)
if (!empty($notices)) {
echo "\n";
}

if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
exit(1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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

40 changes: 40 additions & 0 deletions 40 src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/weak.phpt
Original file line number Diff line number Diff line change
@@ -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)

46 changes: 46 additions & 0 deletions 46 src/Symfony/Bridge/PhpUnit/TextUI/Command.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
1 change: 1 addition & 0 deletions 1 src/Symfony/Bridge/PhpUnit/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<testsuites>
<testsuite name="Symfony PHPUnit Bridge Test Suite">
<directory>./Tests/</directory>
<directory suffix=".phpt">./Tests/DeprecationErrorHandler/</directory>
</testsuite>
</testsuites>

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.