Completed
Pull Request — master (#138)
by Yannick
03:00
created

ParserTest::testParsingManyCommentsShouldPass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
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
    public function testParserResetsTagsBetweenFeatures()
47
    {
48
        $parser = $this->getGherkinParser();
49
50
        $parser->parse(<<<FEATURE
51
Feature:
52
Scenario:
53
Given step
54
@skipped
55
FEATURE
56
        );
57
        $feature2 = $parser->parse(<<<FEATURE
58
Feature:
59
Scenario:
60
Given step
61
FEATURE
62
        );
63
64
        $this->assertFalse($feature2->hasTags());
65
    }
66
67
    protected function getGherkinParser()
68
    {
69
        if (null === $this->gherkin) {
70
            $keywords       = new ArrayKeywords(array(
71
                'en' => array(
72
                    'feature'          => 'Feature',
73
                    'background'       => 'Background',
74
                    'scenario'         => 'Scenario',
75
                    'scenario_outline' => 'Scenario Outline',
76
                    'examples'         => 'Examples',
77
                    'given'            => 'Given',
78
                    'when'             => 'When',
79
                    'then'             => 'Then',
80
                    'and'              => 'And',
81
                    'but'              => 'But'
82
                ),
83
                'ru' => array(
84
                    'feature'          => 'Функционал',
85
                    'background'       => 'Предыстория',
86
                    'scenario'         => 'Сценарий',
87
                    'scenario_outline' => 'Структура сценария',
88
                    'examples'         => 'Значения',
89
                    'given'            => 'Допустим',
90
                    'when'             => 'То',
91
                    'then'             => 'Если',
92
                    'and'              => 'И',
93
                    'but'              => 'Но'
94
                ),
95
                'ja' => array (
96
                    'feature'           => 'フィーチャ',
97
                    'background'        => '背景',
98
                    'scenario'          => 'シナリオ',
99
                    'scenario_outline'  => 'シナリオアウトライン',
100
                    'examples'          => '例|サンプル',
101
                    'given'             => '前提<',
102
                    'when'              => 'もし<',
103
                    'then'              => 'ならば<',
104
                    'and'               => 'かつ<',
105
                    'but'               => 'しかし<'
106
                )
107
            ));
108
            $this->gherkin  = new Parser(new Lexer($keywords));
109
        }
110
111
        return $this->gherkin;
112
    }
113
114
    protected function getYamlParser()
115
    {
116
        if (null === $this->yaml) {
117
            $this->yaml = new YamlFileLoader();
118
        }
119
120
        return $this->yaml;
121
    }
122
123
    protected function parseFixture($fixture)
124
    {
125
        $file = __DIR__ . '/Fixtures/features/' . $fixture;
126
127
        return array($this->getGherkinParser()->parse(file_get_contents($file), $file));
128
    }
129
130
    protected function parseEtalon($etalon)
131
    {
132
        $features = $this->getYamlParser()->load(__DIR__ . '/Fixtures/etalons/' . $etalon);
133
        $feature  = $features[0];
134
135
        return new FeatureNode(
136
            $feature->getTitle(),
137
            $feature->getDescription(),
138
            $feature->getTags(),
139
            $feature->getBackground(),
140
            $feature->getScenarios(),
141
            $feature->getKeyword(),
142
            $feature->getLanguage(),
143
            __DIR__ . '/Fixtures/features/' . basename($etalon, '.yml') . '.feature',
144
            $feature->getLine()
145
        );
146
    }
147
148
    public function testParsingManyCommentsShouldPass()
149
    {
150
        if (! extension_loaded('xdebug')) {
151
            $this->markTestSkipped('xdebug extension must be enabled.');
152
        }
153
        $defaultPHPSetting = 256;
154
        $this->iniSet('xdebug.max_nesting_level', $defaultPHPSetting);
155
156
        $lineCount = 150; // 119 is the real threshold, higher just in case
157
        $this->assertNull($this->getGherkinParser()->parse(str_repeat("# \n", $lineCount)));
158
    }
159
}
160