[Bridge\PhpUnit] Relax silencing rule and fix phpunit runtime detection
This commit is contained in:
parent
045433c7c4
commit
8dc9be3db0
@ -33,7 +33,7 @@ class DeprecationErrorHandler
|
|||||||
'legacy' => array(),
|
'legacy' => array(),
|
||||||
'other' => array(),
|
'other' => array(),
|
||||||
);
|
);
|
||||||
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations) {
|
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $mode) {
|
||||||
if (E_USER_DEPRECATED !== $type) {
|
if (E_USER_DEPRECATED !== $type) {
|
||||||
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
|
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
|
||||||
}
|
}
|
||||||
@ -51,10 +51,7 @@ class DeprecationErrorHandler
|
|||||||
|
|
||||||
$group = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\Legacy') || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true) ? 'legacy' : 'remaining';
|
$group = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\Legacy') || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true) ? 'legacy' : 'remaining';
|
||||||
|
|
||||||
if ('legacy' === $group && 0 === (error_reporting() & E_USER_DEPRECATED)) {
|
if ('legacy' !== $group && 'weak' !== $mode) {
|
||||||
$ref =& $deprecations[$group]['Silenced']['count'];
|
|
||||||
++$ref;
|
|
||||||
} else {
|
|
||||||
$ref =& $deprecations[$group][$msg]['count'];
|
$ref =& $deprecations[$group][$msg]['count'];
|
||||||
++$ref;
|
++$ref;
|
||||||
$ref =& $deprecations[$group][$msg][$class.'::'.$method];
|
$ref =& $deprecations[$group][$msg][$class.'::'.$method];
|
||||||
@ -99,7 +96,7 @@ class DeprecationErrorHandler
|
|||||||
};
|
};
|
||||||
|
|
||||||
foreach (array('remaining', 'legacy', 'other') as $group) {
|
foreach (array('remaining', 'legacy', 'other') as $group) {
|
||||||
if ($deprecations[$group]) {
|
if ($deprecations[$group.'Count']) {
|
||||||
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";
|
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";
|
||||||
|
|
||||||
uasort($deprecations[$group], $cmp);
|
uasort($deprecations[$group], $cmp);
|
||||||
@ -120,13 +117,8 @@ class DeprecationErrorHandler
|
|||||||
if (!empty($notices)) {
|
if (!empty($notices)) {
|
||||||
echo "\n";
|
echo "\n";
|
||||||
}
|
}
|
||||||
if ('weak' !== $mode) {
|
if ('weak' !== $mode && ($deprecations['remaining'] || $deprecations['other'])) {
|
||||||
if ($deprecations['remaining'] || $deprecations['other']) {
|
exit(1);
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if ('strict' === $mode && $deprecations['legacy'] && $deprecations['legacyCount'] !== $ref =& $deprecations['legacy']['Silenced']['count']) {
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -9,19 +9,13 @@ It comes with the following features:
|
|||||||
* auto-register `class_exists` to load Doctrine annotations;
|
* auto-register `class_exists` to load Doctrine annotations;
|
||||||
* print a user deprecation notices summary at the end of the test suite.
|
* print a user deprecation notices summary at the end of the test suite.
|
||||||
|
|
||||||
Handling user deprecation notices is sensitive to the SYMFONY_DEPRECATIONS_HELPER
|
By default any non-legacy-tagged deprecation notice will make tests fail.
|
||||||
environment variable. This env var configures 3 behaviors depending on its value:
|
This can be changed by setting the SYMFONY_DEPRECATIONS_HELPER environment
|
||||||
|
variable to `weak`. This will make the bridge ignore deprecation notices and
|
||||||
|
is useful to projects that must use deprecated interfaces for backward
|
||||||
|
compatibility reasons.
|
||||||
|
|
||||||
* when set to `strict`, all but silenced-legacy-tagged deprecation notices will
|
A summary of deprecation notices is displayed at the end of the test suite:
|
||||||
make tests fail. This is the recommended mode for best forward compatibility
|
|
||||||
* `weak` on the contrary will make tests ignore all deprecation notices.
|
|
||||||
This is the recommended mode for legacy projects that must use deprecated
|
|
||||||
interfaces for backward compatibility reasons.
|
|
||||||
* with any other value, all but silenced-or-not-legacy-tagged deprecation
|
|
||||||
notices will make tests fail.
|
|
||||||
|
|
||||||
All three modes will display a summary of deprecation notices at the end of the
|
|
||||||
test suite, split in two groups:
|
|
||||||
|
|
||||||
* **Legacy** deprecation notices denote tests that explicitly test some legacy
|
* **Legacy** deprecation notices denote tests that explicitly test some legacy
|
||||||
interfaces. There are four ways to mark a test as legacy:
|
interfaces. There are four ways to mark a test as legacy:
|
||||||
@ -36,8 +30,7 @@ Usage
|
|||||||
-----
|
-----
|
||||||
|
|
||||||
Add this bridge to the `require-dev` section of your composer.json file
|
Add this bridge to the `require-dev` section of your composer.json file
|
||||||
(not in `require`) with e.g.
|
(not in `require`) with e.g. `composer require --dev "symfony/phpunit-bridge"`.
|
||||||
`composer require --dev "symfony/phpunit-bridge"`.
|
|
||||||
|
|
||||||
When running `phpunit`, you will see a summary of deprecation notices at the end
|
When running `phpunit`, you will see a summary of deprecation notices at the end
|
||||||
of the test suite.
|
of the test suite.
|
||||||
@ -48,15 +41,3 @@ You have to decide either to:
|
|||||||
* update your code to not use deprecated interfaces anymore, thus gaining better
|
* update your code to not use deprecated interfaces anymore, thus gaining better
|
||||||
forward compatibility;
|
forward compatibility;
|
||||||
* or move them to the **Legacy** section (by using one of the above way).
|
* or move them to the **Legacy** section (by using one of the above way).
|
||||||
|
|
||||||
After reviewing them, you should silence deprecations in the **Legacy** section
|
|
||||||
if you think they are triggered by tests dedicated to testing deprecated
|
|
||||||
interfaces. To do so, add the following line at the beginning of your legacy
|
|
||||||
test case or in the `setUp()` method of your legacy test class:
|
|
||||||
`$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);`
|
|
||||||
|
|
||||||
Last but not least, you should then configure your C.I. to the reporting mode
|
|
||||||
that is appropriated to your project by setting SYMFONY_DEPRECATIONS_HELPER to
|
|
||||||
`strict`, `weak` or empty. It is recommended to start with `weak` mode, upgrade
|
|
||||||
your code as described above, then when the *Remaining/Other* sections are empty,
|
|
||||||
move to `strict` to keep forward compatibility on the long run.
|
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
use Doctrine\Common\Annotations\AnnotationRegistry;
|
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||||
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
|
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
|
||||||
|
|
||||||
if (!class_exists('PHPUnit_Util_ErrorHandler')) {
|
// Detect if we're loaded by an actual run of phpunit
|
||||||
|
if (!class_exists('PHPUnit_TextUI_Command', false)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user