Skip to content

Commit

Permalink
Merge pull request #1 from jonathanjfshaw/testname
Browse files Browse the repository at this point in the history
Fix outline & better data provider names
  • Loading branch information
jonathanjfshaw authored Apr 3, 2019
2 parents 1e960ce + 3b2722a commit 6c99d5e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ You can define these undefined steps in your PHPUnit test class like this:
```

You can specify individual scenarios to run because the scenario title is given as the data provider name. For example to test a scenario with title `Undefined`:
```
phpunit --filter '@Undefined'
```
More complex possibilities exist; see https://phpunit.de/manual/6.5/en/textui.html#textui.examples.filter-patterns.

## Notes
The code has not been tested with Behat's 'Outline' scenarios, but it should work.

Expand Down
7 changes: 4 additions & 3 deletions src/TestTraits/BehatProvidingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Behat\Gherkin\Parser;
use Behat\Gherkin\Keywords\ArrayKeywords;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\OutlineNode;

trait BehatProvidingTrait {

Expand Down Expand Up @@ -47,12 +48,12 @@ public function provideBehatFeature(FeatureNode $feature) {
$scenarios = [];
foreach ($feature->getScenarios() as $scenario) {
if ($scenario instanceof OutlineNode) {
foreach ($outline->getExamples() as $example) {
$scenarios[] = [$example->getTitle(), $example, $feature];
foreach ($scenario->getExamples() as $index => $example) {
$scenarios[$scenario->getTitle() . ' #' . $index] = [$example, $feature];
}
}
else {
$scenarios[] = [$scenario->getTitle(), $scenario, $feature];
$scenarios[$scenario->getTitle()] = [$scenario, $feature];
}
}
return $scenarios;
Expand Down
8 changes: 7 additions & 1 deletion src/TestTraits/BehatScenarioTestingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ 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);
self::assertThat($scenarioResults, $constraint, $message);
}

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
10 changes: 1 addition & 9 deletions src/TestTraits/BehatTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

namespace PHPUnitBehat\TestTraits;

// proof read
// @todo coding standards
// github real
// packagist
// require in project
// merge branch
// refactor hasscenariopassedconstraint for reuse

trait BehatTestTrait {

use BehatScenarioTestingTrait;
Expand All @@ -31,7 +23,7 @@ public function providerTestBehatScenario() {
*
* @dataProvider providerTestBehatScenario
*/
public function testBehatScenario($scenarioTitle, $scenario, $feature) {
public function testBehatScenario($scenario, $feature) {
$this->assertBehatScenario($scenario, $feature);
}

Expand Down

0 comments on commit 6c99d5e

Please sign in to comment.