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.
Install via Composer: composer require behat/gherkin. The entry point is the Gherkin\Parser class—pass in your Gherkin source (string or file path) to get an AST. Start by parsing a simple .feature file:
use Gherkin\Parser;
$parser = new Parser();
$feature = $parser->parse(file_get_contents('tests/features/login.feature'));
// $feature is now an AST root node (typically a FeatureNode)
Inspect the structure using the AST node types (FeatureNode, ScenarioNode, StepNode, etc.). The Gherkin\Node namespace contains all AST classes—use PHP’s var_dump() or Xdebug to explore. For quick validation, try parsing with Parser::parse() wrapped in try/catch to catch syntax errors.
Gherkin\Node\Visitor\AstDumper as a reference) to walk nodes and collect metadata (e.g., all steps with a @slow tag).behat/gherkin’s AST to your own runner that executes step definitions (e.g., mapping Given /^I log in$/ → actual test logic).@smoke tag in production features”) by walking the AST and raising warnings.Parser::setLanguage($locale) (e.g., fr, de) to parse non-English .feature files. Extend Gherkin\Language for custom keywords.Gherkin\Node\Visitor\NodeTransformer or walk the tree and build a new structure.getLine() and getFile() (if provided via parse($source, $file)), but be aware that multi-line elements (e.g., tables, doc strings) report only the first line.trim() step arguments if using raw text.Gherkin::LANGUAGE_EN is default, but @fr might be misapplied).Gherkin\Tokenizer and Gherkin\Lexer for custom lexing (e.g., extend step syntax). Avoid forking—override via composition instead.How can I help you explore Laravel packages today?