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\ExampleTableNode;
14 use Behat\Gherkin\Node\FeatureNode;
15 use Behat\Gherkin\Node\OutlineNode;
16 use Behat\Gherkin\Node\ScenarioInterface;
19 * Filters scenarios by definition line number.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
23 class LineFilter implements FilterInterface
25 protected $filterLine;
30 * @param string $filterLine Line of the scenario to filter on
32 public function __construct($filterLine)
34 $this->filterLine = intval($filterLine);
38 * Checks if Feature matches specified filter.
40 * @param FeatureNode $feature Feature instance
44 public function isFeatureMatch(FeatureNode $feature)
46 return $this->filterLine === $feature->getLine();
50 * Checks if scenario or outline matches specified filter.
52 * @param ScenarioInterface $scenario Scenario or Outline node instance
56 public function isScenarioMatch(ScenarioInterface $scenario)
58 if ($this->filterLine === $scenario->getLine()) {
62 if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
63 return $this->filterLine === $scenario->getLine()
64 || in_array($this->filterLine, $scenario->getExampleTable()->getLines());
71 * Filters feature according to the filter and returns new one.
73 * @param FeatureNode $feature
77 public function filterFeature(FeatureNode $feature)
80 foreach ($feature->getScenarios() as $scenario) {
81 if (!$this->isScenarioMatch($scenario)) {
85 if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
86 $table = $scenario->getExampleTable()->getTable();
87 $lines = array_keys($table);
89 if (in_array($this->filterLine, $lines)) {
90 $filteredTable = array($lines[0] => $table[$lines[0]]);
92 if ($lines[0] !== $this->filterLine) {
93 $filteredTable[$this->filterLine] = $table[$this->filterLine];
96 $scenario = new OutlineNode(
97 $scenario->getTitle(),
99 $scenario->getSteps(),
100 new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
101 $scenario->getKeyword(),
107 $scenarios[] = $scenario;
110 return new FeatureNode(
111 $feature->getTitle(),
112 $feature->getDescription(),
114 $feature->getBackground(),
116 $feature->getKeyword(),
117 $feature->getLanguage(),