Completed
Push — master ( 26056a...749be1 )
by Christophe
03:21 queued 10s
created

KeywordsTest::translationTestDataProvider()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 19
nop 0
dl 0
loc 99
rs 7.0884
c 0
b 0
f 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
            foreach ($dumped as $num => $dumpedFeature) {
0 ignored issues
show
Bug introduced by
The expression $dumped of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
113
                $data[] = array($lang, $num, $features[$num], $dumpedFeature);
114
            }
115
        }
116
117
        return $data;
118
    }
119
120
    /**
121
     * @dataProvider translationTestDataProvider
122
     *
123
     * @param string      $language language name
124
     * @param int         $num      Fixture index for that language
125
     * @param FeatureNode $etalon   etalon features (to test against)
126
     * @param string      $source   gherkin source
127
     */
128
    public function testTranslation($language, $num, FeatureNode $etalon, $source)
129
    {
130
        $keywords = $this->getKeywords();
131
        $lexer = new Lexer($keywords);
132
        $parser = new Parser($lexer);
133
134
        try {
135
            $parsed = $parser->parse($source, $language . '_' . ($num + 1) . '.feature');
136
        } catch (\Exception $e) {
137
            throw new \Exception($e->getMessage() . ":\n" . $source, 0, $e);
138
        }
139
140
        $this->assertEquals($etalon, $parsed);
141
    }
142
}
143