3 namespace Tests\Behat\Gherkin\Keywords;
5 use Behat\Gherkin\Keywords\KeywordsDumper;
6 use Behat\Gherkin\Lexer;
7 use Behat\Gherkin\Node\BackgroundNode;
8 use Behat\Gherkin\Node\ExampleTableNode;
9 use Behat\Gherkin\Node\FeatureNode;
10 use Behat\Gherkin\Node\OutlineNode;
11 use Behat\Gherkin\Node\ScenarioNode;
12 use Behat\Gherkin\Parser;
14 abstract class KeywordsTest extends \PHPUnit_Framework_TestCase
16 abstract protected function getKeywords();
17 abstract protected function getKeywordsArray();
18 abstract protected function getSteps($keywords, $text, &$line, $keywordType);
20 public function translationTestDataProvider()
22 $keywords = $this->getKeywords();
23 $lexer = new Lexer($keywords);
24 $parser = new Parser($lexer);
25 $dumper = new KeywordsDumper($keywords);
26 $keywordsArray = $this->getKeywordsArray();
28 // Remove languages with repeated keywords
29 unset($keywordsArray['en-old'], $keywordsArray['uz']);
32 foreach ($keywordsArray as $lang => $i18nKeywords) {
34 foreach (explode('|', $i18nKeywords['feature']) as $transNum => $featureKeyword) {
43 $keywords = explode('|', $i18nKeywords['background']);
44 $backgroundLine = $line;
46 $background = new BackgroundNode(null, array_merge(
47 $this->getSteps($i18nKeywords['given'], 'there is agent A', $line, 'Given'),
48 $this->getSteps($i18nKeywords['and'], 'there is agent B', $line, 'Given')
49 ), $keywords[0], $backgroundLine);
55 foreach (explode('|', $i18nKeywords['scenario']) as $scenarioKeyword) {
56 $scenarioLine = $line;
60 $this->getSteps($i18nKeywords['given'], 'there is agent J', $line, 'Given'),
61 $this->getSteps($i18nKeywords['and'], 'there is agent K', $line, 'Given'),
62 $this->getSteps($i18nKeywords['when'], 'I erase agent K\'s memory', $line, 'When'),
63 $this->getSteps($i18nKeywords['then'], 'there should be agent J', $line, 'Then'),
64 $this->getSteps($i18nKeywords['but'], 'there should not be agent K', $line, 'Then')
67 $scenarios[] = new ScenarioNode('Erasing agent memory', array(), $steps, $scenarioKeyword, $scenarioLine);
70 foreach (explode('|', $i18nKeywords['scenario_outline']) as $outlineKeyword) {
75 $this->getSteps($i18nKeywords['given'], 'there is agent <agent1>', $line, 'Given'),
76 $this->getSteps($i18nKeywords['and'], 'there is agent <agent2>', $line, 'Given'),
77 $this->getSteps($i18nKeywords['when'], 'I erase agent <agent2>\'s memory', $line, 'When'),
78 $this->getSteps($i18nKeywords['then'], 'there should be agent <agent1>', $line, 'Then'),
79 $this->getSteps($i18nKeywords['but'], 'there should not be agent <agent2>', $line, 'Then')
83 $keywords = explode('|', $i18nKeywords['examples']);
84 $table = new ExampleTableNode(array(
85 ++$line => array('agent1', 'agent2'),
86 ++$line => array('D', 'M')
90 $scenarios[] = new OutlineNode('Erasing other agents\' memory', array(), $steps, $table, $outlineKeyword, $outlineLine);
94 $features[] = new FeatureNode(
95 'Internal operations',
97 In order to stay secret
98 As a secret organization
99 We need to be able to erase past agents' memory
107 $lang . '_' . ($transNum + 1) . '.feature',
112 $dumped = $dumper->dump($lang, false, true);
115 foreach ($dumped as $num => $dumpedFeature) {
116 $parsed[] = $parser->parse($dumpedFeature, $lang . '_' . ($num + 1) . '.feature');
118 } catch (\Exception $e) {
119 throw new \Exception($e->getMessage() . ":\n" . json_encode($dumped), 0, $e);
122 $data[] = array($lang, $features, $parsed);
129 * @dataProvider translationTestDataProvider
131 * @param string $language language name
132 * @param array $etalon etalon features (to test against)
133 * @param array $features array of parsed feature(s)
135 public function testTranslation($language, array $etalon, array $features)
137 $this->assertEquals($etalon, $features);