Skip to content

Commit

Permalink
Backwards compatibility (filter > filters.include).
Browse files Browse the repository at this point in the history
  • Loading branch information
vever001 committed Jun 20, 2024
1 parent 94f241b commit 0039f0c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ default:
NicWortel\BehatUnusedStepDefinitionsExtension\Extension:
filters:
include:
- 'MyProject\\Behat\\Contexts'
- 'OtherProject\\Behat\\(Foo|Bar)Context'
- '/MyProject\\Behat\\Contexts/'
- '/OtherProject\\Behat\\(Foo|Bar)Context/'
exclude:
- 'MyProject\\Behat\\Contexts\\FeatureContext'
- '::excludedMethod'
- 'OtherProject\\Behat\\FooContext::.+Method'
- '/MyProject\\Behat\\Contexts\\FeatureContext/'
- '/::excludedMethod/'
- '/OtherProject\\Behat\\FooContext::.+Method/'
```
#### Ignore pattern aliases
Expand Down
20 changes: 20 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

use function sprintf;
use function trigger_error;

use const E_USER_DEPRECATED;

final class Extension implements BehatExtension
{
public function process(ContainerBuilder $container): void
Expand All @@ -38,6 +43,8 @@ public function configure(ArrayNodeDefinition $builder): void
->booleanNode('ignorePatternAliases')
->defaultFalse()
->end()
// @todo Deprecated config key, remove when possible.
->scalarNode('filter')->end()
->arrayNode('filters')
->info('Specifies include/exclude filters')
->performNoDeepMerging()
Expand All @@ -62,6 +69,19 @@ public function configure(ArrayNodeDefinition $builder): void
*/
public function load(ContainerBuilder $container, array $config): void
{
// @todo Deprecated config key, remove when possible.
if (isset($config['filter'])) {
$config['filters']['include'][] = $config['filter'];
@trigger_error(
sprintf(
'Since %s %s: The "filter" config key is deprecated, use "filters.include" instead.',
'nicwortel/behat-unused-step-definitions-extension',
'1.1.2',
),
E_USER_DEPRECATED
);
}

$serviceDefinition = new Definition(
UnusedStepDefinitionsChecker::class,
[
Expand Down
21 changes: 12 additions & 9 deletions src/UnusedStepDefinitionsChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use function array_diff;
use function array_filter;
use function implode;
use function preg_match;
use function sprintf;

Expand Down Expand Up @@ -105,23 +104,27 @@ private function filterIncludeExclude(array $unusedDefinitions): array
// Get the concrete path reference for this definition.
$path = $this->getConcretePath($definition->getReflection());

$include = $this->filters['include'] ?? null;
$match_include = !empty($include) ? $this->patternsMatch($path, $include) : true;
$includePatterns = $this->filters['include'] ?? null;
$includeMatch = $includePatterns ? $this->matchesPatterns($path, $includePatterns) : true;

$exclude = $this->filters['exclude'] ?? null;
$match_exclude = !empty($exclude) ? $this->patternsMatch($path, $exclude) : false;
$excludePatterns = $this->filters['exclude'] ?? null;
$excludeMatch = $excludePatterns ? $this->matchesPatterns($path, $excludePatterns) : false;

return $match_include && !$match_exclude;
return $includeMatch && !$excludeMatch;
});
}

/**
* @param string[] $patterns
*/
private function patternsMatch(string $path, array $patterns): bool
private function matchesPatterns(string $path, array $patterns): bool
{
$pattern = '/' . implode('|', $patterns) . '/';
return (bool) preg_match($pattern, $path);
foreach ($patterns as $pattern) {
if ((bool) preg_match($pattern, $path)) {
return true;
}
}
return false;
}

private function getConcretePath(ReflectionFunctionAbstract $function): string
Expand Down
12 changes: 12 additions & 0 deletions tests/BehatExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ public function testFilterInclude(): void
$this->assertStringContainsString($expected, $behat->getOutput());
}

public function testFilterIncludeBC(): void
{
$behat = new Process(['../../vendor/bin/behat', '--profile', 'include_bc', '--config', 'behat_filtered.yml'], __DIR__ . '/fixtures/');
$behat->mustRun();

$expected = <<<EOF
1 unused step definitions:
- Then some step that is never used by a feature # FeatureContext::someStepThatIsNeverUsedByAFeature()
EOF;
$this->assertStringContainsString($expected, $behat->getOutput());
}

public function testFilterIncludeInheritance(): void
{
$behat = new Process(['../../vendor/bin/behat', '--profile', 'include_inheritance', '--config', 'behat_filtered.yml'], __DIR__ . '/fixtures/');
Expand Down
5 changes: 0 additions & 5 deletions tests/fixtures/behat_extended.yml

This file was deleted.

15 changes: 10 additions & 5 deletions tests/fixtures/behat_filtered.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@ include:
NicWortel\BehatUnusedStepDefinitionsExtension\Extension:
filters:
include:
- 'someStepThatIsNeverUsedByAFeature'
- '/someStepThatIsNeverUsedByAFeature/'

include_bc:
extensions:
NicWortel\BehatUnusedStepDefinitionsExtension\Extension:
filter: '/someStepThatIsNeverUsedByAFeature/'

include_inheritance:
extensions:
NicWortel\BehatUnusedStepDefinitionsExtension\Extension:
filters:
include:
- 'FeatureContext::'
- '/FeatureContext::/'

exclude:
extensions:
NicWortel\BehatUnusedStepDefinitionsExtension\Extension:
filters:
exclude:
- 'someStepThatIsNeverUsedByAFeature'
- '/someStepThatIsNeverUsedByAFeature/'

include_exclude:
extensions:
NicWortel\BehatUnusedStepDefinitionsExtension\Extension:
filters:
include:
- 'FeatureContext::'
- '/FeatureContext::/'
exclude:
- 'someStepThatIsNeverUsedByAFeature'
- '/someStepThatIsNeverUsedByAFeature/'

0 comments on commit 0039f0c

Please sign in to comment.