Completed
Pull Request — master (#147)
by Christophe
03:26
created

KeywordsTest::translationTestDataProvider()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 97

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 97
rs 7.4432
c 0
b 0
f 0
cc 6
nc 10
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Behat\Gherkin\Keywords;
4
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;
13
14
abstract class KeywordsTest extends \PHPUnit_Framework_TestCase
15
{
16
    abstract protected function getKeywords();
17
    abstract protected function getKeywordsArray();
18
    abstract protected function getSteps($keywords, $text, &$line, $keywordType);
19
20
    public function translationTestDataProvider()
21
    {
22
        $keywords = $this->getKeywords();
23
        $dumper = new KeywordsDumper($keywords);
24
        $keywordsArray = $this->getKeywordsArray();
25
26
        // Remove languages with repeated keywords
27
        unset($keywordsArray['en-old'], $keywordsArray['uz']);
28
29
        $data = array();
30
        foreach ($keywordsArray as $lang => $i18nKeywords) {
31
            $features = array();
32
            foreach (explode('|', $i18nKeywords['feature']) as $transNum => $featureKeyword) {
33
                $line = 1;
34
                if ('en' !== $lang) {
35
                    $line = 2;
36
                }
37
38
                $featureLine = $line;
39
                $line += 5;
40
41
                $keywords = explode('|', $i18nKeywords['background']);
42
                $backgroundLine = $line;
43
                $line += 1;
44
                $background = new BackgroundNode(null, array_merge(
45
                    $this->getSteps($i18nKeywords['given'], 'there is agent A', $line, 'Given'),
46
                    $this->getSteps($i18nKeywords['and'], 'there is agent B', $line, 'Given')
47
                ), $keywords[0], $backgroundLine);
48
49
                $line += 1;
50
51
                $scenarios = array();
52
53
                foreach (explode('|', $i18nKeywords['scenario']) as $scenarioKeyword) {
54
                    $scenarioLine = $line;
55
                    $line += 1;
56
57
                    $steps = array_merge(
58
                        $this->getSteps($i18nKeywords['given'], 'there is agent J', $line, 'Given'),
59
                        $this->getSteps($i18nKeywords['and'], 'there is agent K', $line, 'Given'),
60
                        $this->getSteps($i18nKeywords['when'], 'I erase agent K\'s memory', $line, 'When'),
61
                        $this->getSteps($i18nKeywords['then'], 'there should be agent J', $line, 'Then'),
62
                        $this->getSteps($i18nKeywords['but'], 'there should not be agent K', $line, 'Then')
63
                    );
64
65
                    $scenarios[] = new ScenarioNode('Erasing agent memory', array(), $steps, $scenarioKeyword, $scenarioLine);
66
                    $line += 1;
67
                }
68
                foreach (explode('|', $i18nKeywords['scenario_outline']) as $outlineKeyword) {
69
                    $outlineLine = $line;
70
                    $line += 1;
71
72
                    $steps = array_merge(
73
                        $this->getSteps($i18nKeywords['given'], 'there is agent <agent1>', $line, 'Given'),
74
                        $this->getSteps($i18nKeywords['and'], 'there is agent <agent2>', $line, 'Given'),
75
                        $this->getSteps($i18nKeywords['when'], 'I erase agent <agent2>\'s memory', $line, 'When'),
76
                        $this->getSteps($i18nKeywords['then'], 'there should be agent <agent1>', $line, 'Then'),
77
                        $this->getSteps($i18nKeywords['but'], 'there should not be agent <agent2>', $line, 'Then')
78
                    );
79
                    $line += 1;
80
81
                    $keywords = explode('|', $i18nKeywords['examples']);
82
                    $table = new ExampleTableNode(array(
83
                        ++$line => array('agent1', 'agent2'),
84
                        ++$line => array('D', 'M')
85
                    ), $keywords[0]);
86
                    $line += 1;
87
88
                    $scenarios[] = new OutlineNode('Erasing other agents\' memory', array(), $steps, $table, $outlineKeyword, $outlineLine);
89
                    $line += 1;
90
                }
91
92
                $features[] = new FeatureNode(
93
                    'Internal operations',
94
                    <<<DESC
95
In order to stay secret
96
As a secret organization
97
We need to be able to erase past agents' memory
98
DESC
99
                    ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
100
                    array(),
101
                    $background,
102
                    $scenarios,
103
                    $featureKeyword,
104
                    $lang,
105
                    $lang . '_' . ($transNum + 1) . '.feature',
106
                    $featureLine
107
                );
108
            }
109
110
            $dumped = $dumper->dump($lang, false, true);
111
112
            $data[] = array($lang, $features, $dumped);
113
        }
114
115
        return $data;
116
    }
117
118
    /**
119
     * @dataProvider translationTestDataProvider
120
     *
121
     * @param string   $language language name
122
     * @param array    $etalon   etalon features (to test against)
123
     * @param string[] $features gherkin features
124
     */
125
    public function testTranslation($language, array $etalon, array $features)
126
    {
127
        $keywords = $this->getKeywords();
128
        $lexer = new Lexer($keywords);
129
        $parser = new Parser($lexer);
130
131
        $parsed = array();
132
        try {
133
            foreach ($features as $num => $dumpedFeature) {
134
                $parsed[] = $parser->parse($dumpedFeature, $language . '_' . ($num + 1) . '.feature');
135
            }
136
        } catch (\Exception $e) {
137
            throw new \Exception($e->getMessage() . ":\n" . json_encode($features), 0, $e);
138
        }
139
140
        $this->assertEquals($etalon, $parsed);
141
    }
142
}
143