Skip to content

Commit

Permalink
feature #13398 [PhpUnit] new PhpUnit bridge (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.7 branch.

Discussion
----------

[PhpUnit] new PhpUnit bridge

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13644
| License       | MIT
| Doc PR        | -

From the proposed README:

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.

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
`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.

Commits
-------

acac734 [PhpUnitBridge] new bridge for testing with PHPUnit
  • Loading branch information
fabpot committed Feb 21, 2015
2 parents a630d87 + acac734 commit 9a4f3e1
Show file tree
Hide file tree
Showing 100 changed files with 372 additions and 227 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -7,7 +7,7 @@ matrix:
- php: 5.5
- php: 5.6
- php: 5.3
env: components=low
env: components=low SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.6
env: components=high
- php: hhvm-nightly
Expand All @@ -20,6 +20,7 @@ services: mongodb
env:
global:
- components=no
- SYMFONY_DEPRECATIONS_HELPER=strict

before_install:
- travis_retry sudo apt-get install parallel
Expand All @@ -40,5 +41,5 @@ install:
script:
- 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;
- if [ "$components" = "no" ]; then echo -e "\\nRunning tests requiring tty"; phpunit --group tty || (echo -e "\\e[41mKO\\e[0m tty group" && $(exit 1)); fi;
- 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 || (echo -e "\\e[41mKO\\e[0m {}" && $(exit 1));'; fi;
- 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;
- 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;
4 changes: 4 additions & 0 deletions composer.json
Expand Up @@ -45,6 +45,7 @@
"symfony/locale": "self.version",
"symfony/monolog-bridge": "self.version",
"symfony/options-resolver": "self.version",
"symfony/phpunit-bridge": "self.version",
"symfony/process": "self.version",
"symfony/property-access": "self.version",
"symfony/proxy-manager-bridge": "self.version",
Expand Down Expand Up @@ -86,6 +87,9 @@
],
"files": [ "src/Symfony/Component/Intl/Resources/stubs/functions.php" ]
},
"autoload-dev": {
"files": [ "src/Symfony/Bridge/PhpUnit/bootstrap.php" ]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
Expand Down
13 changes: 5 additions & 8 deletions phpunit.xml.dist
Expand Up @@ -4,16 +4,13 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="autoload.php.dist"
bootstrap="vendor/autoload.php"
>
<php>
<!-- Disable E_USER_DEPRECATED until 3.0 -->
<!-- php -r 'echo -1 & ~E_USER_DEPRECATED;' -->
<ini name="error_reporting" value="-16385"/>

<ini name="intl.default_locale" value="en"/>
<ini name="intl.error_level" value="0"/>
<ini name="memory_limit" value="-1"/>
<ini name="error_reporting" value="-1" />
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
</php>

<testsuites>
Expand Down
14 changes: 0 additions & 14 deletions src/Symfony/Bridge/Doctrine/Tests/bootstrap.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/composer.json
Expand Up @@ -20,6 +20,7 @@
"doctrine/common": "~2.3"
},
"require-dev": {
"symfony/phpunit-bridge": "~2.7|~3.0.0",
"symfony/stopwatch": "~2.2|~3.0.0",
"symfony/dependency-injection": "~2.2|~3.0.0",
"symfony/form": "~2.7|~3.0.0",
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Bridge/Doctrine/phpunit.xml.dist
Expand Up @@ -4,12 +4,10 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="Tests/bootstrap.php"
bootstrap="vendor/autoload.php"
>
<php>
<!-- Disable E_USER_DEPRECATED until 3.0 -->
<!-- php -r 'echo -1 & ~E_USER_DEPRECATED;' -->
<ini name="error_reporting" value="-16385"/>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Monolog/composer.json
Expand Up @@ -20,6 +20,7 @@
"monolog/monolog": "~1.11"
},
"require-dev": {
"symfony/phpunit-bridge": "~2.7|~3.0.0",
"symfony/http-kernel": "~2.4|~3.0.0",
"symfony/console": "~2.4|~3.0.0",
"symfony/event-dispatcher": "~2.2|~3.0.0"
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Bridge/Monolog/phpunit.xml.dist
Expand Up @@ -7,9 +7,7 @@
bootstrap="vendor/autoload.php"
>
<php>
<!-- Disable E_USER_DEPRECATED until 3.0 -->
<!-- php -r 'echo -1 & ~E_USER_DEPRECATED;' -->
<ini name="error_reporting" value="-16385"/>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bridge/PhpUnit/.gitignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
109 changes: 63 additions & 46 deletions autoload.php.dist → ...ridge/PhpUnit/DeprecationErrorHandler.php
@@ -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 <fabien@symfony.com>
*
* 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 <p@tchwork.com>
*/
class DeprecationErrorHandler
{
private static $isRegistered = false;

public static function register()
public static function register($strict = false)
{
if (self::$isRegistered) {
return;
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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);
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Bridge/PhpUnit/LICENSE
@@ -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.
62 changes: 62 additions & 0 deletions src/Symfony/Bridge/PhpUnit/README.md
@@ -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.

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
`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.

0 comments on commit 9a4f3e1

Please sign in to comment.