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