Completed
Pull Request — master (#112)
by Christophe
02:45
created

KeywordsTest::translationTestDataProvider()   C

Complexity

Conditions 8
Paths 28

Size

Total Lines 107
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 107
rs 5.2676
cc 8
eloc 75
nc 28
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
        $lexer = new Lexer($keywords);
24
        $parser = new Parser($lexer);
25
        $dumper = new KeywordsDumper($keywords);
26
        $keywordsArray = $this->getKeywordsArray();
27
28
        // Remove languages with repeated keywords
29
        unset($keywordsArray['en-old'], $keywordsArray['uz']);
30
31
        $data = array();
32
        foreach ($keywordsArray as $lang => $i18nKeywords) {
33
            $features = array();
34
            foreach (explode('|', $i18nKeywords['feature']) as $transNum => $featureKeyword) {
35
                $line = 1;
36
                if ('en' !== $lang) {
37
                    $line = 2;
38
                }
39
40
                $featureLine = $line;
41
                $line += 5;
42
43
                $keywords = explode('|', $i18nKeywords['background']);
44
                $backgroundLine = $line;
45
                $line += 1;
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);
50
51
                $line += 1;
52
53
                $scenarios = array();
54
55
                foreach (explode('|', $i18nKeywords['scenario']) as $scenarioKeyword) {
56
                    $scenarioLine = $line;
57
                    $line += 1;
58
59
                    $steps = array_merge(
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')
65
                    );
66
67
                    $scenarios[] = new ScenarioNode('Erasing agent memory', array(), $steps, $scenarioKeyword, $scenarioLine);
68
                    $line += 1;
69
                }
70
                foreach (explode('|', $i18nKeywords['scenario_outline']) as $outlineKeyword) {
71
                    $outlineLine = $line;
72
                    $line += 1;
73
74
                    $steps = array_merge(
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')
80
                    );
81
                    $line += 1;
82
83
                    $keywords = explode('|', $i18nKeywords['examples']);
84
                    $table = new ExampleTableNode(array(
85
                        ++$line => array('agent1', 'agent2'),
86
                        ++$line => array('D', 'M')
87
                    ), $keywords[0]);
88
                    $line += 1;
89
90
                    $scenarios[] = new OutlineNode('Erasing other agents\' memory', array(), $steps, $table, $outlineKeyword, $outlineLine);
91
                    $line += 1;
92
                }
93
94
                $features[] = new FeatureNode(
95
                    'Internal operations',
96
                    <<<DESC
97
In order to stay secret
98
As a secret organization
99
We need to be able to erase past agents' memory
100
DESC
101
                    ,
0 ignored issues
show
Coding Style introduced by
Space found before comma in function call
Loading history...
102
                    array(),
103
                    $background,
104
                    $scenarios,
105
                    $featureKeyword,
106
                    $lang,
107
                    $lang . '_' . ($transNum + 1) . '.feature',
108
                    $featureLine
109
                );
110
            }
111
112
            $dumped = $dumper->dump($lang, false, true);
113
            $parsed = array();
114
            try {
115
                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...
116
                    $parsed[] = $parser->parse($dumpedFeature, $lang . '_' . ($num + 1) . '.feature');
117
                }
118
            } catch (\Exception $e) {
119
                throw new \Exception($e->getMessage() . ":\n" . json_encode($dumped), 0, $e);
120
            }
121
122
            $data[] = array($lang, $features, $parsed);
123
        }
124
125
        return $data;
126
    }
127
128
    /**
129
     * @dataProvider translationTestDataProvider
130
     *
131
     * @param string $language language name
132
     * @param array  $etalon   etalon features (to test against)
133
     * @param array  $features array of parsed feature(s)
134
     */
135
    public function testTranslation($language, array $etalon, array $features)
0 ignored issues
show
Unused Code introduced by
The parameter $language is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
        $this->assertEquals($etalon, $features);
138
    }
139
}
140