From a4910222f0db40dd82cea486f1400aed07de0ef0 Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Fri, 15 Jul 2022 22:57:53 +0300 Subject: [PATCH] Test cucumber/gherkin compatibility layer --- .github/workflows/build.yml | 4 ++++ composer.json | 7 +++++- src/Codeception/Test/Loader/Gherkin.php | 31 ++++++++++++++++++------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c3ffb45673..fbe4854f81 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/composer.json b/composer.json index 9fe547f0b0..1a99aa8f93 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/Codeception/Test/Loader/Gherkin.php b/src/Codeception/Test/Loader/Gherkin.php index d079dd6af8..21e69c9e64 100644 --- a/src/Codeception/Test/Loader/Gherkin.php +++ b/src/Codeception/Test/Loader/Gherkin.php @@ -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; @@ -57,7 +59,7 @@ class Gherkin implements LoaderInterface */ protected array $tests = []; - protected GherkinParser $parser; + protected GherkinFileLoader|CucumberGherkinLoader $gherkinFileLoader; protected array $settings = []; @@ -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(); } @@ -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