Skip to content
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
209 changes: 209 additions & 0 deletions features/profile_filters.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
Feature: Filters
In order to run only needed features
As a Behat user
I need to be able to use gherkin filters

Background:
Given a file named "features/bootstrap/FeatureContext.php" with:
"""
<?php

use Behat\Behat\Context\Context;

class FeatureContext implements Context
{
/**
* @Given /^Some slow step N(\d+)$/
*/
public function someSlowStepN($num) {}

/**
* @Given /^Some normal step N(\d+)$/
*/
public function someNormalStepN($num) {}

/**
* @Given /^Some fast step N(\d+)$/
*/
public function someFastStepN($num) {}
}
"""
And a file named "features/feature1.feature" with:
"""
@tag1
Feature: A simple feature
In order to ...
As a first user
I need to ...

Background:
Given Some slow step N11

Scenario:
Given Some slow step N12
And Some normal step N13

Scenario:
Given Some fast step N14
"""
And a file named "features/feature2.feature" with:
"""
@tag2
Feature: Second feature
In order to ...
As a second user
I need to ...

Background:
Given Some slow step N11

Scenario:
Given Some slow step N12
And Some normal step N13

Scenario:
Given Some fast step N14
"""
And a file named "features/feature3.feature" with:
"""
@tag2
Feature: A bit less simple feature
In order to ...
As a third user
I need to ...

Background:
Given Some slow step N11

Scenario:
Given Some slow step N12
And Some normal step N13

Scenario:
Given Some fast step N14
"""

Scenario: Tag filters
Given a file named "behat.yml" with:
"""
default:
gherkin:
filters:
tags: tag2
"""
When I run "behat --no-colors -f pretty"
Then it should pass with:
"""
@tag2
Feature: Second feature
In order to ...
As a second user
I need to ...

Background: # features/feature2.feature:7
Given Some slow step N11 # FeatureContext::someSlowStepN()

Scenario: # features/feature2.feature:10
Given Some slow step N12 # FeatureContext::someSlowStepN()
And Some normal step N13 # FeatureContext::someNormalStepN()

Scenario: # features/feature2.feature:14
Given Some fast step N14 # FeatureContext::someFastStepN()

@tag2
Feature: A bit less simple feature
In order to ...
As a third user
I need to ...

Background: # features/feature3.feature:7
Given Some slow step N11 # FeatureContext::someSlowStepN()

Scenario: # features/feature3.feature:10
Given Some slow step N12 # FeatureContext::someSlowStepN()
And Some normal step N13 # FeatureContext::someNormalStepN()

Scenario: # features/feature3.feature:14
Given Some fast step N14 # FeatureContext::someFastStepN()

4 scenarios (4 passed)
10 steps (10 passed)
"""

Scenario: Role filters
Given a file named "behat.yml" with:
"""
default:
gherkin:
filters:
role: second user
"""
When I run "behat --no-colors -f pretty"
Then it should pass with:
"""
@tag2
Feature: Second feature
In order to ...
As a second user
I need to ...

Background: # features/feature2.feature:7
Given Some slow step N11 # FeatureContext::someSlowStepN()

Scenario: # features/feature2.feature:10
Given Some slow step N12 # FeatureContext::someSlowStepN()
And Some normal step N13 # FeatureContext::someNormalStepN()

Scenario: # features/feature2.feature:14
Given Some fast step N14 # FeatureContext::someFastStepN()

2 scenarios (2 passed)
5 steps (5 passed)
"""

Scenario: Name filters
Given a file named "behat.yml" with:
"""
default:
gherkin:
filters:
name: simple feature
"""
When I run "behat --no-colors -f pretty"
Then it should pass with:
"""
@tag1
Feature: A simple feature
In order to ...
As a first user
I need to ...

Background: # features/feature1.feature:7
Given Some slow step N11 # FeatureContext::someSlowStepN()

Scenario: # features/feature1.feature:10
Given Some slow step N12 # FeatureContext::someSlowStepN()
And Some normal step N13 # FeatureContext::someNormalStepN()

Scenario: # features/feature1.feature:14
Given Some fast step N14 # FeatureContext::someFastStepN()

@tag2
Feature: A bit less simple feature
In order to ...
As a third user
I need to ...

Background: # features/feature3.feature:7
Given Some slow step N11 # FeatureContext::someSlowStepN()

Scenario: # features/feature3.feature:10
Given Some slow step N12 # FeatureContext::someSlowStepN()
And Some normal step N13 # FeatureContext::someNormalStepN()

Scenario: # features/feature3.feature:14
Given Some fast step N14 # FeatureContext::someFastStepN()

4 scenarios (4 passed)
10 steps (10 passed)
"""
68 changes: 61 additions & 7 deletions src/Behat/Behat/Gherkin/ServiceContainer/GherkinExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Behat\Behat\Translator\ServiceContainer\TranslatorExtension;
use Behat\Testwork\Cli\ServiceContainer\CliExtension;
use Behat\Testwork\Filesystem\ServiceContainer\FilesystemExtension;
use Behat\Testwork\ServiceContainer\Exception\ExtensionException;
use Behat\Testwork\ServiceContainer\Extension;
use Behat\Testwork\ServiceContainer\ExtensionManager;
use Behat\Testwork\ServiceContainer\ServiceProcessor;
Expand Down Expand Up @@ -82,13 +83,19 @@ public function configure(ArrayNodeDefinition $builder)
$builder
->addDefaultsIfNotSet()
->children()
->scalarNode('cache')
->defaultValue(
is_writable(sys_get_temp_dir())
? sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'gherkin_cache'
: null
)
->end()
->scalarNode('cache')
->defaultValue(
is_writable(sys_get_temp_dir())
? sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'gherkin_cache'
: null
)
->end()
->arrayNode('filters')
->defaultValue(array())
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->end()
->end();
}

Expand All @@ -102,6 +109,7 @@ public function load(ContainerBuilder $container, array $config)
$this->loadKeywords($container);
$this->loadParser($container);
$this->loadDefaultLoaders($container, $config['cache']);
$this->loadProfileFilters($container, $config['filters']);
$this->loadSyntaxController($container);
$this->loadFilterController($container);
$this->loadSuiteWithPathsSetup($container);
Expand Down Expand Up @@ -216,6 +224,21 @@ protected function loadDefaultLoaders(ContainerBuilder $container, $cachePath)
$container->setDefinition('gherkin.loader.gherkin_file', $definition);
}

/**
* Loads profile-level gherkin filters.
*
* @param ContainerBuilder $container
* @param array $filters
*/
protected function loadProfileFilters(ContainerBuilder $container, array $filters)
{
$gherkin = $container->getDefinition(self::MANAGER_ID);
foreach ($filters as $type => $filterString) {
$filter = $this->createFilterDefinition($type, $filterString);
$gherkin->addMethodCall('addFilter', array($filter));
}
}

/**
* Loads syntax controller.
*
Expand Down Expand Up @@ -289,4 +312,35 @@ protected function processLoaders(ContainerBuilder $container)
$definition->addMethodCall('addLoader', array($reference));
}
}

/**
* Creates filter definition of provided type.
*
* @param string $type
* @param string $filterString
*
* @return Definition
*
* @throws ExtensionException If filter type is not recognised
*/
protected function createFilterDefinition($type, $filterString)
{
if ('role' === $type) {
return new Definition('Behat\Gherkin\Filter\RoleFilter', array($filterString));
}

if ('name' === $type) {
return new Definition('Behat\Gherkin\Filter\NameFilter', array($filterString));
}

if ('tags' === $type) {
return new Definition('Behat\Gherkin\Filter\TagFilter', array($filterString));
}

throw new ExtensionException(sprintf(
'`%s` filter is not supported by the `filters` option of gherkin extension. Supported types are %s.',
$type,
implode(', ', array('`role`', '`name`', '`tags`'))
), 'gherkin');
}
}