X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FBehat%2FGherkin%2FSpecification%2FLazyFeatureIterator.php;fp=vendor%2Fbehat%2Fbehat%2Fsrc%2FBehat%2FBehat%2FGherkin%2FSpecification%2FLazyFeatureIterator.php;h=61dd9bdb292d780b407555cd016c8d5af878ef6d;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hp=0000000000000000000000000000000000000000;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68;p=yaffs-website diff --git a/vendor/behat/behat/src/Behat/Behat/Gherkin/Specification/LazyFeatureIterator.php b/vendor/behat/behat/src/Behat/Behat/Gherkin/Specification/LazyFeatureIterator.php new file mode 100644 index 000000000..61dd9bdb2 --- /dev/null +++ b/vendor/behat/behat/src/Behat/Behat/Gherkin/Specification/LazyFeatureIterator.php @@ -0,0 +1,207 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Behat\Behat\Gherkin\Specification; + +use Behat\Gherkin\Filter\FilterInterface; +use Behat\Gherkin\Filter\NameFilter; +use Behat\Gherkin\Filter\NarrativeFilter; +use Behat\Gherkin\Filter\RoleFilter; +use Behat\Gherkin\Filter\TagFilter; +use Behat\Gherkin\Gherkin; +use Behat\Gherkin\Node\FeatureNode; +use Behat\Testwork\Specification\SpecificationIterator; +use Behat\Testwork\Suite\Exception\SuiteConfigurationException; +use Behat\Testwork\Suite\Suite; + +/** + * Lazily iterates (parses one-by-one) over features. + * + * @author Konstantin Kudryashov + */ +final class LazyFeatureIterator implements SpecificationIterator +{ + /** + * @var Suite + */ + private $suite; + /** + * @var Gherkin + */ + private $gherkin; + /** + * @var string[] + */ + private $paths = array(); + /** + * @var FilterInterface[] + */ + private $filters = array(); + /** + * @var integer + */ + private $position = 0; + /** + * @var FeatureNode[] + */ + private $features = array(); + /** + * @var FeatureNode + */ + private $currentFeature; + + /** + * Initializes specifications. + * + * @param Suite $suite + * @param Gherkin $gherkin + * @param string[] $paths + * @param FilterInterface[] $filters + */ + public function __construct(Suite $suite, Gherkin $gherkin, array $paths, array $filters = array()) + { + $this->suite = $suite; + $this->gherkin = $gherkin; + $this->paths = array_values($paths); + $this->filters = array_merge($this->getSuiteFilters($suite), $filters); + } + + /** + * {@inheritdoc} + */ + public function getSuite() + { + return $this->suite; + } + + /** + * {@inheritdoc} + */ + public function rewind() + { + $this->position = 0; + $this->moveToNextAvailableFeature(); + } + + /** + * {@inheritdoc} + */ + public function next() + { + $this->moveToNextAvailableFeature(); + } + + /** + * {@inheritdoc} + */ + public function valid() + { + return null !== $this->currentFeature; + } + + /** + * {@inheritdoc} + */ + public function key() + { + return $this->position; + } + + /** + * {@inheritdoc} + */ + public function current() + { + return $this->currentFeature; + } + + /** + * Returns list of filters from suite settings. + * + * @param Suite $suite + * + * @return FilterInterface[] + */ + private function getSuiteFilters(Suite $suite) + { + if (!$suite->hasSetting('filters') || !is_array($suite->getSetting('filters'))) { + return array(); + } + + $filters = array(); + foreach ($suite->getSetting('filters') as $type => $filterString) { + $filters[] = $this->createFilter($type, $filterString, $suite); + } + + return $filters; + } + + /** + * Creates filter of provided type. + * + * @param string $type + * @param string $filterString + * @param Suite $suite + * + * @return FilterInterface + * + * @throws SuiteConfigurationException If filter type is not recognised + */ + private function createFilter($type, $filterString, Suite $suite) + { + if ('role' === $type) { + return new RoleFilter($filterString); + } + + if ('name' === $type) { + return new NameFilter($filterString); + } + + if ('tags' === $type) { + return new TagFilter($filterString); + } + + if ('narrative' === $type) { + return new NarrativeFilter($filterString); + } + + throw new SuiteConfigurationException(sprintf( + '`%s` filter is not supported by the `%s` suite. Supported types are `%s`.', + $type, + $suite->getName(), + implode('`, `', array('role', 'name', 'tags')) + ), $suite->getName()); + } + + /** + * Parses paths consequently. + */ + private function moveToNextAvailableFeature() + { + while (!count($this->features) && $this->position < count($this->paths)) { + $this->features = $this->parseFeature($this->paths[$this->position]); + $this->position++; + } + + $this->currentFeature = array_shift($this->features); + } + + /** + * Parses feature at path. + * + * @param string $path + * + * @return FeatureNode[] + */ + private function parseFeature($path) + { + return $this->gherkin->load($path, $this->filters); + } +}