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

Skip to content

Commit 2e0fa55

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

7 files changed

Lines changed: 188 additions & 8 deletions

File tree

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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
15+
@trigger_error('root deprecation', E_USER_DEPRECATED);
16+
17+
class PHPUnit_Util_Test
18+
{
19+
public static function getGroups()
20+
{
21+
return array();
22+
}
23+
}
24+
25+
class FooTestCase
26+
{
27+
public function testLegacyFoo()
28+
{
29+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
30+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
31+
}
32+
33+
public function testNonLegacyBar()
34+
{
35+
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
36+
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
37+
}
38+
}
39+
40+
$foo = new FooTestCase();
41+
$foo->testLegacyFoo();
42+
$foo->testNonLegacyBar();
43+
44+
?>
45+
--EXPECTF--
46+
Unsilenced deprecation notices (2)
47+
48+
unsilenced foo deprecation: 1x
49+
1x in FooTestCase::testLegacyFoo
50+
51+
unsilenced bar deprecation: 1x
52+
1x in FooTestCase::testNonLegacyBar
53+
54+
Remaining deprecation notices (1)
55+
56+
silenced bar deprecation: 1x
57+
1x in FooTestCase::testNonLegacyBar
58+
59+
Legacy deprecation notices (1)
60+
61+
Other deprecation notices (1)
62+
63+
root deprecation: 1x
64+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
15+
echo (int) set_error_handler('var_dump');
16+
echo (int) class_exists('Symfony\Bridge\PhpUnit\DeprecationErrorHandler', false);
17+
18+
?>
19+
--EXPECTF--
20+
00
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
15+
@trigger_error('root deprecation', E_USER_DEPRECATED);
16+
17+
class FooTestCase
18+
{
19+
public function testLegacyFoo()
20+
{
21+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
22+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
23+
}
24+
}
25+
26+
$foo = new FooTestCase();
27+
$foo->testLegacyFoo();
28+
29+
?>
30+
--EXPECTF--
31+
Legacy deprecation triggered by FooTestCase::testLegacyFoo:
32+
silenced foo deprecation
33+
Stack trace:
34+
#0 -(13): trigger_error()
35+
#1 -(19): FooTestCase->testLegacyFoo()
36+
#2 {main}
37+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
15+
@trigger_error('root deprecation', E_USER_DEPRECATED);
16+
17+
class FooTestCase
18+
{
19+
public function testLegacyFoo()
20+
{
21+
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
22+
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
23+
}
24+
}
25+
26+
$foo = new FooTestCase();
27+
$foo->testLegacyFoo();
28+
29+
?>
30+
--EXPECTF--
31+
Unsilenced deprecation notices (1)
32+
33+
Legacy deprecation notices (1)
34+

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)