4 * This file is part of the Behat Gherkin.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Gherkin\Filter;
13 use Behat\Gherkin\Node\FeatureNode;
14 use Behat\Gherkin\Node\ScenarioInterface;
17 * Filters scenarios by feature/scenario tag.
19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
21 class TagFilter extends ComplexFilter
23 protected $filterString;
28 * @param string $filterString Name filter string
30 public function __construct($filterString)
32 $this->filterString = trim($filterString);
36 * Checks if Feature matches specified filter.
38 * @param FeatureNode $feature Feature instance
42 public function isFeatureMatch(FeatureNode $feature)
44 return $this->isTagsMatchCondition($feature->getTags());
48 * Checks if scenario or outline matches specified filter.
50 * @param FeatureNode $feature Feature node instance
51 * @param ScenarioInterface $scenario Scenario or Outline node instance
55 public function isScenarioMatch(FeatureNode $feature, ScenarioInterface $scenario)
57 return $this->isTagsMatchCondition(array_merge($feature->getTags(), $scenario->getTags()));
61 * Checks that node matches condition.
63 * @param string[] $tags
67 protected function isTagsMatchCondition($tags)
71 foreach (explode('&&', $this->filterString) as $andTags) {
72 $satisfiesComma = false;
74 foreach (explode(',', $andTags) as $tag) {
75 $tag = str_replace('@', '', trim($tag));
77 if ('~' === $tag[0]) {
78 $tag = mb_substr($tag, 1, mb_strlen($tag, 'utf8') - 1, 'utf8');
79 $satisfiesComma = !in_array($tag, $tags) || $satisfiesComma;
81 $satisfiesComma = in_array($tag, $tags) || $satisfiesComma;
85 $satisfies = (false !== $satisfiesComma && $satisfies && $satisfiesComma) || false;