4 * This file is part of the Behat.
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\Behat\Hook\Call;
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;
22 * Represents a feature hook.
24 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 abstract class RuntimeFeatureHook extends RuntimeFilterableHook
31 * @param string $scopeName
32 * @param null|string $filterString
33 * @param callable $callable
34 * @param null|string $description
36 * @throws BadCallbackException If callback is method, but not a static one
38 public function __construct($scopeName, $filterString, $callable, $description = null)
40 parent::__construct($scopeName, $filterString, $callable, $description);
42 if ($this->isAnInstanceMethod()) {
43 throw new BadCallbackException(sprintf(
44 'Feature hook callback: %s::%s() must be a static method',
54 public function filterMatches(HookScope $scope)
56 if (!$scope instanceof FeatureScope) {
60 if (null === ($filterString = $this->getFilterString())) {
64 return $this->isMatch($scope->getFeature(), $filterString);
68 * @param FeatureNode $feature
69 * @param string $filterString
73 private function isMatch(FeatureNode $feature, $filterString)
75 if (false !== strpos($filterString, '@')) {
76 return $this->isMatchTagFilter($feature, $filterString);
79 if (!empty($filterString)) {
80 return $this->isMatchNameFilter($feature, $filterString);
87 * Checks if feature matches tag filter.
89 * @param FeatureNode $feature
90 * @param string $filterString
94 private function isMatchTagFilter(FeatureNode $feature, $filterString)
96 $filter = new TagFilter($filterString);
98 return $filter->isFeatureMatch($feature);
102 * Checks if feature matches name filter.
104 * @param FeatureNode $feature
105 * @param string $filterString
109 private function isMatchNameFilter(FeatureNode $feature, $filterString)
111 $filter = new NameFilter($filterString);
113 return $filter->isFeatureMatch($feature);