Completed
Push — master ( 5ed365...df4d09 )
by Christophe
11:12 queued 10:00
created

ParserTest::testSingleCharacterStepSupport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
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\Node\ScenarioNode;
8
use Behat\Gherkin\Parser;
9
use Behat\Gherkin\Keywords\ArrayKeywords;
10
use Behat\Gherkin\Loader\YamlFileLoader;
11
use PHPUnit\Framework\TestCase;
12
13
class ParserTest extends TestCase
14
{
15
    private $gherkin;
16
    private $yaml;
17
18
    public function parserTestDataProvider()
19
    {
20
        $data = array();
21
22
        foreach (glob(__DIR__ . '/Fixtures/etalons/*.yml') as $file) {
23
            $testname = basename($file, '.yml');
24
25
            $data[$testname] = array($testname);
26
        }
27
28
        return $data;
29
    }
30
31
    /**
32
     * @dataProvider parserTestDataProvider
33
     *
34
     * @param string $fixtureName name of the fixture
35
     */
36
    public function testParser($fixtureName)
37
    {
38
        $etalon = $this->parseEtalon($fixtureName . '.yml');
39
        $features = $this->parseFixture($fixtureName . '.feature');
40
41
        $this->assertInternalType('array', $features);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit\Framework\Assert::assertInternalType() has been deprecated with message: https://github.com/sebastianbergmann/phpunit/issues/3369

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
42
        $this->assertEquals(1, count($features));
43
        $fixture = $features[0];
44
45
        $this->assertEquals($etalon, $fixture);
46
    }
47
48
    public function testParserResetsTagsBetweenFeatures()
49
    {
50
        $parser = $this->getGherkinParser();
51
52
        $parser->parse(<<<FEATURE
53
Feature:
54
Scenario:
55
Given step
56
@skipped
57
FEATURE
58
        );
59
        $feature2 = $parser->parse(<<<FEATURE
60
Feature:
61
Scenario:
62
Given step
63
FEATURE
64
        );
65
66
        $this->assertFalse($feature2->hasTags());
67
    }
68
69
    public function testSingleCharacterStepSupport()
70
    {
71
        $feature = $this->getGherkinParser()->parse(<<<FEATURE
72
Feature:
73
Scenario:
74
When x
75
FEATURE
76
);
77
78
        $scenarios = $feature->getScenarios();
79
        /** @var ScenarioNode $scenario */
80
        $scenario = array_shift($scenarios);
81
82
        $this->assertCount(1, $scenario->getSteps());
0 ignored issues
show
Documentation introduced by
$scenario->getSteps() is of type array<integer,object<Beh...Gherkin\Node\StepNode>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
    }
84
85
    protected function getGherkinParser()
86
    {
87
        if (null === $this->gherkin) {
88
            $keywords       = new ArrayKeywords(array(
89
                'en' => array(
90
                    'feature'          => 'Feature',
91
                    'background'       => 'Background',
92
                    'scenario'         => 'Scenario',
93
                    'scenario_outline' => 'Scenario Outline',
94
                    'examples'         => 'Examples',
95
                    'given'            => 'Given',
96
                    'when'             => 'When',
97
                    'then'             => 'Then',
98
                    'and'              => 'And',
99
                    'but'              => 'But'
100
                ),
101
                'ru' => array(
102
                    'feature'          => 'Функционал',
103
                    'background'       => 'Предыстория',
104
                    'scenario'         => 'Сценарий',
105
                    'scenario_outline' => 'Структура сценария',
106
                    'examples'         => 'Примеры',
107
                    'given'            => 'Допустим',
108
                    'when'             => 'То',
109
                    'then'             => 'Если',
110
                    'and'              => 'И',
111
                    'but'              => 'Но'
112
                ),
113
                'ja' => array (
114
                    'feature'           => 'フィーチャ',
115
                    'background'        => '背景',
116
                    'scenario'          => 'シナリオ',
117
                    'scenario_outline'  => 'シナリオアウトライン',
118
                    'examples'          => '例|サンプル',
119
                    'given'             => '前提<',
120
                    'when'              => 'もし<',
121
                    'then'              => 'ならば<',
122
                    'and'               => 'かつ<',
123
                    'but'               => 'しかし<'
124
                )
125
            ));
126
            $this->gherkin  = new Parser(new Lexer($keywords));
127
        }
128
129
        return $this->gherkin;
130
    }
131
132
    protected function getYamlParser()
133
    {
134
        if (null === $this->yaml) {
135
            $this->yaml = new YamlFileLoader();
136
        }
137
138
        return $this->yaml;
139
    }
140
141
    protected function parseFixture($fixture)
142
    {
143
        $file = __DIR__ . '/Fixtures/features/' . $fixture;
144
145
        return array($this->getGherkinParser()->parse(file_get_contents($file), $file));
146
    }
147
148
    protected function parseEtalon($etalon)
149
    {
150
        $features = $this->getYamlParser()->load(__DIR__ . '/Fixtures/etalons/' . $etalon);
151
        $feature  = $features[0];
152
153
        return new FeatureNode(
154
            $feature->getTitle(),
155
            $feature->getDescription(),
156
            $feature->getTags(),
157
            $feature->getBackground(),
158
            $feature->getScenarios(),
159
            $feature->getKeyword(),
160
            $feature->getLanguage(),
161
            __DIR__ . '/Fixtures/features/' . basename($etalon, '.yml') . '.feature',
162
            $feature->getLine()
163
        );
164
    }
165
}
166