Completed
Pull Request — master (#510)
by Pieter
02:49 queued 01:25
created

TagTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerTagContainingScopes() 0 10 3
A getTagTraitHelper() 0 7 2
A getTags() 0 6 1
A hasTag() 0 4 1
1
<?php
2
3
namespace Drupal\DrupalExtension;
4
5
use Behat\Behat\Hook\Scope\BeforeFeatureScope;
6
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
7
use Behat\Behat\Hook\Scope\BeforeStepScope;
8
use Behat\Behat\Hook\Scope\StepScope;
9
use Behat\Gherkin\Node\ScenarioInterface;
10
use Behat\Testwork\Hook\Scope\HookScope;
11
12
/**
13
 * Helper methods to check the tags that are present on a feature or scenario.
14
 */
15
trait TagTrait
16
{
17
18
    /**
19
     * @var \Drupal\DrupalExtension\TagTraitHelper
20
     */
21
    protected $tagTraitHelper;
22
23
    /**
24
     * Tracks the scopes that can be tagged.
25
     *
26
     * Tags can be put on features as well as scenarios. This hook will fire
27
     * when a new feature or scenario is being executed and will keep track of
28
     * both so that the tags can be inspected during the test.
29
     *
30
     * @param \Behat\Testwork\Hook\Scope\HookScope $scope
31
     *
32
     * @BeforeScenario
33
     * @BeforeStep
34
     */
35
    public function registerTagContainingScopes(HookScope $scope)
36
    {
37
        if ($scope instanceof BeforeScenarioScope) {
38
            $this->getTagTraitHelper()->registerScenario($scope);
39
        }
40
41
        if ($scope instanceof BeforeStepScope) {
42
            $this->getTagTraitHelper()->registerFeature($scope);
43
        }
44
    }
45
46
    /**
47
     * Returns the helper class as a singleton.
48
     *
49
     * @return \Drupal\DrupalExtension\TagTraitHelper
50
     */
51
    protected function getTagTraitHelper()
52
    {
53
        if (empty($this->tagTraitHelper)) {
54
            $this->tagTraitHelper = new TagTraitHelper();
55
        }
56
        return $this->tagTraitHelper;
57
    }
58
59
    /**
60
     * Returns all tags for the current scenario and feature.
61
     *
62
     * @return string[]
63
     */
64
    protected function getTags()
65
    {
66
        $featureTags = $this->getTagTraitHelper()->getFeature()->getTags();
0 ignored issues
show
Bug introduced by
The method getFeature() cannot be called from this context as it is declared protected in class Drupal\DrupalExtension\FeatureTrait.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
67
        $scenarioTags = $this->getTagTraitHelper()->getScenario()->getTags();
0 ignored issues
show
Bug introduced by
The method getScenario() cannot be called from this context as it is declared protected in class Drupal\DrupalExtension\ScenarioTrait.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
68
        return array_unique(array_merge($featureTags, $scenarioTags));
69
    }
70
71
    /**
72
     * Checks whether the current scenario or feature has the given tag.
73
     *
74
     * @param string $tag
75
     *
76
     * @return bool
77
     */
78
    protected function hasTag($tag)
79
    {
80
        return in_array($tag, $this->getTags());
81
    }
82
}
83