Completed
Pull Request — master (#159)
by Arne
02:50
created

ParserTest::parseFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Tests\Behat\Gherkin;
4
5
use Behat\Gherkin\Node\FeatureNode;
6
use Behat\Gherkin\Lexer;
7
use Behat\Gherkin\Node\ScenarioNode;
8
use Behat\Gherkin\Parser;
9
use Behat\Gherkin\Keywords\ArrayKeywords;
10
use Behat\Gherkin\Loader\YamlFileLoader;
11
12
class ParserTest extends \PHPUnit_Framework_TestCase
13
{
14
    private $gherkin;
15
    private $yaml;
16
17
    public function parserTestDataProvider()
18
    {
19
        $data = array();
20
21
        foreach (glob(__DIR__ . '/Fixtures/etalons/*.yml') as $file) {
22
            $testname = basename($file, '.yml');
23
24
            $data[] = array($testname);
25
        }
26
27
        return $data;
28
    }
29
30
    /**
31
     * @dataProvider parserTestDataProvider
32
     *
33
     * @param string $fixtureName name of the fixture
34
     */
35
    public function testParser($fixtureName)
36
    {
37
        $etalon = $this->parseEtalon($fixtureName . '.yml');
38
        $features = $this->parseFixture($fixtureName . '.feature');
39
40
        $this->assertInternalType('array', $features);
41
        $this->assertEquals(1, count($features));
42
        $fixture = $features[0];
43
44
        $this->assertEquals($etalon, $fixture);
45
    }
46
47
    public function testParserResetsTagsBetweenFeatures()
48
    {
49
        $parser = $this->getGherkinParser();
50
51
        $parser->parse(<<<FEATURE
52
Feature:
53
Scenario:
54
Given step
55
@skipped
56
FEATURE
57
        );
58
        $feature2 = $parser->parse(<<<FEATURE
59
Feature:
60
Scenario:
61
Given step
62
FEATURE
63
        );
64
65
        $this->assertFalse($feature2->hasTags());
66
    }
67
68
    public function testSingleCharacterStepSupport()
69
    {
70
        $feature = $this->getGherkinParser()->parse(<<<FEATURE
71
Feature:
72
Scenario:
73
When x
74
FEATURE
75
);
76
77
        $scenarios = $feature->getScenarios();
78
        /** @var ScenarioNode $scenario */
79
        $scenario = array_shift($scenarios);
80
81
        $this->assertCount(1, $scenario->getSteps());
82
    }
83
84
    protected function getGherkinParser()
85
    {
86
        if (null === $this->gherkin) {
87
            $keywords       = new ArrayKeywords(array(
88
                'en' => array(
89
                    'feature'          => 'Feature',
90
                    'background'       => 'Background',
91
                    'scenario'         => 'Scenario',
92
                    'scenario_outline' => 'Scenario Outline',
93
                    'examples'         => 'Examples',
94
                    'given'            => 'Given',
95
                    'when'             => 'When',
96
                    'then'             => 'Then',
97
                    'and'              => 'And',
98
                    'but'              => 'But'
99
                ),
100
                'ru' => array(
101
                    'feature'          => 'Функционал',
102
                    'background'       => 'Предыстория',
103
                    'scenario'         => 'Сценарий',
104
                    'scenario_outline' => 'Структура сценария',
105
                    'examples'         => 'Примеры',
106
                    'given'            => 'Допустим',
107
                    'when'             => 'То',
108
                    'then'             => 'Если',
109
                    'and'              => 'И',
110
                    'but'              => 'Но'
111
                ),
112
                'ja' => array (
113
                    'feature'           => 'フィーチャ',
114
                    'background'        => '背景',
115
                    'scenario'          => 'シナリオ',
116
                    'scenario_outline'  => 'シナリオアウトライン',
117
                    'examples'          => '例|サンプル',
118
                    'given'             => '前提<',
119
                    'when'              => 'もし<',
120
                    'then'              => 'ならば<',
121
                    'and'               => 'かつ<',
122
                    'but'               => 'しかし<'
123
                )
124
            ));
125
            $this->gherkin  = new Parser(new Lexer($keywords));
126
        }
127
128
        return $this->gherkin;
129
    }
130
131
    protected function getYamlParser()
132
    {
133
        if (null === $this->yaml) {
134
            $this->yaml = new YamlFileLoader();
135
        }
136
137
        return $this->yaml;
138
    }
139
140
    protected function parseFixture($fixture)
141
    {
142
        $file = __DIR__ . '/Fixtures/features/' . $fixture;
143
144
        return array($this->getGherkinParser()->parse(file_get_contents($file), $file));
145
    }
146
147
    protected function parseEtalon($etalon)
148
    {
149
        $features = $this->getYamlParser()->load(__DIR__ . '/Fixtures/etalons/' . $etalon);
150
        $feature  = $features[0];
151
152
        return new FeatureNode(
153
            $feature->getTitle(),
154
            $feature->getDescription(),
155
            $feature->getTags(),
156
            $feature->getBackground(),
157
            $feature->getScenarios(),
158
            $feature->getKeyword(),
159
            $feature->getLanguage(),
160
            __DIR__ . '/Fixtures/features/' . basename($etalon, '.yml') . '.feature',
161
            $feature->getLine()
162
        );
163
    }
164
}
165