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

WIP: Test cucumber/gherkin compatibility layer #6507

Open
wants to merge 1 commit into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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