Skip to content

Instantly share code, notes, and snippets.

@naxhh
Last active March 1, 2017 16:24
Show Gist options
  • Save naxhh/e85bd52715e856b73e1d to your computer and use it in GitHub Desktop.
Save naxhh/e85bd52715e856b73e1d to your computer and use it in GitHub Desktop.
Behat Scenario parser
<?php
require __DIR__ . '/vendor/autoload.php';
$features_path = __DIR__ . 'test/Behat/';
$project = 'project';
$keywords = new Behat\Gherkin\Keywords\CachedArrayKeywords( __DIR__ . '/vendor/behat/gherkin/i18n.php' );
$lexer = new Behat\Gherkin\Lexer($keywords);
$parser = new Behat\Gherkin\Parser($lexer);
$gherkin = new Behat\Gherkin\Gherkin();
$gherkin->addLoader(new Behat\Gherkin\Loader\DirectoryLoader($gherkin));
$gherkin->addLoader(new Behat\Gherkin\Loader\GherkinFileLoader($parser));
$gherkin->setBasePath( $features_path );
try
{
/**
* @var FeatureNode[]
*
* @see https://github.com/Behat/Gherkin/blob/2.3/src/Behat/Gherkin/Node/FeatureNode.php
*/
$features = $gherkin->load( $project ); // all features in provided directory
} catch( \InvalidArgumentException $e )
{
throw new InvalidArgumentException( 'Seems the project "'. $project .'" does not exist!' );
}
foreach ( $features as $feature ) {
printnl( $feature->getTitle() );
runButton( $feature->getFile(), $project, $feature->getTitle() )
if ( $description = $feature->getDescription() ) {
printnl( $description );
}
foreach( $feature->getScenarios() as $scenario ) {
printnl( $scenario->getTitle() );
foreach ($scenario->getSteps() as $step ) {
printnl( $step->getText() );
foreach ($step->getArguments() as $argument ) {
if ( $argument instanceof Behat\Gherkin\Node\PyStringNode ) {
foreach ($argument->getLines() as $line ) {
printnl( $line );
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment