Skip to content

Commit

Permalink
Test cucumber/gherkin compatibility layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Naktibalda committed Jan 13, 2023
1 parent dea896f commit a491022
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Expand Up @@ -82,6 +82,10 @@ jobs:
if: matrix.mode == 'stable'
run: composer update --prefer-source --no-interaction --no-progress --optimize-autoloader --ansi

- name: Test cucumber/gherkin compatibility layer in experimental mode
if: matrix.mode == 'experimental'
run: composer require --no-update cucumber/gherkin behat/gherkin:"dev-cucumber-gherkin as 4.99.99"

- name: Composer install lowest versions of dependencies on PHP 8.0 in experimental mode
if: matrix.php == '8.0' && matrix.mode == 'experimental'
run: composer update --prefer-source --prefer-lowest --no-interaction --no-progress --optimize-autoloader --ansi
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Expand Up @@ -68,7 +68,12 @@
"replace": {
"codeception/phpunit-wrapper": "*"
},

"repositories": [
{
"type": "vcs",
"url": "git@github.com:ciaranmcnulty/Gherkin-1.git"
}
],
"autoload": {
"classmap": [
"src/PHPUnit/TestCase.php"
Expand Down
31 changes: 22 additions & 9 deletions src/Codeception/Test/Loader/Gherkin.php
Expand Up @@ -7,6 +7,8 @@
use Behat\Gherkin\Filter\RoleFilter;
use Behat\Gherkin\Keywords\ArrayKeywords as GherkinKeywords;
use Behat\Gherkin\Lexer as GherkinLexer;
use Behat\Gherkin\Loader\CucumberGherkinLoader;
use Behat\Gherkin\Loader\GherkinFileLoader;
use Behat\Gherkin\Node\ExampleNode;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\OutlineNode;
Expand Down Expand Up @@ -57,7 +59,7 @@ class Gherkin implements LoaderInterface
*/
protected array $tests = [];

protected GherkinParser $parser;
protected GherkinFileLoader|CucumberGherkinLoader $gherkinFileLoader;

protected array $settings = [];

Expand All @@ -73,12 +75,21 @@ public function __construct(array $settings = [])
if (!class_exists(GherkinKeywords::class)) {
throw new TestParseException('Feature file can only be parsed with Behat\Gherkin library. Please install `behat/gherkin` with Composer');
}
$gherkin = new ReflectionClass(\Behat\Gherkin\Gherkin::class);
$gherkinClassPath = dirname($gherkin->getFileName());
$i18n = require $gherkinClassPath . '/../../../i18n.php';
$keywords = new GherkinKeywords($i18n);
$lexer = new GherkinLexer($keywords);
$this->parser = new GherkinParser($lexer);

if (
class_exists(CucumberGherkinLoader::class) // if supporting older behat/gherkin
&& CucumberGherkinLoader::isAvailable()
) {
$this->gherkinFileLoader = new CucumberGherkinLoader();
} else {
$gherkin = new ReflectionClass(\Behat\Gherkin\Gherkin::class);
$gherkinClassPath = dirname($gherkin->getFileName());
$i18n = require $gherkinClassPath . '/../../../i18n.php';
$keywords = new GherkinKeywords($i18n);
$lexer = new GherkinLexer($keywords);
$parser = new GherkinParser($lexer);
$this->gherkinFileLoader = new GherkinFileLoader($parser);
}
$this->fetchGherkinSteps();
}

Expand Down Expand Up @@ -190,12 +201,14 @@ private function validatePattern(string $pattern): void

public function loadTests(string $filename): void
{
$featureNode = $this->parser->parse(file_get_contents($filename), $filename);
$featureNodes = $this->gherkinFileLoader->load($filename);

if (!$featureNode instanceof FeatureNode) {
if ($featureNodes === [] || !$featureNodes[0] instanceof FeatureNode) {
return;
}

$featureNode = $featureNodes[0];

foreach ($featureNode->getScenarios() as $scenarioNode) {
/** @var ScenarioInterface $scenarioNode */
$steps = $this->steps['default']; // load default context
Expand Down

0 comments on commit a491022

Please sign in to comment.