Skip to content

Commit 4fd9792

Browse files
committed
minor #13758 [PhpUnitBridge] Tweak and fix deprecation notices (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [PhpUnitBridge] Tweak and fix deprecation notices | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 4ef3610 [PhpUnitBridge] Tweak and fix deprecation notices
2 parents 346f347 + 4ef3610 commit 4fd9792

File tree

15 files changed

+76
-44
lines changed

15 files changed

+76
-44
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ matrix:
77
- php: 5.5
88
- php: 5.6
99
- php: 5.3
10-
env: components=low SYMFONY_DEPRECATIONS_HELPER=weak
10+
env: components=low
1111
- php: 5.6
1212
env: components=high
1313
- php: hhvm-nightly
@@ -41,5 +41,6 @@ install:
4141
script:
4242
- if [ "$components" = "no" ]; then ls -d src/Symfony/*/* | parallel --gnu --keep-order 'echo -e "\\nRunning {} tests"; phpunit --exclude-group tty,benchmark,intl-data {} || (echo -e "\\e[41mKO\\e[0m {}" && $(exit 1));'; fi;
4343
- if [ "$components" = "no" ]; then echo -e "\\nRunning tests requiring tty"; phpunit --group tty || (echo -e "\\e[41mKO\\e[0m tty group" && $(exit 1)); fi;
44+
- if [ "$components" != "no" ]; then export SYMFONY_DEPRECATIONS_HELPER=weak; fi;
4445
- if [ "$components" = "high" ]; then find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist | sed 's#\(.*\)/.*#\1#' | parallel --gnu --keep-order -j25% 'echo -e "\\nRunning {} tests"; cd {}; composer --prefer-source update; phpunit --exclude-group tty,benchmark,intl-data,legacy || (echo -e "\\e[41mKO\\e[0m {}" && $(exit 1));'; fi;
4546
- if [ "$components" = "low" ]; then find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist | sed 's#\(.*\)/.*#\1#' | parallel --gnu --keep-order -j25% 'echo -e "\\nRunning {} tests"; cd {}; composer --prefer-source --prefer-lowest --prefer-stable update; phpunit --exclude-group tty,benchmark,intl-data || (echo -e "\\e[41mKO\\e[0m {}" && $(exit 1));'; fi;

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DeprecationErrorHandler
2020
{
2121
private static $isRegistered = false;
2222

23-
public static function register($strict = false)
23+
public static function register($mode = false)
2424
{
2525
if (self::$isRegistered) {
2626
return;
@@ -33,7 +33,7 @@ public static function register($strict = false)
3333
'legacy' => array(),
3434
'other' => array(),
3535
);
36-
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $strict) {
36+
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations) {
3737
if (E_USER_DEPRECATED !== $type) {
3838
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
3939
}
@@ -66,29 +66,14 @@ public static function register($strict = false)
6666
++$ref;
6767
}
6868
++$deprecations[$group.'Count'];
69-
unset($trace, $ref);
70-
71-
if ('legacy' !== $group) {
72-
try {
73-
$e = $strict ? error_reporting(-1) : error_reporting();
74-
$result = \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
75-
error_reporting($e);
76-
} catch (\Exception $x) {
77-
error_reporting($e);
78-
79-
throw $x;
80-
}
81-
82-
return $result;
83-
}
8469
};
8570
$oldErrorHandler = set_error_handler($deprecationHandler);
8671

8772
if (null !== $oldErrorHandler) {
8873
restore_error_handler();
8974
if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) {
9075
restore_error_handler();
91-
self::register();
76+
self::register($mode);
9277
}
9378
} else {
9479
self::$isRegistered = true;
@@ -101,7 +86,7 @@ public static function register($strict = false)
10186
} else {
10287
$colorize = function ($str) {return $str;};
10388
}
104-
register_shutdown_function(function () use (&$deprecations, $deprecationHandler, $colorize) {
89+
register_shutdown_function(function () use ($mode, &$deprecations, $deprecationHandler, $colorize) {
10590
$currErrorHandler = set_error_handler('var_dump');
10691
restore_error_handler();
10792

@@ -135,6 +120,14 @@ public static function register($strict = false)
135120
if (!empty($notices)) {
136121
echo "\n";
137122
}
123+
if ('weak' !== $mode) {
124+
if ($deprecations['remaining'] || $deprecations['other']) {
125+
exit(1);
126+
}
127+
if ('strict' === $mode && $deprecations['legacy'] && $deprecations['legacyCount'] !== $ref =& $deprecations['legacy']['Silenced']['count']) {
128+
exit(1);
129+
}
130+
}
138131
});
139132
}
140133
}

src/Symfony/Bridge/PhpUnit/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ It comes with the following features:
1212
Handling user deprecation notices is sensitive to the SYMFONY_DEPRECATIONS_HELPER
1313
environment variable. This env var configures 3 behaviors depending on its value:
1414

15-
* when set to `strict`, all but legacy-tagged deprecation notices will make tests
16-
fail. This is the recommended mode for best forward compatibility.
15+
* when set to `strict`, all but silenced-legacy-tagged deprecation notices will
16+
make tests fail. This is the recommended mode for best forward compatibility
1717
* `weak` on the contrary will make tests ignore all deprecation notices.
1818
This is the recommended mode for legacy projects that must use deprecated
1919
interfaces for backward compatibility reasons.
20-
* any other value will respect the current error reporting level.
20+
* with any other value, all but silenced-or-not-legacy-tagged deprecation
21+
notices will make tests fail.
2122

2223
All three modes will display a summary of deprecation notices at the end of the
2324
test suite, split in two groups:
2425

2526
* **Legacy** deprecation notices denote tests that explicitly test some legacy
26-
interfaces. In all 3 modes, deprecation notices triggered in a legacy-tagged
27-
test do never make a test fail. There are four ways to mark a test as legacy:
27+
interfaces. There are four ways to mark a test as legacy:
2828
- make its class start with the `Legacy` prefix;
2929
- make its method start with `testLegacy`;
3030
- make its data provider start with `provideLegacy` or `getLegacy`;

src/Symfony/Bridge/PhpUnit/bootstrap.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,4 @@
1717
AnnotationRegistry::registerLoader('class_exists');
1818
}
1919

20-
switch (getenv('SYMFONY_DEPRECATIONS_HELPER')) {
21-
case 'strict':
22-
DeprecationErrorHandler::register(true);
23-
break;
24-
25-
case 'weak':
26-
error_reporting(error_reporting() & ~E_USER_DEPRECATED);
27-
// No break;
28-
default:
29-
DeprecationErrorHandler::register(false);
30-
break;
31-
}
20+
DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER'));

src/Symfony/Bridge/PhpUnit/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"require": {
1919
"php": ">=5.3.9"
2020
},
21+
"suggest": {
22+
"symfony/debug": "For tracking deprecated interfaces usages at runtime with DebugClassLoader"
23+
},
2124
"autoload": {
2225
"files": [ "bootstrap.php" ],
2326
"psr-0": { "Symfony\\Bridge\\PhpUnit\\": "" }

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"require-dev": {
2323
"symfony/phpunit-bridge": "~2.7|~3.0.0",
24+
"symfony/asset": "~2.7|~3.0.0",
2425
"symfony/finder": "~2.3|~3.0.0",
2526
"symfony/form": "~2.6|~3.0.0",
2627
"symfony/http-kernel": "~2.3|~3.0.0",

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/LegacyTemplatingAssetHelperPassTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
class LegacyTemplatingAssetHelperPassTest extends \PHPUnit_Framework_TestCase
2020
{
21+
public function setUp()
22+
{
23+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
24+
}
25+
2126
public function getScopesTests()
2227
{
2328
return array(

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ public function testInvalidValueTrustedProxies()
9090
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
9191
* @expectedExceptionMessage You cannot use assets settings under "framework.templating" and "assets" configurations in the same project.
9292
*/
93-
public function testInvalidValueAssets()
93+
public function testLegacyInvalidValueAssets()
9494
{
95+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
96+
9597
$processor = new Processor();
9698
$configuration = new Configuration(true);
9799
$processor->processConfiguration($configuration, array(

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ public function testLegacyFormCsrfFieldNameCanBeSetUnderCsrfSettings()
471471

472472
public function testLegacyFormCsrfFieldNameUnderFormSettingsTakesPrecedence()
473473
{
474+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
475+
474476
$container = $this->createContainerFromFile('form_csrf_under_form_sets_field_name');
475477

476478
$this->assertTrue($container->getParameter('form.type_extension.csrf.enabled'));

src/Symfony/Bundle/TwigBundle/Tests/Extension/LegacyAssetsExtensionTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
use Symfony\Bundle\TwigBundle\Tests\TestCase;
1616
use Symfony\Component\Routing\RequestContext;
1717

18+
/**
19+
* @group legacy
20+
*/
1821
class LegacyAssetsExtensionTest extends TestCase
1922
{
23+
public function setUp()
24+
{
25+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
26+
}
27+
2028
/**
21-
* @dataProvider provideGetGetAssetUrlArguments
29+
* @dataProvider provideGetAssetUrlArguments
2230
*/
2331
public function testGetAssetUrl($path, $packageName, $absolute, $relativeUrl, $expectedUrl, $scheme, $host, $httpPort, $httpsPort)
2432
{
@@ -46,7 +54,7 @@ public function testGetAssetWithoutHost()
4654
$this->assertEquals($relativeUrl, $extension->getAssetUrl($path, $packageName, true));
4755
}
4856

49-
public function provideGetGetAssetUrlArguments()
57+
public function provideGetAssetUrlArguments()
5058
{
5159
return array(
5260
array('/path/to/asset', 'package-name', false, '/bundle-name/path/to/asset', '/bundle-name/path/to/asset', 'http', 'symfony.com', 80, null),

src/Symfony/Component/Debug/Tests/FatalErrorHandler/ClassNotFoundFatalErrorHandlerTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ public function testHandleClassNotFound($error, $translatedMessage)
3535
}
3636

3737
/**
38+
* @group legacy
3839
* @dataProvider provideLegacyClassNotFoundData
3940
*/
4041
public function testLegacyHandleClassNotFound($error, $translatedMessage, $autoloader)
4142
{
42-
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
43-
4443
// Unregister all autoloaders to ensure the custom provided
4544
// autoloader is the only one to be used during the test run.
4645
$autoloaders = spl_autoload_functions();
@@ -114,13 +113,19 @@ public function provideClassNotFoundData()
114113

115114
public function provideLegacyClassNotFoundData()
116115
{
116+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
117+
117118
$prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
118119

119120
$symfonyAutoloader = new SymfonyClassLoader();
120121
$symfonyAutoloader->addPrefixes($prefixes);
121122

122-
$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
123-
$symfonyUniversalClassLoader->registerPrefixes($prefixes);
123+
if (class_exists('Symfony\Component\ClassLoader\UniversalClassLoader')) {
124+
$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
125+
$symfonyUniversalClassLoader->registerPrefixes($prefixes);
126+
} else {
127+
$symfonyUniversalClassLoader = $symfonyAutoloader;
128+
}
124129

125130
return array(
126131
array(

src/Symfony/Component/DependencyInjection/Tests/Dumper/GraphvizDumperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static function setUpBeforeClass()
2525

2626
public function testLegacyDump()
2727
{
28+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
29+
2830
$container = include self::$fixturesPath.'/containers/legacy-container9.php';
2931
$dumper = new GraphvizDumper($container);
3032
$this->assertEquals(str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/legacy-services9.dot')), $dumper->dump(), '->dump() dumps services');

src/Symfony/Component/DependencyInjection/Tests/LegacyDefinitionTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
class LegacyDefinitionTest extends \PHPUnit_Framework_TestCase
1717
{
18+
public function setUp()
19+
{
20+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
21+
}
22+
1823
public function testSetGetFactoryClass()
1924
{
2025
$def = new Definition('stdClass');

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ public function testLoadImports()
122122

123123
public function testLegacyLoadServices()
124124
{
125+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
126+
125127
$container = new ContainerBuilder();
126128
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
127129
$loader->load('legacy-services6.yml');

src/Symfony/Component/Validator/Tests/Constraints/CallbackValidatorTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ public function testArrayCallableExplicitName()
188188
// BC with Symfony < 2.4
189189
public function testLegacySingleMethodBc()
190190
{
191+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
192+
191193
$object = new CallbackValidatorTest_Object();
192194
$constraint = new Callback(array('validate'));
193195

@@ -201,6 +203,8 @@ public function testLegacySingleMethodBc()
201203
// BC with Symfony < 2.4
202204
public function testLegacySingleMethodBcExplicitName()
203205
{
206+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
207+
204208
$object = new CallbackValidatorTest_Object();
205209
$constraint = new Callback(array('methods' => array('validate')));
206210

@@ -214,6 +218,8 @@ public function testLegacySingleMethodBcExplicitName()
214218
// BC with Symfony < 2.4
215219
public function testLegacyMultipleMethodsBc()
216220
{
221+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
222+
217223
$object = new CallbackValidatorTest_Object();
218224
$constraint = new Callback(array('validate', 'validateStatic'));
219225

@@ -229,6 +235,8 @@ public function testLegacyMultipleMethodsBc()
229235
// BC with Symfony < 2.4
230236
public function testLegacyMultipleMethodsBcExplicitName()
231237
{
238+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
239+
232240
$object = new CallbackValidatorTest_Object();
233241
$constraint = new Callback(array(
234242
'methods' => array('validate', 'validateStatic'),
@@ -246,6 +254,8 @@ public function testLegacyMultipleMethodsBcExplicitName()
246254
// BC with Symfony < 2.4
247255
public function testLegacySingleStaticMethodBc()
248256
{
257+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
258+
249259
$object = new CallbackValidatorTest_Object();
250260
$constraint = new Callback(array(
251261
array(__CLASS__.'_Class', 'validateCallback'),
@@ -261,6 +271,8 @@ public function testLegacySingleStaticMethodBc()
261271
// BC with Symfony < 2.4
262272
public function testLegacySingleStaticMethodBcExplicitName()
263273
{
274+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
275+
264276
$object = new CallbackValidatorTest_Object();
265277
$constraint = new Callback(array(
266278
'methods' => array(array(__CLASS__.'_Class', 'validateCallback')),
@@ -298,6 +310,8 @@ public function testExpectValidCallbacks()
298310
*/
299311
public function testLegacyExpectEitherCallbackOrMethods()
300312
{
313+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
314+
301315
$object = new CallbackValidatorTest_Object();
302316

303317
$this->validator->validate($object, new Callback(array(

0 commit comments

Comments
 (0)