Completed
Push — master ( faf631...885177 )
by Konstantin
11s
created

ParserTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 116
wmc 9
lcom 1
cbo 6
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parserTestDataProvider() 0 12 2
A testParser() 0 11 1
A parseFixture() 0 6 1
A parseEtalon() 0 17 1
B getGherkinParser() 0 46 2
A getYamlParser() 0 8 2
1
<?php
2
3
namespace Tests\Behat\Gherkin;
4
5
use Behat\Gherkin\Node\FeatureNode;
6
use Behat\Gherkin\Lexer;
7
use Behat\Gherkin\Parser;
8
use Behat\Gherkin\Keywords\ArrayKeywords;
9
use Behat\Gherkin\Loader\YamlFileLoader;
10
11
class ParserTest extends \PHPUnit_Framework_TestCase
12
{
13
    private $gherkin;
14
    private $yaml;
15
16
    public function parserTestDataProvider()
17
    {
18
        $data = array();
19
20
        foreach (glob(__DIR__ . '/Fixtures/etalons/*.yml') as $file) {
21
            $testname = basename($file, '.yml');
22
23
            $data[] = array($testname);
24
        }
25
26
        return $data;
27
    }
28
29
    /**
30
     * @dataProvider parserTestDataProvider
31
     *
32
     * @param string $fixtureName name of the fixture
33
     */
34
    public function testParser($fixtureName)
35
    {
36
        $etalon = $this->parseEtalon($fixtureName . '.yml');
37
        $features = $this->parseFixture($fixtureName . '.feature');
38
39
        $this->assertInternalType('array', $features);
40
        $this->assertEquals(1, count($features));
41
        $fixture = $features[0];
42
43
        $this->assertEquals($etalon, $fixture);
44
    }
45
46
    protected function getGherkinParser()
47
    {
48
        if (null === $this->gherkin) {
49
            $keywords       = new ArrayKeywords(array(
50
                'en' => array(
51
                    'feature'          => 'Feature',
52
                    'background'       => 'Background',
53
                    'scenario'         => 'Scenario',
54
                    'scenario_outline' => 'Scenario Outline',
55
                    'examples'         => 'Examples',
56
                    'given'            => 'Given',
57
                    'when'             => 'When',
58
                    'then'             => 'Then',
59
                    'and'              => 'And',
60
                    'but'              => 'But'
61
                ),
62
                'ru' => array(
63
                    'feature'          => 'Функционал',
64
                    'background'       => 'Предыстория',
65
                    'scenario'         => 'Сценарий',
66
                    'scenario_outline' => 'Структура сценария',
67
                    'examples'         => 'Значения',
68
                    'given'            => 'Допустим',
69
                    'when'             => 'То',
70
                    'then'             => 'Если',
71
                    'and'              => 'И',
72
                    'but'              => 'Но'
73
                ),
74
                'ja' => array (
75
                    'feature'           => 'フィーチャ',
76
                    'background'        => '背景',
77
                    'scenario'          => 'シナリオ',
78
                    'scenario_outline'  => 'シナリオアウトライン',
79
                    'examples'          => '例|サンプル',
80
                    'given'             => '前提<',
81
                    'when'              => 'もし<',
82
                    'then'              => 'ならば<',
83
                    'and'               => 'かつ<',
84
                    'but'               => 'しかし<'
85
                )
86
            ));
87
            $this->gherkin  = new Parser(new Lexer($keywords));
88
        }
89
90
        return $this->gherkin;
91
    }
92
93
    protected function getYamlParser()
94
    {
95
        if (null === $this->yaml) {
96
            $this->yaml = new YamlFileLoader();
97
        }
98
99
        return $this->yaml;
100
    }
101
102
    protected function parseFixture($fixture)
103
    {
104
        $file = __DIR__ . '/Fixtures/features/' . $fixture;
105
106
        return array($this->getGherkinParser()->parse(file_get_contents($file), $file));
107
    }
108
109
    protected function parseEtalon($etalon)
110
    {
111
        $features = $this->getYamlParser()->load(__DIR__ . '/Fixtures/etalons/' . $etalon);
112
        $feature  = $features[0];
113
114
        return new FeatureNode(
115
            $feature->getTitle(),
116
            $feature->getDescription(),
117
            $feature->getTags(),
118
            $feature->getBackground(),
119
            $feature->getScenarios(),
120
            $feature->getKeyword(),
121
            $feature->getLanguage(),
122
            __DIR__ . '/Fixtures/features/' . basename($etalon, '.yml') . '.feature',
123
            $feature->getLine()
124
        );
125
    }
126
}
127