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

Skip to content

Commit 5747dc1

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

File tree

12 files changed

+247
-27
lines changed

12 files changed

+247
-27
lines changed

.travis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ services:
3737
- redis-server
3838

3939
before_install:
40+
- mkdir /tmp/slapd
41+
- slapd -f src/Symfony/Component/Ldap/Tests/Fixtures/conf/slapd.conf -h ldap://localhost:3389 &
4042
# Matrix lines for intermediate PHP versions are skipped for pull requests
4143
- if [[ ! $deps && ! $TRAVIS_PHP_VERSION = ${MIN_PHP%.*} && $TRAVIS_PHP_VERSION != hhvm && $TRAVIS_PULL_REQUEST != false ]]; then deps=skip; fi;
4244
# A sigchild-enabled-PHP is used to test the Process component on the lowest PHP matrix line
@@ -57,9 +59,6 @@ before_install:
5759
- if [[ $deps != skip && $TRAVIS_REPO_SLUG = symfony/symfony ]]; then cp .composer/* ~/.composer/; composer global install --prefer-dist; fi;
5860
- if [[ $deps != skip ]]; then ./phpunit install; fi;
5961
- export PHPUNIT=$(readlink -f ./phpunit)
60-
- mkdir /tmp/slapd
61-
- slapd -f src/Symfony/Component/Ldap/Tests/Fixtures/conf/slapd.conf -h ldap://localhost:3389 &
62-
- sleep 3
6362
- ldapadd -h localhost:3389 -D cn=admin,dc=symfony,dc=com -w symfony -f src/Symfony/Component/Ldap/Tests/Fixtures/data/base.ldif
6463
- ldapadd -h localhost:3389 -D cn=admin,dc=symfony,dc=com -w symfony -f src/Symfony/Component/Ldap/Tests/Fixtures/data/fixtures.ldif
6564

@@ -69,18 +68,20 @@ install:
6968
- if [[ $deps != skip && $deps ]]; then php .travis.php $TRAVIS_COMMIT_RANGE $TRAVIS_BRANCH $COMPONENTS; fi;
7069
# For the master branch when deps=high, the version before master is checked out and tested with the locally patched components
7170
- if [[ $deps = high && $TRAVIS_BRANCH = master ]]; then SYMFONY_VERSION=$(git ls-remote --heads | grep -o '/[1-9].*' | tail -n 1 | sed s/.//); else SYMFONY_VERSION=$(cat composer.json | grep '^ *"dev-master". *"[1-9]' | grep -o '[0-9.]*'); fi;
72-
- if [[ $deps = high && $TRAVIS_BRANCH = master ]]; then git fetch origin $SYMFONY_VERSION; git checkout -m FETCH_HEAD; COMPONENTS=$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -printf '%h\n'); fi;
71+
- if [[ $deps = high && $TRAVIS_BRANCH = master ]]; then git fetch origin $SYMFONY_VERSION; git checkout -m FETCH_HEAD; COMPONENTS=$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -printf '%h\n'); ./phpunit install; fi;
7372
# Legacy tests are skipped when deps=high and when the current branch version has not the same major version number than the next one
7473
- 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;
7574
- export COMPOSER_ROOT_VERSION=$SYMFONY_VERSION.x-dev;
7675
- if [[ ! $deps ]]; then composer update --prefer-dist; else export SYMFONY_DEPRECATIONS_HELPER=weak; fi;
76+
- if [[ $TRAVIS_BRANCH = master ]]; then export SYMFONY_PHPUNIT_OVERLOAD=1; fi;
7777
- if [[ $TRAVIS_PHP_VERSION != hhvm ]]; then php -i; else hhvm --php -r 'print_r($_SERVER);print_r(ini_get_all());'; fi;
7878

7979
script:
8080
- if [[ ! $deps ]]; then echo "$COMPONENTS" | parallel --gnu '$PHPUNIT --exclude-group tty,benchmark,intl-data {}'; fi;
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;
8584
- 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;
85+
# Test the PhpUnit bridge using the original phpunit script
86+
- if [[ $deps = low ]]; then (cd src/Symfony/Bridge/PhpUnit && phpenv global 5.3 && php --version && composer update --prefer-dist && phpunit); fi;
8687
- 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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
// Please update when phpunit needs to be reinstalled with fresh deps:
14-
// Cache-Id-Version: 2016-03-16 15:36 UTC
14+
// Cache-Id-Version: 2016-03-22 17:23 UTC
1515

1616
use Symfony\Component\Process\ProcessUtils;
1717

@@ -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: 34 additions & 12 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,15 +67,16 @@ 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

60-
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
75+
$trace = debug_backtrace(true);
76+
$group = 'other';
6177

6278
$i = count($trace);
63-
while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) {
79+
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_')))) {
6480
// No-op
6581
}
6682

@@ -95,14 +111,13 @@ public static function register($mode = 0)
95111

96112
exit(1);
97113
}
98-
if ('legacy' !== $group && self::MODE_WEAK !== $mode) {
114+
if ('legacy' !== $group && DeprecationErrorHandler::MODE_WEAK !== $mode) {
99115
$ref = &$deprecations[$group][$msg]['count'];
100116
++$ref;
101117
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
102118
++$ref;
103119
}
104-
} else {
105-
$group = 'other';
120+
} elseif (DeprecationErrorHandler::MODE_WEAK !== $mode) {
106121
$ref = &$deprecations[$group][$msg]['count'];
107122
++$ref;
108123
}
@@ -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: 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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Test DeprecationErrorHandler in weak mode
3+
--FILE--
4+
<?php
5+
6+
putenv('SYMFONY_DEPRECATIONS_HELPER=disabled');
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+
echo (int) set_error_handler('var_dump');
20+
echo (int) class_exists('Symfony\Bridge\PhpUnit\DeprecationErrorHandler', false);
21+
22+
?>
23+
--EXPECTF--
24+
00
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=/foo/');
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+
Legacy deprecation triggered by FooTestCase::testLegacyFoo:
36+
silenced foo deprecation
37+
Stack trace:
38+
#%A(%d): FooTestCase->testLegacyFoo()
39+
#%d {main}
40+
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+

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,24 @@ 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 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+
}
2646
}

0 commit comments

Comments
 (0)