Ejemplo n.º 1
0
 /**
  * Loads feature from provided feature hash.
  *
  * @param array   $hash Feature hash
  * @param integer $line Feature definition line
  *
  * @return FeatureNode
  */
 protected function loadFeatureHash(array $hash, $line = 0)
 {
     $feature = new Node\FeatureNode(null, null, null, isset($hash['line']) ? $hash['line'] : $line);
     $feature->setKeyword(isset($hash['keyword']) ? $hash['keyword'] : 'Feature');
     if (isset($hash['title'])) {
         $feature->setTitle($hash['title']);
     }
     if (isset($hash['description'])) {
         $feature->setDescription($hash['description']);
     }
     if (isset($hash['tags'])) {
         $feature->setTags($hash['tags']);
     }
     if (isset($hash['language'])) {
         $feature->setLanguage($hash['language']);
     }
     if (isset($hash['background'])) {
         $feature->setBackground($this->loadBackgroundHash($hash['background']));
     }
     if (isset($hash['scenarios'])) {
         foreach ($hash['scenarios'] as $scenarioIterator => $scenarioHash) {
             if (isset($scenarioHash['type']) && 'outline' === $scenarioHash['type']) {
                 $feature->addScenario($this->loadOutlineHash($scenarioHash, $scenarioIterator));
             } else {
                 $feature->addScenario($this->loadScenarioHash($scenarioHash, $scenarioIterator));
             }
         }
     }
     return $feature;
 }
Ejemplo n.º 2
0
 public function testCacheAndRead()
 {
     $feature = new FeatureNode('Some feature', 'some description');
     $feature->addScenario(new ScenarioNode('Some scenario'));
     $this->cache->write('some_feature', $feature);
     $featureRead = $this->cache->read('some_feature');
     $this->assertEquals($feature, $featureRead);
 }
Ejemplo n.º 3
0
 public function testScenarios()
 {
     $feature = new FeatureNode();
     $this->assertEquals(0, count($feature->getScenarios()));
     $this->assertFalse($feature->hasScenarios());
     $feature->addScenario(new ScenarioNode());
     $this->assertEquals(1, count($feature->getScenarios()));
     $this->assertTrue($feature->hasScenarios());
     $feature->addScenario(new OutlineNode());
     $this->assertEquals(2, count($feature->getScenarios()));
     $this->assertTrue($feature->hasScenarios());
     $scenarios = $feature->getScenarios();
     $this->assertInstanceOf('Behat\\Gherkin\\Node\\ScenarioNode', $scenarios[0]);
     $this->assertSame($feature, $scenarios[0]->getFeature());
     $this->assertInstanceOf('Behat\\Gherkin\\Node\\OutlineNode', $scenarios[1]);
     $this->assertSame($feature, $scenarios[1]->getFeature());
 }
Ejemplo n.º 4
0
 public function testTokens()
 {
     $step = new StepNode('When', 'Some "<text>" in <string>');
     $scenario = new ScenarioNode();
     $scenario->addStep($step);
     $feature = new FeatureNode();
     $feature->addScenario($scenario);
     $feature->freeze();
     $step1 = $step->createExampleRowStep(array('text' => 'change'));
     $this->assertNotSame($step, $step1);
     $this->assertEquals('Some "change" in <string>', $step1->getText());
     $this->assertEquals('Some "<text>" in <string>', $step1->getCleanText());
     $step2 = $step->createExampleRowStep(array('text' => 'change', 'string' => 'browser'));
     $this->assertNotSame($step, $step2);
     $this->assertEquals('Some "change" in browser', $step2->getText());
     $this->assertEquals('Some "<text>" in <string>', $step2->getCleanText());
 }
Ejemplo n.º 5
0
 public function testLoader()
 {
     $gherkin = new Gherkin();
     $gherkin->addLoader($loader = $this->getLoaderMock());
     $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
     $gherkin->addFilter($tagFilter = $this->getTagFilterMock());
     $feature = new FeatureNode();
     $feature->addScenario($scenario = new ScenarioNode());
     $loader->expects($this->once())->method('supports')->with($resource = 'some/feature/resource')->will($this->returnValue(true));
     $loader->expects($this->once())->method('load')->with($resource)->will($this->returnValue(array($feature)));
     $nameFilter->expects($this->once())->method('isScenarioMatch')->with($scenario)->will($this->returnValue(true));
     $tagFilter->expects($this->once())->method('isScenarioMatch')->with($scenario)->will($this->returnValue(true));
     $features = $gherkin->load($resource);
     $this->assertEquals(1, count($features));
     $scenarios = $features[0]->getScenarios();
     $this->assertEquals(1, count($scenarios));
     $this->assertSame($scenario, $scenarios[0]);
 }
Ejemplo n.º 6
0
 public function testLoader()
 {
     $customFilter1 = $this->getCustomFilterMock();
     $customFilter2 = $this->getCustomFilterMock();
     $gherkin = new Gherkin();
     $gherkin->addLoader($loader = $this->getLoaderMock());
     $gherkin->addFilter($nameFilter = $this->getNameFilterMock());
     $gherkin->addFilter($tagFilter = $this->getTagFilterMock());
     $feature = new FeatureNode();
     $feature->addScenario($scenario = new ScenarioNode());
     $loader->expects($this->once())->method('supports')->with($resource = 'some/feature/resource')->will($this->returnValue(true));
     $loader->expects($this->once())->method('load')->with($resource)->will($this->returnValue(array($feature)));
     $nameFilter->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature));
     $tagFilter->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature));
     $customFilter1->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature));
     $customFilter2->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature));
     $features = $gherkin->load($resource, array($customFilter1, $customFilter2));
     $this->assertEquals(1, count($features));
     $this->assertTrue($feature->isFrozen());
     $scenarios = $features[0]->getScenarios();
     $this->assertEquals(1, count($scenarios));
     $this->assertSame($scenario, $scenarios[0]);
 }
Ejemplo n.º 7
0
 public function testIsScenarioMatchFilter()
 {
     $feature = new Node\FeatureNode();
     $scenario = new Node\ScenarioNode();
     $feature->addScenario($scenario);
     $feature->setTitle('random feature title');
     $scenario->setTitle('UNKNOWN');
     $filter = new NameFilter('feature1');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->setTitle('feature1');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTitle('feature1 title');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTitle('some feature1 title');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTitle('some feature title');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $filter = new NameFilter('/fea.ure/');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTitle('some feaSure title');
     $scenario->setTitle('some feature title');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTitle('some feture title');
     $scenario->setTitle('unk');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->setTitle('unknown');
     $scenario->setTitle('simple scenario title');
     $filter = new NameFilter('scenario');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $scenario->setTitle('simple feature title');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $scenario->setTitle('simple scenerio title');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $filter = new NameFilter('/scen.rio/');
     $this->assertTrue($filter->isScenarioMatch($scenario));
 }
Ejemplo n.º 8
0
 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;
 }
Ejemplo n.º 9
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;
    }
Ejemplo n.º 10
0
 public function testIsScenarioMatchFilter()
 {
     $feature = new Node\FeatureNode();
     $scenario = new Node\ScenarioNode();
     $feature->addScenario($scenario);
     $filter = new TagFilter('@wip');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->addTag('wip');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $filter = new TagFilter('~@done');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->addTag('done');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->setTags(array('tag1', 'tag2', 'tag3'));
     $filter = new TagFilter('@tag5,@tag4,@tag6');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->addTag('tag5');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $filter = new TagFilter('@wip&&@vip');
     $feature->setTags(array('wip', 'not-done'));
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->addTag('vip');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $filter = new TagFilter('@wip,@vip&&@user');
     $feature->setTags(array('wip'));
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->setTags(array('vip'));
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->setTags(array('wip', 'user'));
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTags(array('vip', 'user'));
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTags(array());
     $filter = new TagFilter('@wip');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->addTag('wip');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $filter = new TagFilter('~@done');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->addTag('done');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $scenario->setTags(array('tag1', 'tag2', 'tag3'));
     $filter = new TagFilter('@tag5,@tag4,@tag6');
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->addTag('tag5');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $filter = new TagFilter('@wip&&@vip');
     $scenario->setTags(array('wip', 'not-done'));
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $feature->addTag('vip');
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $filter = new TagFilter('@wip,@vip&&@user');
     $scenario->setTags(array('wip'));
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $scenario->setTags(array('vip'));
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $scenario->setTags(array('wip', 'user'));
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $scenario->setTags(array('vip', 'user'));
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTags(array('wip'));
     $scenario->setTags(array('user'));
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $feature->setTags(array());
     $this->assertFalse($filter->isScenarioMatch($scenario));
     $filter = new TagFilter('@wip,@vip&&~@group');
     $feature->setTags(array('vip'));
     $this->assertTrue($filter->isScenarioMatch($scenario));
     $scenario->addTag('group');
     $this->assertFalse($filter->isScenarioMatch($scenario));
 }
Ejemplo n.º 11
0
 public function addScenario(ScenarioNode $scenario)
 {
     $this->featureNode->addScenario($scenario);
 }