Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Gherkin / Specification / LazyFeatureIterator.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Gherkin\Specification;
12
13 use Behat\Gherkin\Filter\FilterInterface;
14 use Behat\Gherkin\Filter\NameFilter;
15 use Behat\Gherkin\Filter\NarrativeFilter;
16 use Behat\Gherkin\Filter\RoleFilter;
17 use Behat\Gherkin\Filter\TagFilter;
18 use Behat\Gherkin\Gherkin;
19 use Behat\Gherkin\Node\FeatureNode;
20 use Behat\Testwork\Specification\SpecificationIterator;
21 use Behat\Testwork\Suite\Exception\SuiteConfigurationException;
22 use Behat\Testwork\Suite\Suite;
23
24 /**
25  * Lazily iterates (parses one-by-one) over features.
26  *
27  * @author Konstantin Kudryashov <ever.zet@gmail.com>
28  */
29 final class LazyFeatureIterator implements SpecificationIterator
30 {
31     /**
32      * @var Suite
33      */
34     private $suite;
35     /**
36      * @var Gherkin
37      */
38     private $gherkin;
39     /**
40      * @var string[]
41      */
42     private $paths = array();
43     /**
44      * @var FilterInterface[]
45      */
46     private $filters = array();
47     /**
48      * @var integer
49      */
50     private $position = 0;
51     /**
52      * @var FeatureNode[]
53      */
54     private $features = array();
55     /**
56      * @var FeatureNode
57      */
58     private $currentFeature;
59
60     /**
61      * Initializes specifications.
62      *
63      * @param Suite             $suite
64      * @param Gherkin           $gherkin
65      * @param string[]          $paths
66      * @param FilterInterface[] $filters
67      */
68     public function __construct(Suite $suite, Gherkin $gherkin, array $paths, array $filters = array())
69     {
70         $this->suite = $suite;
71         $this->gherkin = $gherkin;
72         $this->paths = array_values($paths);
73         $this->filters = array_merge($this->getSuiteFilters($suite), $filters);
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function getSuite()
80     {
81         return $this->suite;
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     public function rewind()
88     {
89         $this->position = 0;
90         $this->moveToNextAvailableFeature();
91     }
92
93     /**
94      * {@inheritdoc}
95      */
96     public function next()
97     {
98         $this->moveToNextAvailableFeature();
99     }
100
101     /**
102      * {@inheritdoc}
103      */
104     public function valid()
105     {
106         return null !== $this->currentFeature;
107     }
108
109     /**
110      * {@inheritdoc}
111      */
112     public function key()
113     {
114         return $this->position;
115     }
116
117     /**
118      * {@inheritdoc}
119      */
120     public function current()
121     {
122         return $this->currentFeature;
123     }
124
125     /**
126      * Returns list of filters from suite settings.
127      *
128      * @param Suite $suite
129      *
130      * @return FilterInterface[]
131      */
132     private function getSuiteFilters(Suite $suite)
133     {
134         if (!$suite->hasSetting('filters') || !is_array($suite->getSetting('filters'))) {
135             return array();
136         }
137
138         $filters = array();
139         foreach ($suite->getSetting('filters') as $type => $filterString) {
140             $filters[] = $this->createFilter($type, $filterString, $suite);
141         }
142
143         return $filters;
144     }
145
146     /**
147      * Creates filter of provided type.
148      *
149      * @param string $type
150      * @param string $filterString
151      * @param Suite  $suite
152      *
153      * @return FilterInterface
154      *
155      * @throws SuiteConfigurationException If filter type is not recognised
156      */
157     private function createFilter($type, $filterString, Suite $suite)
158     {
159         if ('role' === $type) {
160             return new RoleFilter($filterString);
161         }
162
163         if ('name' === $type) {
164             return new NameFilter($filterString);
165         }
166
167         if ('tags' === $type) {
168             return new TagFilter($filterString);
169         }
170
171         if ('narrative' === $type) {
172             return new NarrativeFilter($filterString);
173         }
174
175         throw new SuiteConfigurationException(sprintf(
176             '`%s` filter is not supported by the `%s` suite. Supported types are `%s`.',
177             $type,
178             $suite->getName(),
179             implode('`, `', array('role', 'name', 'tags'))
180         ), $suite->getName());
181     }
182
183     /**
184      * Parses paths consequently.
185      */
186     private function moveToNextAvailableFeature()
187     {
188         while (!count($this->features) && $this->position < count($this->paths)) {
189             $this->features = $this->parseFeature($this->paths[$this->position]);
190             $this->position++;
191         }
192
193         $this->currentFeature = array_shift($this->features);
194     }
195
196     /**
197      * Parses feature at path.
198      *
199      * @param string $path
200      *
201      * @return FeatureNode[]
202      */
203     private function parseFeature($path)
204     {
205         return $this->gherkin->load($path, $this->filters);
206     }
207 }