Skip to content

Commit ec84aaf

Browse files
bug #18272 [Bridge\PhpUnit] Workaround old phpunit bug, no colors in weak mode, add tests (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Bridge\PhpUnit] Workaround old phpunit bug, no colors in weak mode, add tests | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18222 | License | MIT | Doc PR | - Commits ------- 4ea9548 [Bridge\PhpUnit] Workaround old phpunit bug, no colors in weak mode, add tests
2 parents f4c18db + 4ea9548 commit ec84aaf

File tree

6 files changed

+196
-17
lines changed

6 files changed

+196
-17
lines changed

composer.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@
8282
},
8383
"autoload": {
8484
"psr-4": {
85-
"Symfony\\Bridge\\Doctrine\\": "src/Symfony/Bridge/Doctrine/",
86-
"Symfony\\Bridge\\Monolog\\": "src/Symfony/Bridge/Monolog/",
87-
"Symfony\\Bridge\\ProxyManager\\": "src/Symfony/Bridge/ProxyManager/",
88-
"Symfony\\Bridge\\Swiftmailer\\": "src/Symfony/Bridge/Swiftmailer/",
89-
"Symfony\\Bridge\\Twig\\": "src/Symfony/Bridge/Twig/",
85+
"Symfony\\Bridge\\": "src/Symfony/Bridge/",
9086
"Symfony\\Bundle\\": "src/Symfony/Bundle/",
9187
"Symfony\\Component\\": "src/Symfony/Component/"
9288
},

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public static function register($mode = false)
2525
if (self::$isRegistered) {
2626
return;
2727
}
28+
29+
$getMode = function () use ($mode) {
30+
static $memoizedMode = false;
31+
32+
if (false !== $memoizedMode) {
33+
return $memoizedMode;
34+
}
35+
if (false === $mode) {
36+
$mode = getenv('SYMFONY_DEPRECATIONS_HELPER') ?: '';
37+
}
38+
39+
return $memoizedMode = $mode;
40+
};
41+
2842
$deprecations = array(
2943
'unsilencedCount' => 0,
3044
'remainingCount' => 0,
@@ -35,36 +49,44 @@ public static function register($mode = false)
3549
'legacy' => array(),
3650
'other' => array(),
3751
);
38-
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $mode) {
52+
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode) {
3953
if (E_USER_DEPRECATED !== $type) {
4054
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
4155
}
4256

43-
$trace = debug_backtrace(PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT : true);
57+
$mode = $getMode();
58+
$trace = debug_backtrace(true);
59+
$group = 'other';
4460

4561
$i = count($trace);
46-
while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) {
62+
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_')))) {
4763
// No-op
4864
}
4965

50-
if (0 !== error_reporting()) {
51-
$group = 'unsilenced';
52-
$ref = &$deprecations[$group][$msg]['count'];
53-
++$ref;
54-
} elseif (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
66+
if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
5567
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
5668
$method = $trace[$i]['function'];
5769

58-
$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';
70+
if (0 !== error_reporting()) {
71+
$group = 'unsilenced';
72+
} elseif (0 === strpos($method, 'testLegacy')
73+
|| 0 === strpos($method, 'provideLegacy')
74+
|| 0 === strpos($method, 'getLegacy')
75+
|| strpos($class, '\Legacy')
76+
|| in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true)
77+
) {
78+
$group = 'legacy';
79+
} else {
80+
$group = 'remaining';
81+
}
5982

6083
if ('legacy' !== $group && 'weak' !== $mode) {
6184
$ref = &$deprecations[$group][$msg]['count'];
6285
++$ref;
6386
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
6487
++$ref;
6588
}
66-
} else {
67-
$group = 'other';
89+
} elseif ('weak' !== $mode) {
6890
$ref = &$deprecations[$group][$msg]['count'];
6991
++$ref;
7092
}
@@ -89,10 +111,14 @@ public static function register($mode = false)
89111
} else {
90112
$colorize = function ($str) {return $str;};
91113
}
92-
register_shutdown_function(function () use ($mode, &$deprecations, $deprecationHandler, $colorize) {
114+
register_shutdown_function(function () use ($getMode, &$deprecations, $deprecationHandler, $colorize) {
115+
$mode = $getMode();
93116
$currErrorHandler = set_error_handler('var_dump');
94117
restore_error_handler();
95118

119+
if ('weak' === $mode) {
120+
$colorize = function ($str) {return $str;};
121+
}
96122
if ($currErrorHandler !== $deprecationHandler) {
97123
echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
98124
}
@@ -123,6 +149,7 @@ public static function register($mode = false)
123149
if (!empty($notices)) {
124150
echo "\n";
125151
}
152+
126153
if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
127154
exit(1);
128155
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in default mode
3+
--FILE--
4+
<?php
5+
6+
putenv('SYMFONY_DEPRECATIONS_HELPER');
7+
putenv('ANSICON');
8+
putenv('ConEmuANSI');
9+
putenv('TERM');
10+
11+
$vendor = __DIR__;
12+
while (!file_exists($vendor.'/vendor')) {
13+
$vendor = dirname($vendor);
14+
}
15+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
16+
require PHPUNIT_COMPOSER_INSTALL;
17+
require_once __DIR__.'/../../bootstrap.php';
18+
19+
@trigger_error('root deprecation', E_USER_DEPRECATED);
20+
21+
class PHPUnit_Util_Test
22+
{
23+
public static function getGroups()
24+
{
25+
return array();
26+
}
27+
}
28+
29+
class FooTestCase
30+
{
31+
public function testLegacyFoo()
32+
{
33+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
34+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
35+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
36+
}
37+
38+
public function testNonLegacyBar()
39+
{
40+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
41+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
42+
}
43+
}
44+
45+
$foo = new FooTestCase();
46+
$foo->testLegacyFoo();
47+
$foo->testNonLegacyBar();
48+
49+
?>
50+
--EXPECTF--
51+
Unsilenced deprecation notices (3)
52+
53+
unsilenced foo deprecation: 2x
54+
2x in FooTestCase::testLegacyFoo
55+
56+
unsilenced bar deprecation: 1x
57+
1x in FooTestCase::testNonLegacyBar
58+
59+
Remaining deprecation notices (1)
60+
61+
silenced bar deprecation: 1x
62+
1x in FooTestCase::testNonLegacyBar
63+
64+
Legacy deprecation notices (1)
65+
66+
Other deprecation notices (1)
67+
68+
root deprecation: 1x
69+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in weak mode
3+
--FILE--
4+
<?php
5+
6+
putenv('SYMFONY_DEPRECATIONS_HELPER=weak');
7+
putenv('ANSICON');
8+
putenv('ConEmuANSI');
9+
putenv('TERM');
10+
11+
$vendor = __DIR__;
12+
while (!file_exists($vendor.'/vendor')) {
13+
$vendor = dirname($vendor);
14+
}
15+
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
16+
require PHPUNIT_COMPOSER_INSTALL;
17+
require_once __DIR__.'/../../bootstrap.php';
18+
19+
@trigger_error('root deprecation', E_USER_DEPRECATED);
20+
21+
class FooTestCase
22+
{
23+
public function testLegacyFoo()
24+
{
25+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
26+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
27+
}
28+
}
29+
30+
$foo = new FooTestCase();
31+
$foo->testLegacyFoo();
32+
33+
?>
34+
--EXPECTF--
35+
Unsilenced deprecation notices (1)
36+
37+
Legacy deprecation notices (1)
38+
39+
Other deprecation notices (1)
40+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\TextUI;
13+
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
class Command extends \PHPUnit_TextUI_Command
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
protected function createRunner()
23+
{
24+
return new TestRunner($this->arguments['loader']);
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
protected function handleBootstrap($filename)
31+
{
32+
parent::handleBootstrap($filename);
33+
34+
// By default, we want PHPUnit's autoloader before Symfony's one
35+
if (!getenv('SYMFONY_PHPUNIT_OVERLOAD')) {
36+
$filename = realpath(stream_resolve_include_path($filename));
37+
$symfonyLoader = realpath(dirname(PHPUNIT_COMPOSER_INSTALL).'/../../../vendor/autoload.php');
38+
39+
if ($filename === $symfonyLoader) {
40+
$symfonyLoader = require $symfonyLoader;
41+
$symfonyLoader->unregister();
42+
$symfonyLoader->register(false);
43+
}
44+
}
45+
}
46+
}

src/Symfony/Bridge/PhpUnit/phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<testsuites>
1414
<testsuite name="Symfony PHPUnit Bridge Test Suite">
1515
<directory>./Tests/</directory>
16+
<directory suffix=".phpt">./Tests/DeprecationErrorHandler/</directory>
1617
</testsuite>
1718
</testsuites>
1819

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy