public function testFeature()
 {
     $background = new BackgroundNode();
     $this->assertNull($background->getFeature());
     $background->setFeature($feature = new FeatureNode());
     $this->assertSame($feature, $background->getFeature());
 }
 /**
  * Visits & tests BackgroundNode.
  *
  * @param BackgroundNode   $background
  * @param ScenarioNode     $logicalParent
  * @param ContextInterface $context
  *
  * @see BackgroundTester::visit()
  *
  * @return integer
  */
 protected function visitBackground(BackgroundNode $background, ScenarioNode $logicalParent, ContextInterface $context)
 {
     $tester = $this->container->get('behat.tester.background');
     $tester->setLogicalParent($logicalParent);
     $tester->setContext($context);
     $tester->setSkip($this->moodleskip);
     return $background->accept($tester);
 }
 /**
  * Dumps a background.
  *
  * @param BackgroundNode $background Background instance
  *
  * @return string
  */
 public function dumpBackground(BackgroundNode $background)
 {
     $content = $this->dumpKeyword($this->keywords->getBackgroundKeywords(), $background->getTitle());
     foreach ($background->getSteps() as $step) {
         $content .= PHP_EOL . $this->dumpIndent(1) . $this->dumpStep($step);
     }
     return $content;
 }
   public function testDumpBackgroundReturnsWellFormatedContent()
   {
       $dumper = new Dumper($this->keywords);
       $background = new BackgroundNode('my title');
       $background->addStep(new StepNode('Given', 'I use behat'));
       $expected = 'Background: my title
 Given I use behat';
       $this->assertEquals($expected, $dumper->dumpBackground($background));
   }
Beispiel #5
0
 /**
  * Visits & tests BackgroundNode.
  *
  * @param   Behat\Gherkin\Node\BackgroundNode       $background
  * @param   Behat\Behat\Context\ContextInterface    $context
  *
  * @uses    Behat\Behat\Tester\BackgroundTester::visit()
  *
  * @return  integer
  */
 protected function visitBackground(BackgroundNode $background, ContextInterface $context)
 {
     $tester = $this->container->get('behat.tester.background');
     $tester->setContext($context);
     $tester->setDryRun($this->dryRun);
     return $background->accept($tester);
 }
 public function translationTestDataProvider()
 {
     $data = array();
     $translator = $this->getTranslator();
     $parser = $this->getParser();
     $finder = new Finder();
     $i18ns = $finder->files()->name('*.xliff')->in(__DIR__ . '/../Fixtures/i18n');
     foreach ($i18ns as $i18n) {
         $language = basename($i18n, '.xliff');
         $translator->addResource('xliff', $i18n, $language, 'gherkin');
         $etalon = array();
         $features = array();
         foreach ($this->getTranslatedKeywords('Feature', $language) as $featureNum => $featureKeyword) {
             $gherkin = "# language: {$language}";
             $lineNum = 1;
             $feature = new Node\FeatureNode(null, null, null, ++$lineNum);
             $feature->setLanguage($language);
             $feature->setKeyword($featureKeyword);
             $feature->setTitle($title = "title of the feature N{$featureNum}");
             $feature->setDescription($description = "some\nfeature\ndescription");
             $gherkin .= "\n{$featureKeyword}: {$title}";
             $gherkin .= "\n{$description}";
             $lineNum += 3;
             $stepKeywords = $this->getTranslatedKeywords('Given|When|Then|And|But', $language);
             $backgroundKeywords = $this->getTranslatedKeywords('Background', $language);
             $examplesKeywords = $this->getTranslatedKeywords('Examples', $language);
             // Background
             $backgroundKeyword = $backgroundKeywords[0];
             $background = new Node\BackgroundNode(null, ++$lineNum);
             $background->setKeyword($backgroundKeyword);
             $feature->setBackground($background);
             $gherkin .= "\n{$backgroundKeyword}:";
             foreach ($stepKeywords as $stepNum => $stepKeyword) {
                 $step = new Node\StepNode($stepKeyword, $text = "text of the step N{$stepNum}", ++$lineNum);
                 $background->addStep($step);
                 $gherkin .= "\n{$stepKeyword} {$text}";
             }
             // Scenarios
             foreach ($this->getTranslatedKeywords('Scenario', $language) as $scenarioNum => $scenarioKeyword) {
                 $scenario = new Node\ScenarioNode($title = "title of the scenario N{$scenarioNum}", ++$lineNum);
                 $scenario->setKeyword($scenarioKeyword);
                 $feature->addScenario($scenario);
                 $gherkin .= "\n{$scenarioKeyword}: {$title}";
                 foreach ($stepKeywords as $stepNum => $stepKeyword) {
                     $step = new Node\StepNode($stepKeyword, $text = "text of the step N{$stepNum}", ++$lineNum);
                     $scenario->addStep($step);
                     $gherkin .= "\n{$stepKeyword} {$text}";
                 }
             }
             // Scenario Outlines
             foreach ($this->getTranslatedKeywords('Scenario Outline', $language) as $outlineNum => $outlineKeyword) {
                 $outline = new Node\OutlineNode($title = "title of the outline N{$outlineNum}", ++$lineNum);
                 $outline->setKeyword($outlineKeyword);
                 $feature->addScenario($outline);
                 $gherkin .= "\n{$outlineKeyword}: {$title}";
                 $stepKeyword = $stepKeywords[0];
                 $step = new Node\StepNode($stepKeyword, $text = "text of the step <num>", ++$lineNum);
                 $outline->addStep($step);
                 $gherkin .= "\n{$stepKeyword} {$text}";
                 $examplesKeyword = $examplesKeywords[0];
                 $examples = new Node\TableNode();
                 $examples->setKeyword($examplesKeyword);
                 $examples->addRow(array('num'));
                 $examples->addRow(array(2));
                 $outline->setExamples($examples);
                 $gherkin .= "\n{$examplesKeyword}:";
                 $gherkin .= "\n  | num |";
                 $gherkin .= "\n  | 2   |";
                 $lineNum += 3;
             }
             $etalon[] = $feature;
             $features[] = $this->getParser()->parse($gherkin);
         }
         $data[] = array($language, $etalon, $features);
     }
     return $data;
 }
Beispiel #7
0
 /**
  * Loads background from provided hash.
  *
  * @param array $hash Background hash
  *
  * @return BackgroundNode
  */
 protected function loadBackgroundHash(array $hash)
 {
     $background = new Node\BackgroundNode(null, isset($hash['line']) ? $hash['line'] : 0);
     $background->setKeyword(isset($hash['keyword']) ? $hash['keyword'] : 'Background');
     if (isset($hash['title'])) {
         $background->setTitle($hash['title']);
     }
     if (isset($hash['steps'])) {
         foreach ($hash['steps'] as $stepIterator => $stepHash) {
             $background->addStep($this->loadStepHash($stepHash, $stepIterator));
         }
     }
     return $background;
 }
Beispiel #8
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;
    }
 /**
  * @param BackgroundNode $background
  *
  * @return string
  */
 private function getBackgroundShortDescription(BackgroundNode $background)
 {
     return trim(sprintf('%s: %s', trim($background->getKeyword()), trim($background->getTitle())));
 }
Beispiel #10
0
 /**
  * Parses background token & returns it's node.
  *
  * @return  Behat\Gherkin\Node\BackgroundNode
  */
 protected function parseBackground()
 {
     $token = $this->expectTokenType('Background');
     $node = new Node\BackgroundNode(trim($token->value) ?: null, $token->line);
     $node->setKeyword($token->keyword);
     $this->skipComments();
     // Parse title
     $this->parseNodeDescription($node, $token->indent + 2);
     // Parse steps
     while ('Step' === $this->predictTokenType()) {
         $node->addStep($this->parseExpression());
     }
     return $node;
 }
Beispiel #11
0
 /**
  * Visits & tests BackgroundNode.
  *
  * @param   Behat\Gherkin\Node\BackgroundNode               $background
  * @param   Behat\Behat\Environment\EnvironmentInterface    $environment
  *
  * @uses    Behat\Behat\Tester\BackgroundTester::visit()
  *
  * @return  integer
  */
 protected function visitBackground(BackgroundNode $background, EnvironmentInterface $environment)
 {
     $tester = $this->container->get('behat.tester.background');
     $tester->setEnvironment($environment);
     return $background->accept($tester);
 }