parse() public method

Parses input & returns features array.
public parse ( string $input, string $file = null ) : Behat\Gherkin\Node\FeatureNode | null
$input string Gherkin string document
$file string File name
return Behat\Gherkin\Node\FeatureNode | null
 /**
  * {@inheritDoc}
  *
  * @throws \InvalidArgumentException
  * @throws \RuntimeException
  * @throws ParserException
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $align = $input->getOption('align') === Step::ALIGN_TO_LEFT ? Step::ALIGN_TO_LEFT : Step::ALIGN_TO_RIGHT;
     $directory = $input->getArgument('directory');
     $finder = (new FeatureResolve($directory))->__invoke();
     $output->writeln("\nFinding files on <info>" . $directory . "</info>\n");
     $tagFormatter = new Tags();
     $featureDescription = new FeatureDescription();
     $background = new Background($align);
     $scenario = new Scenario($align);
     /* @var $file \Symfony\Component\Finder\SplFileInfo */
     foreach ($finder as $file) {
         $fileContent = $file->getContents();
         $contentWithoutComments = $this->removeComments($fileContent);
         $feature = $this->parser->parse($fileContent);
         $formatted = $feature->hasTags() ? $tagFormatter->format($feature->getTags()) . "\n" : '';
         $formatted .= $featureDescription->format($feature) . "\n\n";
         $formatted .= $feature->hasBackground() ? $background->format($feature->getBackground()) . "\n" : '';
         $formatted .= $feature->hasScenarios() ? $scenario->format($feature->getScenarios()) : '';
         if ($formatted !== $contentWithoutComments) {
             if (!defined('FAILED')) {
                 define('FAILED', true);
             }
             $diff = new Differ("--- Original\n+++ Expected\n", false);
             $output->writeln('<error>Wrong style: ' . $file->getRealPath() . '</error>');
             $output->writeln($diff->diff($contentWithoutComments, $formatted));
         }
     }
     if (defined('FAILED')) {
         return 1;
     }
     $output->writeln('<bg=green;fg=white>     Everything is OK!     </>');
 }
Exemplo n.º 2
0
 public function loadTests($filename)
 {
     $featureNode = $this->parser->parse(file_get_contents($filename), $filename);
     foreach ($featureNode->getScenarios() as $scenarioNode) {
         /** @var $scenarioNode ScenarioNode  **/
         $steps = $this->steps['default'];
         // load default context
         foreach (array_merge($scenarioNode->getTags(), $featureNode->getTags()) as $tag) {
             // load tag contexts
             if (isset($this->steps["tag:{$tag}"])) {
                 $steps = array_merge($steps, $this->steps["tag:{$tag}"]);
             }
         }
         $roles = $this->settings['gherkin']['contexts']['role'];
         // load role contexts
         foreach ($roles as $role) {
             $filter = new RoleFilter($role);
             if ($filter->isFeatureMatch($featureNode)) {
                 $steps = array_merge($steps, $this->steps["role:{$role}"]);
                 break;
             }
         }
         $this->tests[] = new GherkinFormat($featureNode, $scenarioNode, $steps);
     }
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function parse($input, $file = null)
 {
     $this->_currentFile = $file;
     $this->_featureDir = $this->container->getParameter('behat.paths.features');
     $input = $this->getParsedFileText($this->explodeTextToLines($input));
     $input = $this->parseFeatureHoldersDefinitions($input);
     $feature = parent::parse($input, $file);
     return $feature;
 }
Exemplo n.º 4
0
    /**
     * @expectedException \Behat\Gherkin\Exception\ParserException
     */
    public function testMultipleFeatures()
    {
        $feature = <<<GHERKIN
Feature:

Feature:
GHERKIN;
        $this->gherkin->parse($feature);
    }
Exemplo n.º 5
0
    /**
     * @expectedException \Behat\Gherkin\Exception\ParserException
     */
    public function testTableWithoutRightBorder()
    {
        $feature = <<<GHERKIN
Feature:

    Scenario:
        Given something with:
        | foo | bar
        | 42  | 42
GHERKIN;
        $this->gherkin->parse($feature);
    }
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $align = $input->getOption('align') === Step::ALIGN_TO_LEFT ? Step::ALIGN_TO_LEFT : Step::ALIGN_TO_RIGHT;
     $directory = $input->getArgument('directory');
     $finder = (new FeatureResolve($directory))->__invoke();
     $output->writeln("\nFinding files on <info>" . $directory . "</info>\n");
     $tagFormatter = new Tags();
     $featureDescription = new FeatureDescription();
     $background = new Background($align);
     $scenario = new Scenario($align);
     /* @var $file \Symfony\Component\Finder\SplFileInfo */
     foreach ($finder as $file) {
         $fileContent = $file->getContents();
         $feature = $this->parser->parse($fileContent);
         $formatted = $feature->hasTags() ? $tagFormatter->format($feature->getTags()) . "\n" : '';
         $formatted .= $featureDescription->format($feature) . "\n\n";
         $formatted .= $feature->hasBackground() ? $background->format($feature->getBackground()) . "\n" : '';
         $formatted .= $feature->hasScenarios() ? $scenario->format($feature->getScenarios()) : '';
         $filePointer = $file->openFile('w');
         $filePointer->fwrite($formatted);
         $output->writeln('<info>' . $file->getRealPath() . '</info>');
     }
 }
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $directory = $input->getArgument('directory');
     $finder = new Finder();
     $finder->files()->in($directory)->name('*.feature');
     $output->writeln('');
     $output->writeln('Finding files on <info>' . $directory . '</info>');
     /* @var $file \Symfony\Component\Finder\SplFileInfo */
     foreach ($finder as $file) {
         $feature = $this->parser->parse(file_get_contents($file->getRealpath()));
         $tagFormatter = new Tags();
         $featureDescription = new FeatureDescription();
         $background = new Background();
         $scenario = new Scenario();
         $formatted = $feature->hasTags() ? $tagFormatter->format($feature->getTags()) . PHP_EOL : '';
         $formatted .= $featureDescription->format($feature->getTitle(), explode(PHP_EOL, $feature->getDescription())) . PHP_EOL . PHP_EOL;
         $formatted .= $feature->hasBackground() ? $background->format($feature->getBackground()) . PHP_EOL . PHP_EOL : '';
         $formatted .= $feature->hasScenarios() ? $scenario->format(...$feature->getScenarios()) : '';
         $filePointer = $file->openFile('w');
         $filePointer->fwrite($formatted);
         $output->writeln('');
         $output->writeln('<info>' . $file->getRealpath() . '</info>');
     }
 }
 public function initialize()
 {
     if ($this->initialized) {
         return true;
     }
     $keywords = new \Behat\Gherkin\Keywords\ArrayKeywords(array('en' => array('feature' => 'Feature', 'background' => 'Background', 'scenario' => 'Scenario', 'scenario_outline' => 'Scenario Outline|Scenario Template', 'examples' => 'Examples|Scenarios', 'given' => 'Given', 'when' => 'When', 'then' => 'Then', 'and' => 'And', 'but' => 'But')));
     $keywords->setLanguage('en');
     $lexer = new \Behat\Gherkin\Lexer($keywords);
     $parser = new Parser($lexer);
     foreach ($this->getParameter('templates') as $template) {
         foreach (glob($template) as $file) {
             /** @var $feature \Behat\Gherkin\Node\FeatureNode */
             $feature = $parser->parse(file_get_contents($file));
             /** @var $scenario \Behat\Gherkin\Node\ScenarioNode */
             foreach ($feature->getScenarios() as $scenario) {
                 $this->definitionDispatcher->addDefinition(new \Weavora\MinkExtra\Definition\LazyDefinition($scenario));
             }
         }
     }
     $this->initialized = true;
     return true;
 }
Exemplo n.º 9
0
 public function loadTests($filename)
 {
     $featureNode = $this->parser->parse(file_get_contents($filename), $filename);
     if (!$featureNode) {
         return;
     }
     foreach ($featureNode->getScenarios() as $scenarioNode) {
         /** @var $scenarioNode ScenarioInterface  **/
         $steps = $this->steps['default'];
         // load default context
         foreach (array_merge($scenarioNode->getTags(), $featureNode->getTags()) as $tag) {
             // load tag contexts
             if (isset($this->steps["tag:{$tag}"])) {
                 $steps = array_merge($steps, $this->steps["tag:{$tag}"]);
             }
         }
         $roles = $this->settings['gherkin']['contexts']['role'];
         // load role contexts
         foreach ($roles as $role => $context) {
             $filter = new RoleFilter($role);
             if ($filter->isFeatureMatch($featureNode)) {
                 $steps = array_merge($steps, $this->steps["role:{$role}"]);
                 break;
             }
         }
         if ($scenarioNode instanceof OutlineNode) {
             foreach ($scenarioNode->getExamples() as $example) {
                 /** @var $example ExampleNode  **/
                 $params = implode(', ', $example->getTokens());
                 $exampleNode = new ScenarioNode($scenarioNode->getTitle() . " | {$params}", $scenarioNode->getTags(), $example->getSteps(), $example->getKeyword(), $example->getLine());
                 $this->tests[] = new GherkinFormat($featureNode, $exampleNode, $steps);
             }
             continue;
         }
         $this->tests[] = new GherkinFormat($featureNode, $scenarioNode, $steps);
     }
 }
Exemplo n.º 10
0
 public function generate($steps)
 {
     $feature = $this->generateFeatureForSteps($steps);
     $featureNode = $this->parser->parse($feature);
     return new DummyFeatureNode($featureNode);
 }
Exemplo n.º 11
0
    public function translationTestDataProvider()
    {
        $keywords = $this->getKeywords();
        $lexer = new Lexer($keywords);
        $parser = new Parser($lexer);
        $dumper = new KeywordsDumper($keywords);
        $keywordsArray = $this->getKeywordsArray();
        // Remove languages with repeated keywords
        unset($keywordsArray['en-old'], $keywordsArray['uz']);
        $data = array();
        foreach ($keywordsArray as $lang => $i18nKeywords) {
            $features = array();
            foreach (explode('|', $i18nKeywords['feature']) as $transNum => $featureKeyword) {
                $line = 1;
                if ('en' !== $lang) {
                    $line = 2;
                }
                $featureLine = $line;
                $line += 5;
                $keywords = explode('|', $i18nKeywords['background']);
                $backgroundLine = $line;
                $line += 1;
                $background = new BackgroundNode(null, array_merge($this->getSteps($i18nKeywords['given'], 'there is agent A', $line, 'Given'), $this->getSteps($i18nKeywords['and'], 'there is agent B', $line, 'Given')), $keywords[0], $backgroundLine);
                $line += 1;
                $scenarios = array();
                foreach (explode('|', $i18nKeywords['scenario']) as $scenarioKeyword) {
                    $scenarioLine = $line;
                    $line += 1;
                    $steps = array_merge($this->getSteps($i18nKeywords['given'], 'there is agent J', $line, 'Given'), $this->getSteps($i18nKeywords['and'], 'there is agent K', $line, 'Given'), $this->getSteps($i18nKeywords['when'], 'I erase agent K\'s memory', $line, 'When'), $this->getSteps($i18nKeywords['then'], 'there should be agent J', $line, 'Then'), $this->getSteps($i18nKeywords['but'], 'there should not be agent K', $line, 'Then'));
                    $scenarios[] = new ScenarioNode('Erasing agent memory', array(), $steps, $scenarioKeyword, $scenarioLine);
                    $line += 1;
                }
                foreach (explode('|', $i18nKeywords['scenario_outline']) as $outlineKeyword) {
                    $outlineLine = $line;
                    $line += 1;
                    $steps = array_merge($this->getSteps($i18nKeywords['given'], 'there is agent <agent1>', $line, 'Given'), $this->getSteps($i18nKeywords['and'], 'there is agent <agent2>', $line, 'Given'), $this->getSteps($i18nKeywords['when'], 'I erase agent <agent2>\'s memory', $line, 'When'), $this->getSteps($i18nKeywords['then'], 'there should be agent <agent1>', $line, 'Then'), $this->getSteps($i18nKeywords['but'], 'there should not be agent <agent2>', $line, 'Then'));
                    $line += 1;
                    $keywords = explode('|', $i18nKeywords['examples']);
                    $table = new ExampleTableNode(array(++$line => array('agent1', 'agent2'), ++$line => array('D', 'M')), $keywords[0]);
                    $line += 1;
                    $scenarios[] = new OutlineNode('Erasing other agents\' memory', array(), $steps, $table, $outlineKeyword, $outlineLine);
                    $line += 1;
                }
                $features[] = new FeatureNode('Internal operations', <<<DESC
In order to stay secret
As a secret organization
We need to be able to erase past agents' memory
DESC
, array(), $background, $scenarios, $featureKeyword, $lang, $lang . '_' . ($transNum + 1) . '.feature', $featureLine);
            }
            $dumped = $dumper->dump($lang, false, true);
            $parsed = array();
            try {
                foreach ($dumped as $num => $dumpedFeature) {
                    $parsed[] = $parser->parse($dumpedFeature, $lang . '_' . ($num + 1) . '.feature');
                }
            } catch (\Exception $e) {
                throw new \Exception($e->getMessage() . ":\n" . json_encode($dumped), 0, $e);
            }
            $data[] = array($lang, $features, $parsed);
        }
        return $data;
    }
Exemplo n.º 12
0
    public function translationTestDataProvider()
    {
        $keywords = $this->getKeywords();
        $lexer = new Lexer($keywords);
        $parser = new Parser($lexer);
        $dumper = new KeywordsDumper($keywords);
        $data = array();
        foreach ($this->getKeywordsArray() as $lang => $i18nKeywords) {
            $features = array();
            foreach (explode('|', $i18nKeywords['feature']) as $transNum => $featureKeyword) {
                $line = 1;
                if ('en' !== $lang) {
                    $line = 2;
                }
                $feature = new Node\FeatureNode('Internal operations', <<<DESC
In order to stay secret
As a secret organization
We need to be able to erase past agents' memory
DESC
, $lang . '_' . ($transNum + 1) . '.feature', $line);
                $feature->setLanguage($lang);
                $feature->setKeyword($featureKeyword);
                $line += 5;
                $background = new Node\BackgroundNode(null, $line);
                $keywords = explode('|', $i18nKeywords['background']);
                $background->setKeyword($keywords[0]);
                $line += 1;
                $line = $this->addSteps($background, $i18nKeywords['given'], 'there is agent A', $line);
                $line = $this->addSteps($background, $i18nKeywords['and'], 'there is agent B', $line);
                $feature->setBackground($background);
                $line += 1;
                foreach (explode('|', $i18nKeywords['scenario']) as $scenarioKeyword) {
                    $scenario = new Node\ScenarioNode('Erasing agent memory', $line);
                    $scenario->setKeyword($scenarioKeyword);
                    $line += 1;
                    $line = $this->addSteps($scenario, $i18nKeywords['given'], 'there is agent J', $line);
                    $line = $this->addSteps($scenario, $i18nKeywords['and'], 'there is agent K', $line);
                    $line = $this->addSteps($scenario, $i18nKeywords['when'], 'I erase agent K\'s memory', $line);
                    $line = $this->addSteps($scenario, $i18nKeywords['then'], 'there should be agent J', $line);
                    $line = $this->addSteps($scenario, $i18nKeywords['but'], 'there should not be agent K', $line);
                    $feature->addScenario($scenario);
                    $line += 1;
                }
                foreach (explode('|', $i18nKeywords['scenario_outline']) as $outlineKeyword) {
                    $outline = new Node\OutlineNode('Erasing other agents\' memory', $line);
                    $outline->setKeyword($outlineKeyword);
                    $line += 1;
                    $line = $this->addSteps($outline, $i18nKeywords['given'], 'there is agent <agent1>', $line);
                    $line = $this->addSteps($outline, $i18nKeywords['and'], 'there is agent <agent2>', $line);
                    $line = $this->addSteps($outline, $i18nKeywords['when'], 'I erase agent <agent2>\'s memory', $line);
                    $line = $this->addSteps($outline, $i18nKeywords['then'], 'there should be agent <agent1>', $line);
                    $line = $this->addSteps($outline, $i18nKeywords['but'], 'there should not be agent <agent2>', $line);
                    $line += 1;
                    $outline->setExamples($examples = new Node\TableNode(<<<TABLE
                      | agent1 | agent2 |
                      | D      | M      |
TABLE
));
                    $keywords = explode('|', $i18nKeywords['examples']);
                    $examples->setKeyword($keywords[0]);
                    $line += 3;
                    $feature->addScenario($outline);
                    $line += 1;
                }
                $features[] = $feature;
            }
            $dumped = $dumper->dump($lang, false);
            $parsed = array();
            try {
                foreach ($dumped as $num => $dumpedFeature) {
                    $parsed[] = $parser->parse($dumpedFeature, $lang . '_' . ($num + 1) . '.feature');
                }
            } catch (\Exception $e) {
                throw new \Exception($e->getMessage() . ":\n" . $dumped, 0, $e);
            }
            $data[] = array($lang, $features, $parsed);
        }
        return $data;
    }