Skip to content

Commit

Permalink
Add possibility to ignore deprecation messages and/or files that emit…
Browse files Browse the repository at this point in the history
… them.

Add possibility to ignore deprecation messages and/or files that emit them.
  • Loading branch information
jdeniau authored and caciobanu committed Mar 13, 2019
1 parent c89151f commit 601d9fb
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,45 @@ Run Behat and enjoy :)
- Remaining
- Deprecation notices are all other (non-legacy) notices.
## Ignore some deprecation
You can filter the file that did make the call to `trigger_error` like this:

```yaml
default:
extensions:
Caciobanu\Behat\DeprecationExtension:
ignore:
- { file: '#symfony#' }
- { file: '#my-app#' }
```

It will ignore every files that matches any of the listed regexps

Or you can filter deprecation messages like this:

```yaml
default:
extensions:
Caciobanu\Behat\DeprecationExtension:
ignore:
- { message: '#symfony#' }
- { message: '#my-app#' }
```

It will ignore every deprecation message that matches any of the listed regexps

You can use both filter types at the same time:


```yaml
default:
extensions:
Caciobanu\Behat\DeprecationExtension:
ignore:
- { file: '#symfony#', message: '#symfony#' }
```

## Credits

This library is developed by [Catalin Ciobanu](https://github.com/caciobanu).
Expand Down
24 changes: 24 additions & 0 deletions Tests/Error/Handler/DeprecationErrorHandler/ignore_file.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Test DeprecationErrorHandler in weak mode
--FILE--
<?php

putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');

$behat = __DIR__;
while (!file_exists($behat . '/vendor/bin/behat')) {
$behat = dirname($behat);
}

passthru("php -d error_reporting=32767 " . $behat . "/vendor/bin/behat --profile=ignore_file --out=default.log 2>/dev/null", $exitCode);

if ($exitCode === 1) {
echo "Exit code: 1";
}

?>
--EXPECTF--

Exit code: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Test DeprecationErrorHandler in weak mode
--FILE--
<?php

putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');

$behat = __DIR__;
while (!file_exists($behat . '/vendor/bin/behat')) {
$behat = dirname($behat);
}

passthru("php -d error_reporting=32767 " . $behat . "/vendor/bin/behat --profile=ignore_file_and_message --out=default.log 2>/dev/null", $exitCode);

if ($exitCode === 1) {
echo "Exit code: 1";
}

?>
--EXPECTF--

Remaining deprecation notices (2)

Method 'deprecatedMethodSilenced' is deprecated: 2x
2x in DeprecatedCaller::callDeprecatedMethodSilenced from Caciobanu\Behat\DeprecationExtension\Tests\Deprecated

Legacy deprecation notices (4)

Method 'deprecatedMethodSilenced' is deprecated: 4x
4x in DeprecatedCaller::callDeprecatedMethodSilenced from Caciobanu\Behat\DeprecationExtension\Tests\Deprecated

Exit code: 1
34 changes: 34 additions & 0 deletions Tests/Error/Handler/DeprecationErrorHandler/ignore_message.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Test DeprecationErrorHandler in weak mode
--FILE--
<?php

putenv('ANSICON');
putenv('ConEmuANSI');
putenv('TERM');

$behat = __DIR__;
while (!file_exists($behat . '/vendor/bin/behat')) {
$behat = dirname($behat);
}

passthru("php -d error_reporting=32767 " . $behat . "/vendor/bin/behat --profile=ignore_message --out=default.log 2>/dev/null", $exitCode);

if ($exitCode === 1) {
echo "Exit code: 1";
}

?>
--EXPECTF--

Remaining deprecation notices (2)

Method 'deprecatedMethodSilenced' is deprecated: 2x
2x in DeprecatedCaller::callDeprecatedMethodSilenced from Caciobanu\Behat\DeprecationExtension\Tests\Deprecated

Legacy deprecation notices (4)

Method 'deprecatedMethodSilenced' is deprecated: 4x
4x in DeprecatedCaller::callDeprecatedMethodSilenced from Caciobanu\Behat\DeprecationExtension\Tests\Deprecated

Exit code: 1
52 changes: 51 additions & 1 deletion Tests/ServiceContainer/DeprecationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testLoad()

$this->assertEquals('Caciobanu\Behat\DeprecationExtension\Error\Handler\DeprecationErrorHandler' ,$definition->getClass());
$this->assertEquals('%caciobanu.deprecation_extension.mode%', (string) $definition->getArgument(0));
$this->assertEquals('%caciobanu.deprecation_extension.ignore%', (string) $definition->getArgument(1));
}

/**
Expand All @@ -62,6 +63,25 @@ public function testConfigureInvalidValue()
));
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testConfigureInvalidIgnoreValue()
{
$configurationTree = new ConfigurationTree();
$tree = $configurationTree->getConfigTree(array(new DeprecationExtension()));

$processor = new Processor();
$processor->process($tree, array(
'testwork' => array(
'caciobanu_deprecation_extension' => array(
'mode' => 'weak',
'ignore' => array('hello'),
),
),
));
}

/**
* @dataProvider configValueProvider
*/
Expand All @@ -79,7 +99,28 @@ public function testConfigure($mode)
),
));

$this->assertEquals(array('caciobanu_deprecation_extension' => array('mode' => $mode)), $config);
$this->assertEquals(array('caciobanu_deprecation_extension' => array('mode' => $mode, 'ignore' => array())), $config);
}

/**
* @dataProvider configIgnoreValueProvider
*/
public function testConfigureIgnore($ignore)
{
$configurationTree = new ConfigurationTree();
$tree = $configurationTree->getConfigTree(array(new DeprecationExtension()));

$processor = new Processor();
$config = $processor->process($tree, array(
'testwork' => array(
'caciobanu_deprecation_extension' => array(
'mode' => 'weak',
'ignore' => $ignore
),
),
));

$this->assertEquals(array('caciobanu_deprecation_extension' => array('mode' => 'weak', 'ignore' => $ignore)), $config);
}

public function configValueProvider()
Expand All @@ -92,4 +133,13 @@ public function configValueProvider()
array(100),
);
}

public function configIgnoreValueProvider()
{
return array(
array(array(array('file' => 'file'))),
array(array(array('message' => 'message'))),
array(array(array('file' => 'file', 'message' => 'message'), array('file' => 'file', 'message' => 'message'))),
);
}
}
24 changes: 24 additions & 0 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ integer:
Caciobanu\Behat\DeprecationExtension:
mode: 1

ignore_file:
calls:
error_reporting: 32767
extensions:
Caciobanu\Behat\DeprecationExtension:
ignore:
- { file: '#DeprecatedClass#' }

ignore_message:
calls:
error_reporting: 32767
extensions:
Caciobanu\Behat\DeprecationExtension:
ignore:
- { message: '#deprecatedMethodUnsilenced#' }

ignore_file_and_message:
calls:
error_reporting: 32767
extensions:
Caciobanu\Behat\DeprecationExtension:
ignore:
- { file: '#DeprecatedClass#', message: '#deprecatedMethodUnsilenced#' }

default_no_error_reporting:
calls:
error_reporting: 0
Expand Down
50 changes: 49 additions & 1 deletion src/Error/Handler/DeprecationErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ class DeprecationErrorHandler
*/
private $mode;

/**
* @var array $ignore
*/
private $ignore;

/**
* @param int|string|null $mode The reporting mode.
* @param array $ignore
*/
public function __construct($mode = null)
public function __construct($mode = null, array $ignore = array())
{
$this->mode = $mode;
$this->ignore = $ignore;
}

/**
Expand All @@ -65,6 +72,10 @@ public function register(Call $call, $level, $message)
return;
}

if ($this->isIgnored($message)) {
return;
}

$trace = debug_backtrace();

$group = 'remaining';
Expand Down Expand Up @@ -205,4 +216,41 @@ private function hasColorSupport()

return defined('STDOUT') && function_exists('posix_isatty') && @posix_isatty(STDOUT);
}

private function getCaller()
{
$backtrace = debug_backtrace();
foreach ($backtrace as $item) {
if ('trigger_error' === $item['function']) {
return $item;
}
}
}

/**
* @param string $message
* @return bool
*/
private function isIgnored($message)
{
if (empty($this->ignore)) {
return false;
}

$callerItem = $this->getCaller();

foreach ($this->ignore as $ignore) {
if (isset($ignore['file'], $ignore['message'])) {
return preg_match($ignore['file'], $callerItem['file']) && preg_match($ignore['message'], $message);
}
if (isset($ignore['file']) && preg_match($ignore['file'], $callerItem['file'])) {
return true;
}
if (isset($ignore['message']) && preg_match($ignore['message'], $message)) {
return true;
}
}

return false;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<service id="caciobanu.deprecation_extension.deprecation_error_handler"
class="Caciobanu\Behat\DeprecationExtension\Error\Handler\DeprecationErrorHandler">
<argument>%caciobanu.deprecation_extension.mode%</argument>
<argument>%caciobanu.deprecation_extension.ignore%</argument>
</service>
</services>
</container>
20 changes: 20 additions & 0 deletions src/ServiceContainer/DeprecationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ public function configure(ArrayNodeDefinition $builder)
{
$builder
->children()
->arrayNode('ignore')
->arrayPrototype()
->children()
->scalarNode('file')->end()
->scalarNode('message')->end()
->end()
->validate()
->ifTrue(function ($value) {
if (!isset($value['file']) && !isset($value['message'])) {
return true;
}

return false;
})
->thenInvalid('At least "file" or "message" must be set')
->end()
->end()
->end()
->scalarNode('mode')
->defaultValue(null)
->validate()
Expand All @@ -81,6 +99,8 @@ public function configure(ArrayNodeDefinition $builder)
public function load(ContainerBuilder $container, array $config)
{
$container->setParameter('caciobanu.deprecation_extension.mode', $config['mode']);
$ignore = isset($config['ignore']) ? $config['ignore'] : array();
$container->setParameter('caciobanu.deprecation_extension.ignore', $ignore);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
Expand Down

0 comments on commit 601d9fb

Please sign in to comment.