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

Use static not self in test traits #19

Merged
merged 2 commits into from
Jan 21, 2025
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
22 changes: 16 additions & 6 deletions src/PHPUnit/Framework/Constraint/HasScenarioPassedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,22 @@ protected function setupSnippetGenerator() {
$identifier = new \Behat\Behat\Context\Snippet\Generator\FixedContextIdentifier($context);
$this->snippetGenerator->setContextIdentifier($identifier);

// Modify the snippet generator's template so that it no longer
// refers to PendingException().
$templateReflector = new \ReflectionProperty(get_class($this->snippetGenerator), 'templateTemplate');
$templateReflector->setAccessible(true);
$templateReflector->setValue($this->snippetGenerator, $this->snippetTemplate);
}
// Determine the correct property name.
$reflector = new \ReflectionClass(get_class($this->snippetGenerator));
$templateProperty = null;
if ($reflector->hasProperty('snippetTemplate')) {
$templateProperty = 'snippetTemplate';
} elseif ($reflector->hasProperty('templateTemplate')) {
$templateProperty = 'templateTemplate';
}

if ($templateProperty !== null) {
// Modify the snippet generator's template to remove reference to pending exception.
$templateReflector = new \ReflectionProperty(get_class($this->snippetGenerator), $templateProperty);
$templateReflector->setAccessible(true);
$templateReflector->setValue($this->snippetGenerator, $this->snippetTemplate);
}
}

protected function generateSnippet($step) {
if (!is_null($this->snippetGenerator) && !is_null($this->environment)) {
Expand Down
4 changes: 2 additions & 2 deletions src/TestTraits/BehatProvidingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait BehatProvidingTrait {
* A Behat feature.
*/
public static function parseBehatFeature($featureString, $keywords = NULL) {
$lexer = new Lexer(self::getBehatKeywords($keywords));
$lexer = new Lexer(static::getBehatKeywords($keywords));
$parser = new Parser($lexer);
$feature = $parser->parse($featureString);
return $feature;
Expand Down Expand Up @@ -71,7 +71,7 @@ public static function provideBehatFeature(FeatureNode $feature) {
*/
protected static function getBehatKeywords($keywords = NULL) {
if (is_null($keywords)) {
$keywords = self::getBehatDefaultKeywords();
$keywords = static::getBehatDefaultKeywords();
}
return $keywords;
}
Expand Down
4 changes: 2 additions & 2 deletions src/TestTraits/BehatTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ trait BehatTestTrait {
* breaking it down into individual scenarios for testing.
*/
public static function providerTestBehatScenario() {
$feature = self::parseBehatFeature(self::$feature);
return self::provideBehatFeature($feature);
$feature = static::parseBehatFeature(static::$feature);
return static::provideBehatFeature($feature);
}

/**
Expand Down
45 changes: 45 additions & 0 deletions tests/BehatProvidingTraitChildTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace PHPUnitBehat\Tests;

use Behat\Gherkin\Node\KeywordNodeInterface;
use Behat\Gherkin\Node\ScenarioInterface;

/**
* Tests that BehatProvidingTrait can discover features in child classes.
*/
class BehatProvidingTraitChildTest extends BehatProvidingTraitTestParent {

protected static $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());
}

}

18 changes: 18 additions & 0 deletions tests/BehatProvidingTraitTestParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PHPUnitBehat\Tests;

use PHPUnit\Framework\TestCase;
use PHPUnitBehat\TestTraits\BehatTestTrait;
use Behat\Gherkin\Node\KeywordNodeInterface;
use Behat\Gherkin\Node\ScenarioInterface;

/**
*
*/
class BehatProvidingTraitTestParent extends TestCase {

use BehatTestTrait;

}

Loading