-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PhpUnit] new PhpUnit bridge #13398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[PhpUnit] new PhpUnit bridge #13398
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
<?php | ||
|
||
if (PHP_VERSION_ID >= 50400 && gc_enabled()) { | ||
// Disabling Zend Garbage Collection to prevent segfaults with PHP5.4+ | ||
// https://bugs.php.net/bug.php?id=53976 | ||
gc_disable(); | ||
} | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\PhpUnit; | ||
|
||
/** | ||
* Catch deprecation notices and print a summary report at the end of the test suite | ||
* | ||
* @internal | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
class DeprecationErrorHandler | ||
{ | ||
private static $isRegistered = false; | ||
|
||
public static function register() | ||
public static function register($strict = false) | ||
{ | ||
if (self::$isRegistered) { | ||
return; | ||
|
@@ -28,9 +33,9 @@ public static function register() | |
'legacy' => array(), | ||
'other' => array(), | ||
); | ||
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations) { | ||
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $strict) { | ||
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); | ||
} | ||
|
||
$trace = debug_backtrace(PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT : true); | ||
|
@@ -44,19 +49,38 @@ public static function register() | |
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class']; | ||
$method = $trace[$i]['function']; | ||
|
||
$type = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\Legacy') ? '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' === $type && 0 === (error_reporting() & E_USER_DEPRECATED)) { | ||
@++$deprecations[$type]['Silenced']['count']; | ||
if ('legacy' === $group && 0 === (error_reporting() & E_USER_DEPRECATED)) { | ||
$ref =& $deprecations[$group]['Silenced']['count']; | ||
++$ref; | ||
} else { | ||
@++$deprecations[$type][$msg]['count']; | ||
@++$deprecations[$type][$msg][$class.'::'.$method]; | ||
$ref =& $deprecations[$group][$msg]['count']; | ||
++$ref; | ||
$ref =& $deprecations[$group][$msg][$class.'::'.$method]; | ||
++$ref; | ||
} | ||
} else { | ||
$type = 'other'; | ||
@++$deprecations[$type][$msg]['count']; | ||
$group = 'other'; | ||
$ref =& $deprecations[$group][$msg]['count']; | ||
++$ref; | ||
} | ||
++$deprecations[$group.'Count']; | ||
unset($trace, $ref); | ||
|
||
if ('legacy' !== $group) { | ||
try { | ||
$e = $strict ? error_reporting(-1) : error_reporting(); | ||
$result = \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context); | ||
error_reporting($e); | ||
} catch (\Exception $x) { | ||
error_reporting($e); | ||
|
||
throw $x; | ||
} | ||
|
||
return $result; | ||
} | ||
++$deprecations[$type.'Count']; | ||
}; | ||
$oldErrorHandler = set_error_handler($deprecationHandler); | ||
|
||
|
@@ -68,20 +92,16 @@ public static function register() | |
} | ||
} else { | ||
self::$isRegistered = true; | ||
register_shutdown_function(function () use (&$deprecations, $deprecationHandler) { | ||
|
||
$colorize = new \SebastianBergmann\Environment\Console(); | ||
|
||
if ($colorize->hasColorSupport()) { | ||
$colorize = function ($str, $red) { | ||
$color = $red ? '41;37' : '43;30'; | ||
|
||
return "\x1B[{$color}m{$str}\x1B[0m"; | ||
}; | ||
} else { | ||
$colorize = function ($str) {return $str;}; | ||
} | ||
if (self::hasColorSupport()) { | ||
$colorize = function ($str, $red) { | ||
$color = $red ? '41;37' : '43;30'; | ||
|
||
return "\x1B[{$color}m{$str}\x1B[0m"; | ||
}; | ||
} else { | ||
$colorize = function ($str) {return $str;}; | ||
} | ||
register_shutdown_function(function () use (&$deprecations, $deprecationHandler, $colorize) { | ||
$currErrorHandler = set_error_handler('var_dump'); | ||
restore_error_handler(); | ||
|
||
|
@@ -93,13 +113,13 @@ public static function register() | |
return $b['count'] - $a['count']; | ||
}; | ||
|
||
foreach (array('remaining', 'legacy', 'other') as $type) { | ||
if ($deprecations[$type]) { | ||
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($type), $deprecations[$type.'Count']), 'legacy' !== $type), "\n"; | ||
foreach (array('remaining', 'legacy', 'other') as $group) { | ||
if ($deprecations[$group]) { | ||
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n"; | ||
|
||
uasort($deprecations[$type], $cmp); | ||
uasort($deprecations[$group], $cmp); | ||
|
||
foreach ($deprecations[$type] as $msg => $notices) { | ||
foreach ($deprecations[$group] as $msg => $notices) { | ||
echo "\n", $msg, ': ', $notices['count'], "x\n"; | ||
|
||
arsort($notices); | ||
|
@@ -118,16 +138,13 @@ public static function register() | |
}); | ||
} | ||
} | ||
} | ||
|
||
if (class_exists('PHPUnit_Util_ErrorHandler')) { | ||
DeprecationErrorHandler::register(); | ||
} | ||
|
||
$loader = require_once __DIR__.'/vendor/autoload.php'; | ||
|
||
use Doctrine\Common\Annotations\AnnotationRegistry; | ||
|
||
AnnotationRegistry::registerLoader(array($loader, 'loadClass')); | ||
private static function hasColorSupport() | ||
{ | ||
if ('\\' === DIRECTORY_SEPARATOR) { | ||
return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'); | ||
} | ||
|
||
return $loader; | ||
return defined('STDOUT') && function_exists('posix_isatty') && @posix_isatty(STDOUT); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2014-2015 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
PHPUnit Bridge | ||
============== | ||
|
||
Provides utilities for PHPUnit, especially user deprecation notices management. | ||
|
||
It comes with the following features: | ||
|
||
* disable the garbage collector; | ||
* auto-register `class_exists` to load Doctrine annotations; | ||
* print a user deprecation notices summary at the end of the test suite. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test suite output? |
||
|
||
Handling user deprecation notices is sensitive to the SYMFONY_DEPRECATIONS_HELPER | ||
environment variable. This env var configures 3 behaviors depending on its value: | ||
|
||
* when set to `strict`, all but legacy-tagged deprecation notices will 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. | ||
* any other value will respect the current error reporting level. | ||
|
||
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 | ||
interfaces. In all 3 modes, deprecation notices triggered in a legacy-tagged | ||
test do never make a test fail. There are four ways to mark a test as legacy: | ||
- make its class start with the `Legacy` prefix; | ||
- make its method start with `testLegacy`; | ||
- make its data provider start with `provideLegacy` or `getLegacy`; | ||
- add the `@group legacy` annotation to its class or method. | ||
* **Remaining/Other** deprecation notices are all other (non-legacy) | ||
notices, grouped by message, test class and method. | ||
|
||
Usage | ||
----- | ||
|
||
Add this bridge to the `require-dev` section of your composer.json file | ||
(not in `require`) with e.g. | ||
`composer require --dev "symfony/phpunit-bridge"`. | ||
|
||
When running `phpunit`, you will see a summary of deprecation notices at the end | ||
of the test suite. | ||
|
||
Deprecation notices in the **Remaining/Other** section need some thought. | ||
You have to decide either to: | ||
|
||
* update your code to not use deprecated interfaces anymore, thus gaining better | ||
forward compatibility; | ||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. appropriate -d |
||
`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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provides utilities for PHPUnit like deprecation notices management.
And I would remove everything else. The
require-dev
section sentence should be move in another paragraph that explains how to use the bridge in a project.