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

Skip to content

Commit 8e00abf

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

File tree

8 files changed

+182
-9
lines changed

8 files changed

+182
-9
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ script:
8181
- if [[ ! $deps ]]; then echo -e "\\nRunning tests requiring tty"; $PHPUNIT --group tty; fi;
8282
- 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;
8383
- 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;
84+
# Test the PhpUnit bridge using the original phpunit script
85+
- 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;
8586
- 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;
8687
- if [[ $deps = skip ]]; then echo This matrix line is skipped for pull requests.; fi;

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 29 additions & 7 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+
$mode = 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,
@@ -53,7 +68,7 @@ public static function register($mode = 0)
5368
'other' => array(),
5469
);
5570
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $mode) {
56-
if (E_USER_DEPRECATED !== $type) {
71+
if (E_USER_DEPRECATED !== $type || DeprecationErrorHandler::MODE_DISABLED === $mode = $mode()) {
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) {
@@ -128,9 +143,16 @@ public static function register($mode = 0)
128143
$colorize = function ($str) {return $str;};
129144
}
130145
register_shutdown_function(function () use ($mode, &$deprecations, $deprecationHandler, $colorize) {
146+
$mode = $mode();
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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}
30+
31+
public function testNonLegacyBar()
32+
{
33+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
34+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
35+
}
36+
}
37+
38+
$foo = new FooTestCase();
39+
$foo->testLegacyFoo();
40+
$foo->testNonLegacyBar();
41+
42+
?>
43+
--EXPECTF--
44+
Unsilenced deprecation notices (2)
45+
46+
unsilenced foo deprecation: 1x
47+
1x in FooTestCase::testLegacyFoo
48+
49+
unsilenced bar deprecation: 1x
50+
1x in FooTestCase::testNonLegacyBar
51+
52+
Remaining deprecation notices (1)
53+
54+
silenced bar deprecation: 1x
55+
1x in FooTestCase::testNonLegacyBar
56+
57+
Legacy deprecation notices (1)
58+
59+
Other deprecation notices (1)
60+
61+
root deprecation: 1x
62+
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/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)