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 range.
21 * @author Fabian Kiss <headrevision@gmail.com>
23 class LineRangeFilter implements FilterInterface
25 protected $filterMinLine;
26 protected $filterMaxLine;
31 * @param string $filterMinLine Minimum line of a scenario to filter on
32 * @param string $filterMaxLine Maximum line of a scenario to filter on
34 public function __construct($filterMinLine, $filterMaxLine)
36 $this->filterMinLine = intval($filterMinLine);
37 if ($filterMaxLine == '*') {
38 $this->filterMaxLine = PHP_INT_MAX;
40 $this->filterMaxLine = intval($filterMaxLine);
45 * Checks if Feature matches specified filter.
47 * @param FeatureNode $feature Feature instance
51 public function isFeatureMatch(FeatureNode $feature)
53 return $this->filterMinLine <= $feature->getLine()
54 && $this->filterMaxLine >= $feature->getLine();
58 * Checks if scenario or outline matches specified filter.
60 * @param ScenarioInterface $scenario Scenario or Outline node instance
64 public function isScenarioMatch(ScenarioInterface $scenario)
66 if ($this->filterMinLine <= $scenario->getLine() && $this->filterMaxLine >= $scenario->getLine()) {
70 if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
71 foreach ($scenario->getExampleTable()->getLines() as $line) {
72 if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
82 * Filters feature according to the filter.
84 * @param FeatureNode $feature
88 public function filterFeature(FeatureNode $feature)
91 foreach ($feature->getScenarios() as $scenario) {
92 if (!$this->isScenarioMatch($scenario)) {
96 if ($scenario instanceof OutlineNode && $scenario->hasExamples()) {
97 $table = $scenario->getExampleTable()->getTable();
98 $lines = array_keys($table);
100 $filteredTable = array($lines[0] => $table[$lines[0]]);
101 unset($table[$lines[0]]);
103 foreach ($table as $line => $row) {
104 if ($this->filterMinLine <= $line && $this->filterMaxLine >= $line) {
105 $filteredTable[$line] = $row;
109 $scenario = new OutlineNode(
110 $scenario->getTitle(),
111 $scenario->getTags(),
112 $scenario->getSteps(),
113 new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()),
114 $scenario->getKeyword(),
119 $scenarios[] = $scenario;
122 return new FeatureNode(
123 $feature->getTitle(),
124 $feature->getDescription(),
126 $feature->getBackground(),
128 $feature->getKeyword(),
129 $feature->getLanguage(),