-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathCompatibilityTest.php
More file actions
318 lines (278 loc) · 12 KB
/
Copy pathCompatibilityTest.php
File metadata and controls
318 lines (278 loc) · 12 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/*
* This file is part of the Behat Gherkin Parser.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Behat\Gherkin\Cucumber;
use Behat\Gherkin\Dialect\CucumberDialectProvider;
use Behat\Gherkin\Exception\ParserException;
use Behat\Gherkin\Filesystem;
use Behat\Gherkin\GherkinCompatibilityMode;
use Behat\Gherkin\Lexer;
use Behat\Gherkin\Parser;
use FilesystemIterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use SebastianBergmann\Comparator\Factory;
use SplFileInfo;
use UnexpectedValueException;
/**
* Tests the parser against the upstream cucumber/gherkin test data.
*
* @group cucumber-compatibility
*
* @phpstan-type TCucumberParsingTestCase array{mode: GherkinCompatibilityMode, file: SplFileInfo}
* @phpstan-type TKnownIncompatibilityMap array<value-of<GherkinCompatibilityMode>, array<string,string>>
*/
class CompatibilityTest extends TestCase
{
private const GHERKIN_TESTDATA_PATH = __DIR__ . '/../../vendor/cucumber/gherkin-monorepo/testdata';
private const EXTRA_TESTDATA_PATH = __DIR__ . '/extra_testdata';
private const PROJECT_BASE_PATH = __DIR__ . '/../../';
private const EXPECTED_VARIANTS_PATH = __DIR__ . '/expected_variants';
/**
* @phpstan-var TKnownIncompatibilityMap
*/
private static array $notParsingCorrectly = [
'legacy' => [
'complex_background.feature' => 'Rule keyword not supported',
'docstrings.feature' => 'Escaped delimiters in docstrings are not unescaped',
'datatables_with_new_lines.feature' => 'Escaped newlines in table cells are not unescaped',
'escaped_pipes.feature' => 'Escaped newlines in table cells are not unescaped',
'rule.feature' => 'Rule keyword not supported',
'rule_with_tag.feature' => 'Rule keyword not supported',
'tags.feature' => 'Rule keyword not supported',
'descriptions.feature' => 'Examples table descriptions not supported',
'descriptions_with_comments.feature' => 'Examples table descriptions not supported',
'feature_keyword_in_scenario_description.feature' => 'Scenario descriptions not supported',
'padded_example.feature' => 'Table padding is not trimmed as aggressively',
'spaces_in_language.feature' => 'Whitespace not supported around language selector',
'rule_without_name_and_description.feature' => 'Rule is wrongly parsed as Description',
'incomplete_background_2.feature' => 'Background descriptions not supported',
'examples_keyword_in_background_description.feature' => 'Background descriptions not supported',
'extra_blank_lines_everywhere.feature' => 'Background / Examples descriptions not supported',
],
'gherkin-32' => [
'complex_background.feature' => 'Rule keyword not supported',
'rule.feature' => 'Rule keyword not supported',
'rule_with_tag.feature' => 'Rule keyword not supported',
'tags.feature' => 'Rule keyword not supported',
'rule_without_name_and_description.feature' => 'Rule is wrongly parsed as Description',
],
];
/**
* @phpstan-var TKnownIncompatibilityMap
*/
private static array $parsedButShouldNotBe = [
'legacy' => [
'invalid_language.feature' => 'Invalid language is silently ignored',
],
'gherkin-32' => [
],
];
/**
* @phpstan-var TKnownIncompatibilityMap
*/
private array $deprecatedInsteadOfParseError = [
'legacy' => [
'whitespace_in_tags.feature' => '/Whitespace in tags is deprecated/',
],
'gherkin-32' => [
],
];
private Parser $parser;
private NDJsonAstParser $ndJsonAstParser;
private static ?StepNodeComparator $stepNodeComparator = null;
private static ?FeatureNodeComparator $featureNodeComparator = null;
public static function setUpBeforeClass(): void
{
self::$stepNodeComparator = new StepNodeComparator();
Factory::getInstance()->register(self::$stepNodeComparator);
self::$featureNodeComparator = new FeatureNodeComparator();
Factory::getInstance()->register(self::$featureNodeComparator);
}
public static function tearDownAfterClass(): void
{
if (self::$stepNodeComparator !== null) {
Factory::getInstance()->unregister(self::$stepNodeComparator);
self::$stepNodeComparator = null;
}
if (self::$featureNodeComparator !== null) {
Factory::getInstance()->unregister(self::$featureNodeComparator);
self::$featureNodeComparator = null;
}
}
protected function setUp(): void
{
$lexer = new Lexer(new CucumberDialectProvider());
$this->parser = new Parser($lexer);
$this->ndJsonAstParser = new NDJsonAstParser();
}
#[DataProvider('goodCucumberFeatures')]
public function testFeaturesParseTheSameAsCucumber(GherkinCompatibilityMode $mode, SplFileInfo $file): void
{
if (isset(self::$notParsingCorrectly[$mode->value][$file->getFilename()])) {
$this->markTestIncomplete(self::$notParsingCorrectly[$mode->value][$file->getFilename()]);
}
assert(self::$featureNodeComparator instanceof FeatureNodeComparator);
assert(self::$stepNodeComparator instanceof StepNodeComparator);
self::$featureNodeComparator->setGherkinCompatibilityMode($mode);
self::$stepNodeComparator->setGherkinCompatibilityMode($mode);
$this->parser->setGherkinCompatibilityMode($mode);
$this->ndJsonAstParser->setGherkinCompatibilityMode($mode);
$gherkinFile = $file->getPathname();
$actual = $this->parser->parseFile($gherkinFile);
$cucumberFeatures = $this->ndJsonAstParser->load($gherkinFile . '.ast.ndjson');
$expected = $cucumberFeatures ? $cucumberFeatures[0] : null;
$this->assertEquals(
$expected,
$actual,
Filesystem::readFile($gherkinFile),
);
}
#[DataProvider('badCucumberFeatures')]
public function testBadFeaturesDoNotParse(GherkinCompatibilityMode $mode, SplFileInfo $file): void
{
if (isset(self::$parsedButShouldNotBe[$mode->value][$file->getFilename()])) {
$this->markTestIncomplete(self::$parsedButShouldNotBe[$mode->value][$file->getFilename()]);
}
$gherkinFile = $file->getPathname();
$this->parser->setGherkinCompatibilityMode($mode);
if (isset($this->deprecatedInsteadOfParseError[$mode->value][$file->getFilename()])) {
$this->expectDeprecationErrorMatches(
$this->deprecatedInsteadOfParseError[$mode->value][$file->getFilename()],
);
} else {
// Note that the exception message is not part of compatibility testing and therefore cannot be checked.
$this->expectException(ParserException::class);
}
$this->parser->parseFile($gherkinFile);
}
#[DataProvider('skippedCucumberFeatures')]
public function testSkippedExamplesParseAsExpected(GherkinCompatibilityMode $mode, SplFileInfo $file): void
{
$dumper = new ParserResultDumper(Filesystem::getRealPath(self::PROJECT_BASE_PATH));
$this->parser->setGherkinCompatibilityMode($mode);
try {
$gherkinFile = Filesystem::getRealPath($file->getPathname());
$result = $this->parser->parse(Filesystem::readFile($gherkinFile), $gherkinFile);
} catch (ParserException $e) {
$result = $e;
}
$dumpedResult = $dumper->dump($result);
$expectationFile = $this->getExpectedVariantFilename($mode, $file);
try {
$this->assertStringEqualsFile($expectationFile, $dumpedResult);
} catch (ExpectationFailedException $e) {
if (getenv('RE_RECORD_EXPECTATIONS')) {
Filesystem::writeFile($expectationFile, $dumpedResult);
}
throw $e;
}
}
public function testNoRedundantVariantsFiles(): void
{
// This is a sanity check to ensure that variant expectation files are deleted once they are no longer required.
$usedFiles = [];
foreach (self::skippedCucumberFeatures() as ['mode' => $mode, 'file' => $file]) {
$usedFiles[] = $this->getExpectedVariantFilename($mode, $file);
}
$redundantFiles = array_diff(
Filesystem::findFilesRecursively(self::EXPECTED_VARIANTS_PATH, '*.expected.yaml'),
$usedFiles
);
if ($redundantFiles === []) {
$this->addToAssertionCount(1);
return;
}
$fileList = implode(
"\n - ",
array_map(
fn ($f) => preg_replace(
'/^' . preg_quote(self::EXPECTED_VARIANTS_PATH . '/', '/') . '/',
'',
$f
),
$redundantFiles
)
);
throw new UnexpectedValueException(sprintf(
"Found redundant expectation files in %s that were not used by any skipped feature:\n - %s",
self::EXPECTED_VARIANTS_PATH,
$fileList
));
}
/**
* @phpstan-return iterable<string, TCucumberParsingTestCase>
*/
public static function goodCucumberFeatures(): iterable
{
yield from self::getCucumberFeatures(self::GHERKIN_TESTDATA_PATH . '/good');
yield from self::getCucumberFeatures(self::EXTRA_TESTDATA_PATH . '/good');
}
/**
* @phpstan-return iterable<string, TCucumberParsingTestCase>
*/
public static function badCucumberFeatures(): iterable
{
yield from self::getCucumberFeatures(self::GHERKIN_TESTDATA_PATH . '/bad');
yield from self::getCucumberFeatures(self::EXTRA_TESTDATA_PATH . '/bad');
}
/**
* @return iterable<string, TCucumberParsingTestCase>
*/
public static function skippedCucumberFeatures(): iterable
{
foreach (self::goodCucumberFeatures() as $key => ['mode' => $mode, 'file' => $file]) {
if (isset(self::$notParsingCorrectly[$mode->value][$file->getFilename()])) {
yield $key => ['mode' => $mode, 'file' => $file];
}
}
foreach (self::badCucumberFeatures() as $key => ['mode' => $mode, 'file' => $file]) {
if (isset(self::$parsedButShouldNotBe[$mode->value][$file->getFilename()])) {
yield $key => ['mode' => $mode, 'file' => $file];
}
}
}
/**
* @phpstan-return iterable<string, TCucumberParsingTestCase>
*/
private static function getCucumberFeatures(string $folder): iterable
{
$fileIterator = new FilesystemIterator($folder);
/**
* @var iterable<string, SplFileInfo> $fileIterator
*/
foreach ($fileIterator as $file) {
if ($file->isFile() && $file->getExtension() === 'feature') {
foreach (GherkinCompatibilityMode::cases() as $mode) {
yield $file->getFilename() . ' (' . $mode->value . ')' => [
'mode' => $mode,
'file' => $file,
];
}
}
}
}
private function expectDeprecationErrorMatches(string $message): void
{
set_error_handler(
static function ($errno, $errstr) {
restore_error_handler();
throw new RuntimeException($errstr, $errno);
},
E_ALL
);
$this->expectExceptionMessageMatches($message);
$this->expectException(RuntimeException::class);
}
private function getExpectedVariantFilename(GherkinCompatibilityMode $mode, SplFileInfo $file): string
{
return self::EXPECTED_VARIANTS_PATH . '/' . $mode->value . '/' . $file->getFilename() . '.expected.yaml';
}
}