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

Skip to content

Commit d79d731

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

File tree

12 files changed

+222
-19
lines changed

12 files changed

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

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)