b3d4fb24b370b928b8845f547d36efd765b2fd03
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Hook / Call / RuntimeFeatureHook.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\Hook\Call;
12
13 use Behat\Behat\Hook\Scope\FeatureScope;
14 use Behat\Gherkin\Filter\NameFilter;
15 use Behat\Gherkin\Filter\TagFilter;
16 use Behat\Gherkin\Node\FeatureNode;
17 use Behat\Testwork\Call\Exception\BadCallbackException;
18 use Behat\Testwork\Hook\Call\RuntimeFilterableHook;
19 use Behat\Testwork\Hook\Scope\HookScope;
20
21 /**
22  * Represents a feature hook.
23  *
24  * @author Konstantin Kudryashov <ever.zet@gmail.com>
25  */
26 abstract class RuntimeFeatureHook extends RuntimeFilterableHook
27 {
28     /**
29      * Initializes hook.
30      *
31      * @param string      $scopeName
32      * @param null|string $filterString
33      * @param callable    $callable
34      * @param null|string $description
35      *
36      * @throws BadCallbackException If callback is method, but not a static one
37      */
38     public function __construct($scopeName, $filterString, $callable, $description = null)
39     {
40         parent::__construct($scopeName, $filterString, $callable, $description);
41
42         if ($this->isAnInstanceMethod()) {
43             throw new BadCallbackException(sprintf(
44                 'Feature hook callback: %s::%s() must be a static method',
45                 $callable[0],
46                 $callable[1]
47             ), $callable);
48         }
49     }
50
51     /**
52      * {@inheritdoc}
53      */
54     public function filterMatches(HookScope $scope)
55     {
56         if (!$scope instanceof FeatureScope) {
57             return false;
58         }
59
60         if (null === ($filterString = $this->getFilterString())) {
61             return true;
62         }
63
64         return $this->isMatch($scope->getFeature(), $filterString);
65     }
66
67     /**
68      * @param FeatureNode $feature
69      * @param string      $filterString
70      *
71      * @return Boolean
72      */
73     private function isMatch(FeatureNode $feature, $filterString)
74     {
75         if (false !== strpos($filterString, '@')) {
76             return $this->isMatchTagFilter($feature, $filterString);
77         }
78
79         if (!empty($filterString)) {
80             return $this->isMatchNameFilter($feature, $filterString);
81         }
82
83         return false;
84     }
85
86     /**
87      * Checks if feature matches tag filter.
88      *
89      * @param FeatureNode $feature
90      * @param string      $filterString
91      *
92      * @return Boolean
93      */
94     private function isMatchTagFilter(FeatureNode $feature, $filterString)
95     {
96         $filter = new TagFilter($filterString);
97
98         return $filter->isFeatureMatch($feature);
99     }
100
101     /**
102      * Checks if feature matches name filter.
103      *
104      * @param FeatureNode $feature
105      * @param string      $filterString
106      *
107      * @return Boolean
108      */
109     private function isMatchNameFilter(FeatureNode $feature, $filterString)
110     {
111         $filter = new NameFilter($filterString);
112
113         return $filter->isFeatureMatch($feature);
114     }
115 }