Skip to content

Commit

Permalink
[Bridge\PhpUnit] Add "disabled" mode to SYMFONY_DEPRECATIONS_HELPER
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 19, 2016
1 parent 83ebf97 commit 2e0fa55
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 8 deletions.
36 changes: 29 additions & 7 deletions src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php
Expand Up @@ -19,6 +19,7 @@
class DeprecationErrorHandler
{
const MODE_WEAK = 'weak';
const MODE_DISABLED = 'disabled';

private static $isRegistered = false;

Expand All @@ -39,9 +40,23 @@ public static function register($mode = 0)
if (self::$isRegistered) {
return;
}
if (self::MODE_WEAK !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
$mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
}

$mode = function () use ($mode) {
static $memoizedMode = false;

if (false !== $memoizedMode) {
return $memoizedMode;
}
if (false === $mode) {
$mode = getenv('SYMFONY_DEPRECATIONS_HELPER');
}
if (DeprecationErrorHandler::MODE_WEAK !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
$mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
}

return $memoizedMode = $mode;
};

$deprecations = array(
'unsilencedCount' => 0,
'remainingCount' => 0,
Expand All @@ -53,7 +68,7 @@ public static function register($mode = 0)
'other' => array(),
);
$deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $mode) {
if (E_USER_DEPRECATED !== $type) {
if (E_USER_DEPRECATED !== $type || DeprecationErrorHandler::MODE_DISABLED === $mode = $mode()) {
return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
}

Expand Down Expand Up @@ -95,13 +110,13 @@ public static function register($mode = 0)

exit(1);
}
if ('legacy' !== $group && self::MODE_WEAK !== $mode) {
if ('legacy' !== $group && DeprecationErrorHandler::MODE_WEAK !== $mode) {
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
$ref = &$deprecations[$group][$msg][$class.'::'.$method];
++$ref;
}
} else {
} elseif (DeprecationErrorHandler::MODE_WEAK !== $mode) {
$group = 'other';
$ref = &$deprecations[$group][$msg]['count'];
++$ref;
Expand All @@ -116,7 +131,7 @@ public static function register($mode = 0)
restore_error_handler();
self::register($mode);
}
} elseif (!isset($mode[0]) || '/' !== $mode[0]) {
} else {
self::$isRegistered = true;
if (self::hasColorSupport()) {
$colorize = function ($str, $red) {
Expand All @@ -128,9 +143,16 @@ public static function register($mode = 0)
$colorize = function ($str) {return $str;};
}
register_shutdown_function(function () use ($mode, &$deprecations, $deprecationHandler, $colorize) {
$mode = $mode();
if (isset($mode[0]) && '/' === $mode[0]) {
return;
}
$currErrorHandler = set_error_handler('var_dump');
restore_error_handler();

if (DeprecationErrorHandler::MODE_WEAK === $mode) {
$colorize = function ($str) {return $str;};
}
if ($currErrorHandler !== $deprecationHandler) {
echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
}
Expand Down
@@ -0,0 +1,64 @@
--TEST--
Test DeprecationErrorHandler in default mode
--FILE--
<?php

putenv('SYMFONY_DEPRECATIONS_HELPER');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;

@trigger_error('root deprecation', E_USER_DEPRECATED);

class PHPUnit_Util_Test
{
public static function getGroups()
{
return array();
}
}

class FooTestCase
{
public function testLegacyFoo()
{
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
}

public function testNonLegacyBar()
{
@trigger_error('silenced bar deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced bar deprecation', E_USER_DEPRECATED);
}
}

$foo = new FooTestCase();
$foo->testLegacyFoo();
$foo->testNonLegacyBar();

?>
--EXPECTF--
Unsilenced deprecation notices (2)

unsilenced foo deprecation: 1x
1x in FooTestCase::testLegacyFoo

unsilenced bar deprecation: 1x
1x in FooTestCase::testNonLegacyBar

Remaining deprecation notices (1)

silenced bar deprecation: 1x
1x in FooTestCase::testNonLegacyBar

Legacy deprecation notices (1)

Other deprecation notices (1)

root deprecation: 1x

@@ -0,0 +1,20 @@
--TEST--
Test DeprecationErrorHandler in weak mode
--FILE--
<?php

putenv('SYMFONY_DEPRECATIONS_HELPER=disabled');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;

echo (int) set_error_handler('var_dump');
echo (int) class_exists('Symfony\Bridge\PhpUnit\DeprecationErrorHandler', false);

?>
--EXPECTF--
00
@@ -0,0 +1,37 @@
--TEST--
Test DeprecationErrorHandler in weak mode
--FILE--
<?php

putenv('SYMFONY_DEPRECATIONS_HELPER=/foo/');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;

@trigger_error('root deprecation', E_USER_DEPRECATED);

class FooTestCase
{
public function testLegacyFoo()
{
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
}
}

$foo = new FooTestCase();
$foo->testLegacyFoo();

?>
--EXPECTF--
Legacy deprecation triggered by FooTestCase::testLegacyFoo:
silenced foo deprecation
Stack trace:
#0 -(13): trigger_error()
#1 -(19): FooTestCase->testLegacyFoo()
#2 {main}

34 changes: 34 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/weak.phpt
@@ -0,0 +1,34 @@
--TEST--
Test DeprecationErrorHandler in weak mode
--FILE--
<?php

putenv('SYMFONY_DEPRECATIONS_HELPER=weak');

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
define('PHPUNIT_COMPOSER_INSTALL', $vendor.'/vendor/autoload.php');
require PHPUNIT_COMPOSER_INSTALL;

@trigger_error('root deprecation', E_USER_DEPRECATED);

class FooTestCase
{
public function testLegacyFoo()
{
@trigger_error('silenced foo deprecation', E_USER_DEPRECATED);
trigger_error('unsilenced foo deprecation', E_USER_DEPRECATED);
}
}

$foo = new FooTestCase();
$foo->testLegacyFoo();

?>
--EXPECTF--
Unsilenced deprecation notices (1)

Legacy deprecation notices (1)

4 changes: 3 additions & 1 deletion src/Symfony/Bridge/PhpUnit/bootstrap.php
Expand Up @@ -24,4 +24,6 @@
AnnotationRegistry::registerLoader('class_exists');
}

DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER'));
if ('disabled' !== getenv('SYMFONY_DEPRECATIONS_HELPER')) {
DeprecationErrorHandler::register(getenv('SYMFONY_DEPRECATIONS_HELPER'));
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/PhpUnit/phpunit.xml.dist
Expand Up @@ -13,6 +13,7 @@
<testsuites>
<testsuite name="Symfony PHPUnit Bridge Test Suite">
<directory>./Tests/</directory>
<directory suffix=".phpt">./Tests/DeprecationErrorHandler/</directory>
</testsuite>
</testsuites>

Expand Down

0 comments on commit 2e0fa55

Please sign in to comment.