Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide TagTrait to replace ScenarioTagTrait. #510

Merged
merged 4 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions features/blackbox.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,6 +17,9 @@
* conflicts.
*/
class FeatureContext extends RawDrupalContext {

use TagTrait;

/**
* Hook into node creation to test `@beforeNodeCreate`
*
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 8 additions & 6 deletions src/Drupal/DrupalExtension/Context/MinkContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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.
*/
class MinkContext extends MinkExtension implements TranslatableContext
{

use ScenarioTagTrait;
use TagTrait;

/**
* Returns list of definition translation resources paths.
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
46 changes: 46 additions & 0 deletions src/Drupal/DrupalExtension/FeatureTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\DrupalExtension;

use Behat\Behat\Hook\Scope\BeforeStepScope;

/**
* A workaround to discover the current feature.
*
* @see https://github.com/Behat/Behat/issues/653
* @see https://github.com/Behat/Behat/issues/650
* The solution is documented in this issue: https://github.com/Behat/Behat/issues/703#issuecomment-86687563
*/
trait FeatureTrait
{

/**
* The registered feature.
*
* @var \Behat\Gherkin\Node\FeatureNode
*/
protected $currentFeature;

/**
* Register the feature.
*
* This fires on a BeforeStep rather than a BeforeFeature since the latter
* can only be called statically.
*
* @param \Behat\Behat\Hook\Scope\BeforeStepScope $scope
*
* @BeforeStep
*/
public function registerFeature(BeforeStepScope $scope)
{
$this->currentFeature = $scope->getFeature();
}

/**
* @return \Behat\Gherkin\Node\FeatureNode
*/
protected function getFeature()
{
return $this->currentFeature;
}
}
2 changes: 1 addition & 1 deletion src/Drupal/DrupalExtension/ScenarioTagTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
43 changes: 43 additions & 0 deletions src/Drupal/DrupalExtension/ScenarioTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Drupal\DrupalExtension;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;

/**
* A workaround to discover the current scenario.
*
* @see https://github.com/Behat/Behat/issues/653
* @see https://github.com/Behat/Behat/issues/650
* The solution is documented in this issue: https://github.com/Behat/Behat/issues/703#issuecomment-86687563
*/
trait ScenarioTrait
{

/**
* The registered scenario.
*
* @var \Behat\Gherkin\Node\ScenarioInterface
*/
protected $currentScenario;

/**
* Register the scenario.
*
* @param \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope
*
* @BeforeScenario
*/
public function registerScenario(BeforeScenarioScope $scope)
{
$this->currentScenario = $scope->getScenario();
}

/**
* @return \Behat\Gherkin\Node\ScenarioInterface
*/
protected function getScenario()
{
return $this->currentScenario;
}
}
35 changes: 35 additions & 0 deletions src/Drupal/DrupalExtension/TagTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Drupal\DrupalExtension;

/**
* Helper methods to check the tags that are present on a feature or scenario.
*/
trait TagTrait
{
use FeatureTrait, ScenarioTrait;

/**
* Returns all tags for the current scenario and feature.
*
* @return string[]
*/
protected function getTags()
{
$featureTags = $this->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());
}
}