Skip to content

Commit

Permalink
Better availability of provided scenario inside tests
Browse files Browse the repository at this point in the history
Better availability of provided scenario inside tests
  • Loading branch information
jonathanjfshaw authored Jun 19, 2019
2 parents 6c99d5e + a382356 commit eaaff77
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 7 deletions.
41 changes: 41 additions & 0 deletions src/TestTraits/BehatProvidingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Behat\Gherkin\Keywords\ArrayKeywords;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\OutlineNode;
use Behat\Gherkin\Node\KeywordNodeInterface;
use Behat\Gherkin\Node\ScenarioInterface;

trait BehatProvidingTrait {

Expand Down Expand Up @@ -96,4 +98,43 @@ public function getBehatDefaultKeywords() {
]);
}

/**
* Get the current feature.
*
* This is intended to be called from within a test method or test setUp
* method, where it is sometimes useful to have access to the feature for
* prettier troubleshooting output.
*
* @return \Behat\Gherkin\Node\KeywordNodeInterface
*/
protected function getProvidedFeature() {
$data = $this->getProvidedData();
if (is_array($data) && $feature = $data[1]) {
if ($feature instanceof KeywordNodeInterface) {
return $feature;
}
}
throw new \Exception("Feature not found in provided data.");
}

/**
* Get the current scenario or example.
*
* This is intended to be called from within a test method or test setUp
* method, where it is sometimes useful to have access to the scenario for
* prettier troubleshooting output.
*
* @return \Behat\Gherkin\Node\ScenarioInterface
* The current scenario or example.
*/
protected function getProvidedScenario() {
$data = $this->getProvidedData();
if (is_array($data) && $scenario = $data[0]) {
if ($scenario instanceof ScenarioInterface) {
return $scenario;
}
}
throw new \Exception("Scenario not found in provided data.");
}

}
7 changes: 0 additions & 7 deletions src/TestTraits/BehatScenarioTestingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ trait BehatScenarioTestingTrait {
use BehatContainerTrait;
use BehatEnvironmentTrait;

// Make the scenario and feature available to test methods,
// this can be useful for debugging output.
protected $behatFeature;
protected $behatScenario;

public static function assertBehatScenarioPassed($scenarioResults, $scenario = NULL, $stepResults = [], $snippetGenerator = NULL, $environment = NULL, $message = '', $callHandler = '')
{
$constraint = new HasScenarioPassedConstraint($scenario, $stepResults, $callHandler, $snippetGenerator, $environment);
Expand All @@ -25,8 +20,6 @@ public static function assertBehatScenarioPassed($scenarioResults, $scenario = N

public function executeBehatScenario($scenario, $feature) {
$tester = $this->getBehatContainer()->get(TesterExtension::SCENARIO_TESTER_ID);
$this->behatFeature = $feature;
$this->behatScenario = $scenario;
$scenarioResults = $tester->test($this->getBehatEnvironment(), $feature, $scenario, false);
return $scenarioResults;
}
Expand Down
51 changes: 51 additions & 0 deletions tests/BehatProvidingTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace PHPUnitBehat\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\ExpectationFailedException;
use Behat\Testwork\Argument\Exception\UnknownParameterValueException;
use PHPUnitBehat\TestTraits\BehatTestTrait;
use Behat\Gherkin\Node\KeywordNodeInterface;
use Behat\Gherkin\Node\ScenarioInterface;

/**
*
*/
class BehatProvidingTraitTest extends TestCase {

use BehatTestTrait;

protected $feature = <<<'FEATURE'
Feature: BehatProvidingTrait
In order to test a feature
We need to able provide it to phpunit
Scenario: getProvidedScenario
Then getProvidedScenario returns a scenario with the title "getProvidedScenario"
Scenario: getProvidedFeature
Then getProvidedScenario returns a scenario with the title "getProvidedFeature"
Then getProvidedFeature returns a feature with the title "BehatProvidingTrait"

FEATURE;


/**
* @Then getProvidedScenario returns a scenario with the title :title
*/
public function getProvidedScenarioGets($title) {
$this->assertInstanceOf(ScenarioInterface::class, $this->getProvidedScenario());
$this->assertEquals($title, $this->getProvidedScenario()->getTitle());
}

/**
* @Then getProvidedFeature returns a feature with the title :title
*/
public function getProvidedFeatureGets($title) {
$this->assertInstanceOf(KeywordNodeInterface::class, $this->getProvidedFeature());
$this->assertEquals($title, $this->getProvidedFeature()->getTitle());
}

}

0 comments on commit eaaff77

Please sign in to comment.