diff --git a/features/blackbox.feature b/features/blackbox.feature index a7218419..be28ae70 100644 --- a/features/blackbox.feature +++ b/features/blackbox.feature @@ -86,3 +86,9 @@ Feature: Test DrupalContext Given I am on the homepage When I click "Download & Extend" Then I should see the link "Distributions" + + @scenariotag + Scenario: Check tags on feature and scenario + Then the "scenariotag" tag should be present + And the "blackbox" tag should be present + But the "nonexisting" tag should not be present diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 196b4894..53458df6 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -5,6 +5,7 @@ use Drupal\DrupalExtension\Context\RawDrupalContext; use Drupal\DrupalExtension\Hook\Scope\BeforeNodeCreateScope; use Drupal\DrupalExtension\Hook\Scope\EntityScope; +use Drupal\DrupalExtension\TagTrait; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; @@ -16,6 +17,9 @@ * conflicts. */ class FeatureContext extends RawDrupalContext { + + use TagTrait; + /** * Hook into node creation to test `@beforeNodeCreate` * @@ -419,6 +423,36 @@ public function itShouldFail($success) } } + /** + * Checks if the current scenario or feature has the given tag. + * + * @Then the :tag tag should be present + * + * @param string $tag + * The tag to check. + */ + public function shouldHaveTag($tag) + { + if (!$this->hasTag($tag)) { + throw new \Exception("Expected tag $tag was not found in the scenario or feature."); + } + } + + /** + * Checks if the current scenario or feature does not have the given tag. + * + * @Then the :tag tag should not be present + * + * @param string $tag + * The tag to check. + */ + public function shouldNotHaveTag($tag) + { + if ($this->hasTag($tag)) { + throw new \Exception("Expected tag $tag was found in the scenario or feature."); + } + } + private function getExitCode() { return $this->process->getExitCode(); diff --git a/src/Drupal/DrupalExtension/Context/MinkContext.php b/src/Drupal/DrupalExtension/Context/MinkContext.php index 69b15725..b7bf9e1c 100644 --- a/src/Drupal/DrupalExtension/Context/MinkContext.php +++ b/src/Drupal/DrupalExtension/Context/MinkContext.php @@ -5,7 +5,7 @@ use Behat\Behat\Context\TranslatableContext; use Behat\Mink\Exception\UnsupportedDriverActionException; use Behat\MinkExtension\Context\MinkContext as MinkExtension; -use Drupal\DrupalExtension\ScenarioTagTrait; +use Drupal\DrupalExtension\TagTrait; /** * Extensions to the Mink Extension. @@ -13,7 +13,7 @@ class MinkContext extends MinkExtension implements TranslatableContext { - use ScenarioTagTrait; + use TagTrait; /** * Returns list of definition translation resources paths. @@ -95,8 +95,11 @@ public function assertEnterField($field, $value) public function beforeJavascriptStep($event) { /** @var \Behat\Behat\Hook\Scope\BeforeStepScope $event */ - $tags = $this->getCurrentScenarioTags($event); - if (!in_array('javascript', $tags)) { + // Make sure the feature is registered in case this hook fires before + // ::registerFeature() which is also a @BeforeStep. Behat doesn't + // support ordering hooks. + $this->registerFeature($event); + if (!$this->hasTag('javascript')) { return; } $text = $event->getStep()->getText(); @@ -113,8 +116,7 @@ public function beforeJavascriptStep($event) public function afterJavascriptStep($event) { /** @var \Behat\Behat\Hook\Scope\BeforeStepScope $event */ - $tags = $this->getCurrentScenarioTags($event); - if (!in_array('javascript', $tags)) { + if (!$this->hasTag('javascript')) { return; } $text = $event->getStep()->getText(); diff --git a/src/Drupal/DrupalExtension/FeatureTrait.php b/src/Drupal/DrupalExtension/FeatureTrait.php new file mode 100644 index 00000000..13b500ec --- /dev/null +++ b/src/Drupal/DrupalExtension/FeatureTrait.php @@ -0,0 +1,46 @@ +currentFeature = $scope->getFeature(); + } + + /** + * @return \Behat\Gherkin\Node\FeatureNode + */ + protected function getFeature() + { + return $this->currentFeature; + } +} diff --git a/src/Drupal/DrupalExtension/ScenarioTagTrait.php b/src/Drupal/DrupalExtension/ScenarioTagTrait.php index a54b0f05..5420c9b0 100644 --- a/src/Drupal/DrupalExtension/ScenarioTagTrait.php +++ b/src/Drupal/DrupalExtension/ScenarioTagTrait.php @@ -14,7 +14,7 @@ * * The solution is documented in this issue: https://github.com/Behat/Behat/issues/703#issuecomment-86687563 * - * @package Drupal\DrupalExtension + * @deprecated Use \Drupal\DrupalExtension\TagTrait instead. */ trait ScenarioTagTrait { diff --git a/src/Drupal/DrupalExtension/ScenarioTrait.php b/src/Drupal/DrupalExtension/ScenarioTrait.php new file mode 100644 index 00000000..4979e45b --- /dev/null +++ b/src/Drupal/DrupalExtension/ScenarioTrait.php @@ -0,0 +1,43 @@ +currentScenario = $scope->getScenario(); + } + + /** + * @return \Behat\Gherkin\Node\ScenarioInterface + */ + protected function getScenario() + { + return $this->currentScenario; + } +} diff --git a/src/Drupal/DrupalExtension/TagTrait.php b/src/Drupal/DrupalExtension/TagTrait.php new file mode 100644 index 00000000..b703459d --- /dev/null +++ b/src/Drupal/DrupalExtension/TagTrait.php @@ -0,0 +1,35 @@ +getFeature()->getTags(); + $scenarioTags = $this->getScenario()->getTags(); + return array_unique(array_merge($featureTags, $scenarioTags)); + } + + /** + * Checks whether the current scenario or feature has the given tag. + * + * @param string $tag + * + * @return bool + */ + protected function hasTag($tag) + { + return in_array($tag, $this->getTags()); + } +}