behat/gherkin
behat/gherkin is a PHP library for parsing the Gherkin language used in BDD. Read and tokenize feature files, build an AST, and integrate with Behat or other test runners to execute human-readable scenarios in your test suite.
.feature files). It fits seamlessly into test automation pipelines, specification-driven development, or documentation-as-code workflows.i18n.php), making it ideal for multilingual projects or localized test suites.legacy and gherkin-32 modes for alignment with Cucumber’s parser, reducing friction in cross-tool ecosystems.GherkinParser) for dependency injection..feature files during migrations, deployments, or CI pipelines..feature files, strings, or streams, enabling:
FeatureNode objects can be traversed/modified, enabling:
| Risk Area | Mitigation Strategy |
|---|---|
| Parser Quirks | Use legacy mode for stability; gherkin-32 for Cucumber parity (but test thoroughly). |
| Deprecations | Avoid extending Lexer, Parser, or Node classes (use ParserInterface instead). |
| Tag Filtering | Deprecated syntax (e.g., @wip&&~slow) may break; enforce @-prefixed tags in CI. |
| Performance | Cache parsed ASTs (Laravel’s file cache or Redis) to avoid reprocessing. |
| Language Support | Validate unsupported languages early (e.g., via NoSuchLanguageException). |
| PHP Version | Laravel 10+ (PHP 8.1+) is fully supported; drop PHP 7.2 if using newer Laravel. |
gherkin-32 mode for Cucumber alignment, or stick with legacy for stability?php artisan gherkin:parse)?.feature files before merging.composer require behat/gherkin
use Behat\Gherkin\Parser;
use Behat\Gherkin\Lexer;
use Behat\Gherkin\Keywords\ArrayKeywords;
$keywords = new ArrayKeywords(['en' => [...]]); // Load from package
$lexer = new Lexer($keywords);
$parser = new Parser($lexer);
$feature = $parser->parse(file_get_contents('path/to/features/login.feature'));
$this->app->singleton(Parser::class, fn() => new Parser(new Lexer(new ArrayKeywords(['en' => [...])))));
// app/Facades/Gherkin.php
public static function parse(string $file): FeatureNode { ... }
FeatureNode methods (getChildren(), getSteps(), etc.) to transform or validate specs.DialectProviderInterface for domain-specific keywords.| Component | Compatibility Notes |
|---|---|
| Laravel | Works with Laravel 5.8+ (PHP 7.2+); PHP 8.5 fully supported. |
| Behat | Native integration; no additional setup needed if using Behat. |
| Cucumber | gherkin-32 mode aligns with Cucumber’s parser (but test thoroughly). |
| PHP Extensions | No hard dependencies, but YAML support (for NDJSON) may require yaml extension. |
| File Formats | Supports .feature files, strings, and streams; can extend for NDJSON. |
php artisan gherkin:parse features/login.feature)..feature files on PR.gherkin-32 mode).Lexer/Parser (use ParserInterface).@wip&&~slow) is deprecated; enforce @-prefixed tags in CI.@ in tags).How can I help you explore Laravel packages today?