Skip to content

[PhpUnitBridge] Enforce @-silencing of deprecation notices according to new policy #15031

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 1 commit into from
Jun 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Bridge\PhpUnit;

/**
* Catch deprecation notices and print a summary report at the end of the test suite
* Catch deprecation notices and print a summary report at the end of the test suite.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
Expand All @@ -26,9 +26,11 @@ public static function register($mode = false)
return;
}
$deprecations = array(
'unsilencedCount' => 0,
'remainingCount' => 0,
'legacyCount' => 0,
'otherCount' => 0,
'unsilenced' => array(),
'remaining' => array(),
'legacy' => array(),
'other' => array(),
Expand All @@ -45,21 +47,25 @@ public static function register($mode = false)
// No-op
}

if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
if (0 !== error_reporting()) {
$group = 'unsilenced';
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
} elseif (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
$class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
$method = $trace[$i]['function'];

$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 && 'weak' !== $mode) {
$ref =& $deprecations[$group][$msg]['count'];
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
$ref =& $deprecations[$group][$msg][$class.'::'.$method];
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
++$ref;
}
} else {
$group = 'other';
$ref =& $deprecations[$group][$msg]['count'];
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
}
++$deprecations[$group.'Count'];
Expand Down Expand Up @@ -95,7 +101,7 @@ public static function register($mode = false)
return $b['count'] - $a['count'];
};

foreach (array('remaining', 'legacy', 'other') as $group) {
foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
if ($deprecations[$group.'Count']) {
echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";

Expand All @@ -117,7 +123,7 @@ public static function register($mode = false)
if (!empty($notices)) {
echo "\n";
}
if ('weak' !== $mode && ($deprecations['remaining'] || $deprecations['other'])) {
if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
exit(1);
}
});
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Bridge/PhpUnit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ It comes with the following features:
* auto-register `class_exists` to load Doctrine annotations;
* print a user deprecation notices summary at the end of the test suite.

By default any non-legacy-tagged deprecation notice will make tests fail.
By default any non-legacy-tagged or any non-@-silenced deprecation notices will
make tests fail.
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.

A summary of deprecation notices is displayed at the end of the test suite:

* **Unsilenced** reports deprecation notices that were triggered without the
recommended @-silencing operator;
* **Legacy** deprecation notices denote tests that explicitly test some legacy
interfaces. There are four ways to mark a test as legacy:
- make its class start with the `Legacy` prefix;
Expand All @@ -35,6 +38,12 @@ Add this bridge to the `require-dev` section of your composer.json file
When running `phpunit`, you will see a summary of deprecation notices at the end
of the test suite.

Deprecation notices in the **Unsilenced** section should just be @-silenced:
`@trigger_error('...', E_USER_DEPRECATED);`. Without the @-silencing operator,
users would need to opt-out from deprecation notices. Silencing by default swaps
this behavior and allows users to opt-in when they are ready to cope with them
(by adding a custom error handler like the one provided by this bridge.)

Deprecation notices in the **Remaining/Other** section need some thought.
You have to decide either to:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ public function getDescribeContainerBuilderTestData()
*/
public function testLegacyDescribeSynchronizedServiceDefinition(Definition $definition, $expectedDescription)
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$this->assertDescription($expectedDescription, $definition);
}

public function provideLegacySynchronizedServiceDefinitionTestData()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

return $this->getDescriptionTestData(ObjectsProvider::getLegacyContainerDefinitions());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
*/
class LegacyFragmentRendererPassTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
}

/**
* Tests that content rendering not implementing FragmentRendererInterface
* trigger an exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
*/
class LegacyTemplatingAssetHelperPassTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
}

public function getScopesTests()
{
return array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ public function testInvalidValueTrustedProxies()
*/
public function testLegacyInvalidValueAssets()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$processor = new Processor();
$configuration = new Configuration(true);
$processor->processConfiguration($configuration, array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ public function testTemplating()
*/
public function testLegacyTemplatingAssets()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$this->checkAssetsPackages($this->createContainerFromFile('legacy_templating_assets'), true);
}

Expand Down Expand Up @@ -296,8 +294,6 @@ public function testValidation()
*/
public function testLegacyFullyConfiguredValidationService()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

if (!extension_loaded('apc')) {
$this->markTestSkipped('The apc extension is not available.');
}
Expand Down Expand Up @@ -399,8 +395,6 @@ public function testFormsCanBeEnabledWithoutCsrfProtection()
*/
public function testLegacyFormCsrfFieldNameCanBeSetUnderCsrfSettings()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = $this->createContainerFromFile('form_csrf_sets_field_name');

$this->assertTrue($container->getParameter('form.type_extension.csrf.enabled'));
Expand All @@ -412,8 +406,6 @@ public function testLegacyFormCsrfFieldNameCanBeSetUnderCsrfSettings()
*/
public function testLegacyFormCsrfFieldNameUnderFormSettingsTakesPrecedence()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = $this->createContainerFromFile('form_csrf_under_form_sets_field_name');

$this->assertTrue($container->getParameter('form.type_extension.csrf.enabled'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ class LegacyContainerAwareHIncludeFragmentRendererTest extends TestCase
{
public function testRender()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container->expects($this->once())
->method('get')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ protected function setUp()
*/
public function testLegacyGetSecurity()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$securityContext = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');

$this->assertNull($this->globals->getSecurity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class AssetsHelperTest extends \PHPUnit_Framework_TestCase
*/
public function testLegacyGetUrl()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$package = new Package(new StaticVersionStrategy('22', '%s?version=%s'));
$packages = new Packages($package);
$helper = new AssetsHelper($packages);
Expand All @@ -37,8 +35,6 @@ public function testLegacyGetUrl()
*/
public function testLegacyGetVersion()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$package = new Package(new StaticVersionStrategy('22'));
$imagePackage = new Package(new StaticVersionStrategy('42'));
$packages = new Packages($package, array('images' => $imagePackage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public function testCollectWhenAuthenticationTokenIsNull()
*/
public function testLegacyCollectWhenAuthenticationTokenIsNull()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$tokenStorage = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$collector = new SecurityDataCollector($tokenStorage, $this->getRoleHierarchy());
$collector->collect($this->getRequest(), $this->getResponse());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class TwigExtensionTest extends TestCase
*/
public function testLegacyFormResourcesConfigurationKey($format)
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'legacy-form-resources-only', $format);
Expand All @@ -49,8 +47,6 @@ public function testLegacyFormResourcesConfigurationKey($format)
*/
public function testLegacyMergeFormResourcesConfigurationKeyWithFormThemesConfigurationKey($format)
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'legacy-merge-form-resources-with-form-themes', $format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@

use Symfony\Bundle\TwigBundle\Extension\AssetsExtension;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Component\Routing\RequestContext;

/**
* @group legacy
*/
class LegacyAssetsExtensionTest extends TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
}

/**
* @dataProvider provideGetAssetUrlArguments
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
*/
class LegacyRenderTokenParserTest extends TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
}

/**
* @dataProvider getTestsForRender
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class LegacyApcUniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

if (!extension_loaded('apc')) {
$this->markTestSkipped('The apc extension is not available.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
class LegacyUniversalClassLoaderTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
}

/**
* @dataProvider getLoadClassTests
*/
Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,6 @@ public function testSetCatchExceptions()
*/
public function testLegacyAsText()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$application = new Application();
$application->add(new \FooCommand());
$this->ensureStaticCommandHelp($application);
Expand All @@ -509,8 +507,6 @@ public function testLegacyAsText()
*/
public function testLegacyAsXml()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$application = new Application();
$application->add(new \FooCommand());
$this->ensureStaticCommandHelp($application);
Expand Down Expand Up @@ -679,7 +675,7 @@ public function testRun()
}

/**
* Issue #9285
* Issue #9285.
*
* If the "verbose" option is just before an argument in ArgvInput,
* an argument value should not be treated as verbosity value.
Expand Down
4 changes: 0 additions & 4 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,6 @@ public function callableMethodCommand(InputInterface $input, OutputInterface $ou
*/
public function testLegacyAsText()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$command = new \TestCommand();
$command->setApplication(new Application());
$tester = new CommandTester($command);
Expand All @@ -328,8 +326,6 @@ public function testLegacyAsText()
*/
public function testLegacyAsXml()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);

$command = new \TestCommand();
$command->setApplication(new Application());
$tester = new CommandTester($command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
*/
class LegacyDialogHelperTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
}

public function testSelect()
{
$dialog = new DialogHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
*/
class LegacyProgressHelperTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
}

public function testAdvance()
{
$progress = new ProgressHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class LegacyTableHelperTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
$this->stream = fopen('php://memory', 'r+');
}

Expand Down
Loading