Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b91ee53

Browse files
[Bridge\PhpUnit] Add "disabled" mode to SYMFONY_DEPRECATIONS_HELPER
1 parent 83ebf97 commit b91ee53

File tree

12 files changed

+210
-19
lines changed

12 files changed

+210
-19
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ install:
7474
- if [[ $deps = high && ${SYMFONY_VERSION%.*} != $(git show $(git ls-remote --heads | grep -FA1 /$SYMFONY_VERSION | tail -n 1):composer.json | grep '^ *"dev-master". *"[1-9]' | grep -o '[0-9]*' | head -n 1) ]]; then LEGACY=,legacy; fi;
7575
- export COMPOSER_ROOT_VERSION=$SYMFONY_VERSION.x-dev;
7676
- if [[ ! $deps ]]; then composer update --prefer-dist; else export SYMFONY_DEPRECATIONS_HELPER=weak; fi;
77+
- if [[ $TRAVIS_BRANCH = master ]]; then export SYMFONY_PHPUNIT_OVERLOAD=1; fi;
7778
- if [[ $TRAVIS_PHP_VERSION != hhvm ]]; then php -i; else hhvm --php -r 'print_r($_SERVER);print_r(ini_get_all());'; fi;
7879

7980
script:
8081
- if [[ ! $deps ]]; then echo "$COMPONENTS" | parallel --gnu '$PHPUNIT --exclude-group tty,benchmark,intl-data {}'; fi;
8182
- if [[ ! $deps ]]; then echo -e "\\nRunning tests requiring tty"; $PHPUNIT --group tty; fi;
8283
- if [[ ! $deps && $TRAVIS_PHP_VERSION = ${MIN_PHP%.*} ]]; then echo -e "1\\n0" | xargs -I{} sh -c 'echo "\\nPHP --enable-sigchild enhanced={}" && ENHANCE_SIGCHLD={} php-$MIN_PHP/sapi/cli/php .phpunit/phpunit-4.8/phpunit --colors=always src/Symfony/Component/Process/'; fi;
8384
- if [[ $deps = high ]]; then echo "$COMPONENTS" | parallel --gnu -j10% 'cd {}; composer update --prefer-dist; $PHPUNIT --exclude-group tty,benchmark,intl-data'$LEGACY; fi;
84-
- if [[ $deps = high ]]; then (cd src/Symfony/Bridge/PhpUnit && phpenv global 5.3 && php --version && $PHPUNIT && find -name '*.php' -not -path './vendor/*' | xargs -n1 php -l); fi;
8585
- if [[ $deps = low ]]; then echo "$COMPONENTS" | parallel --gnu -j10% 'cd {}; composer update --prefer-dist --prefer-lowest --prefer-stable; $PHPUNIT --exclude-group tty,benchmark,intl-data'; fi;
86+
# Test the PhpUnit bridge using the original phpunit script
87+
- if [[ $deps = low ]]; then (cd src/Symfony/Bridge/PhpUnit && phpenv global 5.3 && php --version && composer update --prefer-dist && phpunit && find -name '*.php' -not -path './vendor/*' | xargs -n1 php -l); fi;
8688
- if [[ $deps = skip ]]; then echo This matrix line is skipped for pull requests.; fi;

composer.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@
9393
},
9494
"autoload": {
9595
"psr-4": {
96-
"Symfony\\Bridge\\Doctrine\\": "src/Symfony/Bridge/Doctrine/",
97-
"Symfony\\Bridge\\Monolog\\": "src/Symfony/Bridge/Monolog/",
98-
"Symfony\\Bridge\\ProxyManager\\": "src/Symfony/Bridge/ProxyManager/",
99-
"Symfony\\Bridge\\Twig\\": "src/Symfony/Bridge/Twig/",
96+
"Symfony\\Bridge\\": "src/Symfony/Bridge/",
10097
"Symfony\\Bundle\\": "src/Symfony/Bundle/",
10198
"Symfony\\Component\\": "src/Symfony/Component/"
10299
},

phpunit

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,17 @@ if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__
5454
passthru("$COMPOSER remove --no-update symfony/yaml");
5555
passthru("$COMPOSER require --dev --no-update symfony/phpunit-bridge \">=3.1@dev\"");
5656
passthru("$COMPOSER install --prefer-dist --no-progress --ansi");
57-
file_put_contents('phpunit', <<<EOPHP
57+
file_put_contents('phpunit', <<<'EOPHP'
5858
<?php
5959
6060
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/vendor/autoload.php');
61-
require PHPUNIT_COMPOSER_INSTALL;
61+
62+
$loader = require PHPUNIT_COMPOSER_INSTALL;
63+
64+
if (getenv('SYMFONY_PHPUNIT_OVERLOAD') && file_exists(__DIR__.'/../../src/Symfony/Bridge/PhpUnit')) {
65+
$loader->addPsr4('Symfony\\Bridge\\PhpUnit\\', array('src/Symfony/Bridge/PhpUnit'), true);
66+
}
67+
unset($loader);
6268
Symfony\Bridge\PhpUnit\TextUI\Command::main();
6369

6470
EOPHP

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
class DeprecationErrorHandler
2020
{
2121
const MODE_WEAK = 'weak';
22+
const MODE_DISABLED = 'disabled';
2223

2324
private static $isRegistered = false;
2425

@@ -39,9 +40,23 @@ public static function register($mode = 0)
3940
if (self::$isRegistered) {
4041
return;
4142
}
42-
if (self::MODE_WEAK !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
43-
$mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
44-
}
43+
44+
$getMode = function () use ($mode) {
45+
static $memoizedMode = false;
46+
47+
if (false !== $memoizedMode) {
48+
return $memoizedMode;
49+
}
50+
if (false === $mode) {
51+
$mode = getenv('SYMFONY_DEPRECATIONS_HELPER');
52+
}
53+
if (DeprecationErrorHandler::MODE_WEAK !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
54+
$mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
55+
}
56+
57+
return $memoizedMode = $mode;
58+
};
59+
4560
$deprecations = array(
4661
'unsilencedCount' => 0,
4762
'remainingCount' => 0,
@@ -52,8 +67,8 @@ public static function register($mode = 0)
5267
'legacy' => array(),
5368
'other' => array(),
5469
);
55-
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $mode) {
56-
if (E_USER_DEPRECATED !== $type) {
70+
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $getMode) {
71+
if (E_USER_DEPRECATED !== $type || DeprecationErrorHandler::MODE_DISABLED === $mode = $getMode()) {
5772
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
5873
}
5974

@@ -95,13 +110,13 @@ public static function register($mode = 0)
95110

96111
exit(1);
97112
}
98-
if ('legacy' !== $group && self::MODE_WEAK !== $mode) {
113+
if ('legacy' !== $group && DeprecationErrorHandler::MODE_WEAK !== $mode) {
99114
$ref = &$deprecations[$group][$msg]['count'];
100115
++$ref;
101116
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
102117
++$ref;
103118
}
104-
} else {
119+
} elseif (DeprecationErrorHandler::MODE_WEAK !== $mode) {
105120
$group = 'other';
106121
$ref = &$deprecations[$group][$msg]['count'];
107122
++$ref;
@@ -116,7 +131,7 @@ public static function register($mode = 0)
116131
restore_error_handler();
117132
self::register($mode);
118133
}
119-
} elseif (!isset($mode[0]) || '/' !== $mode[0]) {
134+
} else {
120135
self::$isRegistered = true;
121136
if (self::hasColorSupport()) {
122137
$colorize = function ($str, $red) {
@@ -127,10 +142,17 @@ public static function register($mode = 0)
127142
} else {
128143
$colorize = function ($str) {return $str;};
129144
}
130-
register_shutdown_function(function () use ($mode, &$deprecations, $deprecationHandler, $colorize) {
145+
register_shutdown_function(function () use ($getMode, &$deprecations, $deprecationHandler, $colorize) {
146+
$mode = $getMode();
147+
if (isset($mode[0]) && '/' === $mode[0]) {
148+
return;
149+
}
131150
$currErrorHandler = set_error_handler('var_dump');
132151
restore_error_handler();
133152

153+
if (DeprecationErrorHandler::MODE_WEAK === $mode) {
154+
$colorize = function ($str) {return $str;};
155+
}
134156
if ($currErrorHandler !== $deprecationHandler) {
135157
echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
136158
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in default mode
3+
--SKIPIF--
4+
<?php if (!file_exists('DeprecationErrorHandler.php')) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
putenv('SYMFONY_DEPRECATIONS_HELPER');
9+
10+
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/../../vendor/autoload.php');
11+
require PHPUNIT_COMPOSER_INSTALL;
12+
13+
@trigger_error('root deprecation', E_USER_DEPRECATED);
14+
15+
class PHPUnit_Util_Test
16+
{
17+
public static function getGroups()
18+
{
19+
return array();
20+
}
21+
}
22+
23+
class FooTestCase
24+
{
25+
public function testLegacyFoo()
26+
{
27+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
28+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
29+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
30+
}
31+
32+
public function testNonLegacyBar()
33+
{
34+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
35+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
36+
}
37+
}
38+
39+
$foo = new FooTestCase();
40+
$foo->testLegacyFoo();
41+
$foo->testNonLegacyBar();
42+
43+
?>
44+
--EXPECTF--
45+
Unsilenced deprecation notices (3)
46+
47+
unsilenced foo deprecation: 2x
48+
2x in FooTestCase::testLegacyFoo
49+
50+
unsilenced bar deprecation: 1x
51+
1x in FooTestCase::testNonLegacyBar
52+
53+
Remaining deprecation notices (1)
54+
55+
silenced bar deprecation: 1x
56+
1x in FooTestCase::testNonLegacyBar
57+
58+
Legacy deprecation notices (1)
59+
60+
Other deprecation notices (1)
61+
62+
root deprecation: 1x
63+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in weak mode
3+
--SKIPIF--
4+
<?php if (!file_exists('DeprecationErrorHandler.php')) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
putenv('SYMFONY_DEPRECATIONS_HELPER=disabled');
9+
10+
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/../../vendor/autoload.php');
11+
require PHPUNIT_COMPOSER_INSTALL;
12+
13+
echo (int) set_error_handler('var_dump');
14+
echo (int) class_exists('Symfony\Bridge\PhpUnit\DeprecationErrorHandler', false);
15+
16+
?>
17+
--EXPECTF--
18+
00
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in weak mode
3+
--SKIPIF--
4+
<?php if (!file_exists('DeprecationErrorHandler.php')) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
putenv('SYMFONY_DEPRECATIONS_HELPER=/foo/');
9+
10+
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/../../vendor/autoload.php');
11+
require PHPUNIT_COMPOSER_INSTALL;
12+
13+
@trigger_error('root deprecation', E_USER_DEPRECATED);
14+
15+
class FooTestCase
16+
{
17+
public function testLegacyFoo()
18+
{
19+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
20+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
21+
}
22+
}
23+
24+
$foo = new FooTestCase();
25+
$foo->testLegacyFoo();
26+
27+
?>
28+
--EXPECTF--
29+
Legacy deprecation triggered by FooTestCase::testLegacyFoo:
30+
silenced foo deprecation
31+
Stack trace:
32+
#0 -(14): trigger_error()
33+
#1 -(20): FooTestCase->testLegacyFoo()
34+
#2 {main}
35+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in weak mode
3+
--SKIPIF--
4+
<?php if (!file_exists('DeprecationErrorHandler.php')) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
putenv('SYMFONY_DEPRECATIONS_HELPER=weak');
9+
10+
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/../../vendor/autoload.php');
11+
require PHPUNIT_COMPOSER_INSTALL;
12+
13+
@trigger_error('root deprecation', E_USER_DEPRECATED);
14+
15+
class FooTestCase
16+
{
17+
public function testLegacyFoo()
18+
{
19+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
20+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
21+
}
22+
}
23+
24+
$foo = new FooTestCase();
25+
$foo->testLegacyFoo();
26+
27+
?>
28+
--EXPECTF--
29+
Unsilenced deprecation notices (1)
30+
31+
Legacy deprecation notices (1)
32+

src/Symfony/Bridge/PhpUnit/Tests/DnsMockTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
use Symfony\Bridge\PhpUnit\DnsMock;
1515

16-
require_once __DIR__.'/../DnsMock.php';
17-
1816
class DnsMockTest extends \PHPUnit_Framework_TestCase
1917
{
2018
protected function tearDown()

src/Symfony/Bridge/PhpUnit/TextUI/Command.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,19 @@ protected function createRunner()
2323
{
2424
return new TestRunner($this->arguments['loader']);
2525
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
protected function handleBootstrap($filename)
31+
{
32+
parent::handleBootstrap($filename);
33+
34+
// By default, we want PHPUnit's autoloader to be always first
35+
if (!getenv('SYMFONY_PHPUNIT_OVERLOAD')) {
36+
$loader = require PHPUNIT_COMPOSER_INSTALL;
37+
$loader->unregister();
38+
$loader->register(true);
39+
}
40+
}
2641
}

src/Symfony/Bridge/PhpUnit/bootstrap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@
2424
AnnotationRegistry::registerLoader('class_exists');
2525
}
2626

27-
DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER'));
27+
if ('disabled' !== getenv('SYMFONY_DEPRECATIONS_HELPER')) {
28+
DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER'));
29+
}

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)