Skip to content

Commit c85ba5c

Browse files
committed
Move acceptance tests into PHPUnit
1 parent 708347f commit c85ba5c

24 files changed

+154
-67
lines changed

.github/workflows/test-php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
run: |
5757
vendor/bin/php-cs-fixer --dry-run --diff fix
5858
vendor/bin/psalm --no-cache
59-
vendor/bin/phpunit
59+
vendor/bin/phpunit --testsuite unit
6060
6161
- name: run acceptance tests
6262
run: make acceptance

php/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
build
2-
acceptance
32
vendor
43
composer.lock
54
.phpunit.cache

php/Makefile

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ GHERKIN_RAZOR = gherkin-php.razor
66
SOURCE_FILES = $(shell find . -name "*.php" | grep -v $(GHERKIN_PARSER))
77

88
GHERKIN = bin/gherkin
9-
GHERKIN_GENERATE_TOKENS = bin/gherkin-generate-tokens
10-
11-
GOOD_FEATURE_FILES = $(shell find ../testdata/good -name "*.feature")
12-
BAD_FEATURE_FILES = $(shell find ../testdata/bad -name "*.feature")
13-
14-
TOKENS = $(patsubst ../testdata/%,acceptance/testdata/%.tokens,$(GOOD_FEATURE_FILES))
15-
ASTS = $(patsubst ../testdata/%,acceptance/testdata/%.ast.ndjson,$(GOOD_FEATURE_FILES))
16-
PICKLES = $(patsubst ../testdata/%,acceptance/testdata/%.pickles.ndjson,$(GOOD_FEATURE_FILES))
17-
SOURCES = $(patsubst ../testdata/%,acceptance/testdata/%.source.ndjson,$(GOOD_FEATURE_FILES))
18-
ERRORS = $(patsubst ../testdata/%,acceptance/testdata/%.errors.ndjson,$(BAD_FEATURE_FILES))
199

2010
.DEFAULT_GOAL = help
2111

@@ -39,12 +29,13 @@ clean: ## Remove all build artifacts and files generated by the acceptance tests
3929

4030
.DELETE_ON_ERROR:
4131

42-
acceptance: .built $(TOKENS) $(ASTS) $(PICKLES) $(ERRORS) $(SOURCES) ## Build acceptance test dir and compare results with reference
32+
acceptance: .built ## Test parser against test data
33+
vendor/bin/phpunit --testsuite acceptance
4334

44-
.built: vendor $(SOURCE_FILES)
35+
.built: vendor/autoload.php $(SOURCE_FILES)
4536
touch $@
4637

47-
vendor: composer.json
38+
vendor/autoload.php: composer.json
4839
composer update
4940

5041
$(GHERKIN_PARSER): $(GHERKIN_RAZOR) ../gherkin.berp
@@ -57,28 +48,3 @@ $(GHERKIN_PARSER): $(GHERKIN_RAZOR) ../gherkin.berp
5748

5849
$(GHERKIN_LANGUAGES_JSON):
5950
cp ../gherkin-languages.json $@
60-
61-
acceptance/testdata/%.tokens: ../testdata/% ../testdata/%.tokens
62-
mkdir -p $(@D)
63-
$(GHERKIN_GENERATE_TOKENS) $< > $@
64-
diff --unified $<.tokens $@
65-
66-
acceptance/testdata/%.ast.ndjson: ../testdata/% ../testdata/%.ast.ndjson
67-
mkdir -p $(@D)
68-
$(GHERKIN) --no-source --no-pickles --predictable-ids $< | jq --sort-keys --compact-output "." > $@
69-
diff --unified <(jq "." $<.ast.ndjson) <(jq "." $@)
70-
71-
acceptance/testdata/%.pickles.ndjson: ../testdata/% ../testdata/%.pickles.ndjson
72-
mkdir -p $(@D)
73-
$(GHERKIN) --no-source --no-ast --predictable-ids $< | jq --sort-keys --compact-output "." > $@
74-
diff --unified <(jq "." $<.pickles.ndjson) <(jq "." $@)
75-
76-
acceptance/testdata/%.source.ndjson: ../testdata/% ../testdata/%.source.ndjson
77-
mkdir -p $(@D)
78-
$(GHERKIN) --no-ast --no-pickles --predictable-ids $< | jq --sort-keys --compact-output "." > $@
79-
diff --unified <(jq "." $<.source.ndjson) <(jq "." $@)
80-
81-
acceptance/testdata/%.errors.ndjson: ../testdata/% ../testdata/%.errors.ndjson
82-
mkdir -p $(@D)
83-
$(GHERKIN) --no-source --predictable-ids $< | jq --sort-keys --compact-output "." > $@
84-
diff --unified <(jq "." $<.errors.ndjson) <(jq "." $@)

php/bin/gherkin-generate-tokens

Lines changed: 0 additions & 24 deletions
This file was deleted.

php/phpunit.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
failOnWarning="true"
1313
verbose="true">
1414
<testsuites>
15-
<testsuite name="default">
16-
<directory>tests</directory>
15+
<testsuite name="unit">
16+
<directory>tests/unit</directory>
17+
</testsuite>
18+
<testsuite name="acceptance">
19+
<file>tests/acceptance/TestDataTest.php</file>
1720
</testsuite>
1821
</testsuites>
1922

php/psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
88
>
99
<projectFiles>
10-
<file name="bin/gherkin-generate-tokens"/>
1110
<file name="bin/gherkin"/>
1211
<directory name="src"/>
1312
<directory name="src-generated"/>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cucumber\Gherkin;
6+
7+
use Cucumber\Messages\Envelope;
8+
use Cucumber\Messages\Source;
9+
use Cucumber\Messages\Streams\NdJson\NdJsonStreamWriter;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class TestDataTest extends TestCase
13+
{
14+
/**
15+
* @dataProvider provideGoodFeatureFiles
16+
*/
17+
public function testTokensAreSameAsTestData(string $fullPath): void
18+
{
19+
$result = (new Parser(new TokenFormatterBuilder()))->parse(
20+
$fullPath,
21+
new StringTokenScanner(file_get_contents($fullPath)),
22+
new TokenMatcher(),
23+
);
24+
25+
self::assertStringEqualsFile($fullPath . '.tokens', $result);
26+
}
27+
28+
/**
29+
* @dataProvider provideGoodFeatureFiles
30+
*/
31+
public function testAstsAreSameAsTestData(string $fullPath, Source $source): void
32+
{
33+
$envelopes = (new GherkinParser(
34+
predictableIds: true,
35+
includeSource: false,
36+
includeGherkinDocument: true,
37+
includePickles: false,
38+
))->parse([$source]);
39+
40+
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.ast.ndjson');
41+
}
42+
43+
/**
44+
* @dataProvider provideGoodFeatureFiles
45+
*/
46+
public function testSourcesAreSameAsTestData(string $fullPath, Source $source): void
47+
{
48+
$envelopes = (new GherkinParser(
49+
predictableIds: true,
50+
includeSource: true,
51+
includeGherkinDocument: false,
52+
includePickles: false,
53+
))->parse([$source]);
54+
55+
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.source.ndjson');
56+
}
57+
58+
/**
59+
* @dataProvider provideGoodFeatureFiles
60+
*/
61+
public function testPicklesAreSameAsTestData(string $fullPath, Source $source): void
62+
{
63+
$envelopes = (new GherkinParser(
64+
predictableIds: true,
65+
includeSource: false,
66+
includeGherkinDocument: false,
67+
includePickles: true,
68+
))->parse([$source]);
69+
70+
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.pickles.ndjson');
71+
}
72+
73+
/**
74+
* @dataProvider provideBadFeatureFiles
75+
*/
76+
public function testErrorsAreSameAsTestData(string $fullPath, Source $source): void
77+
{
78+
$envelopes = (new GherkinParser(
79+
predictableIds: true,
80+
includeSource: false,
81+
includeGherkinDocument: false,
82+
includePickles: true,
83+
))->parse([$source]);
84+
85+
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.errors.ndjson');
86+
}
87+
88+
/**
89+
* @return iterable<string, array{0: string, 1: Source}>
90+
*/
91+
public function provideGoodFeatureFiles(): iterable
92+
{
93+
return $this->provideFeatureFiles("good");
94+
}
95+
96+
/**
97+
* @return iterable<string, array{0: string, 1: Source}>
98+
*/
99+
public function provideBadFeatureFiles(): iterable
100+
{
101+
return $this->provideFeatureFiles("bad");
102+
}
103+
104+
/**
105+
* @param 'good'|'bad' $subDir
106+
*
107+
* @return iterable<string, array{0: string, 1: Source}>
108+
*/
109+
private function provideFeatureFiles(string $subDir): iterable
110+
{
111+
foreach (glob(__DIR__ . "/../../../testdata/$subDir/*.feature") as $fullPath) {
112+
$shortPath = substr($fullPath, strlen(__DIR__ . '/../../'));
113+
114+
yield $shortPath => [$fullPath, new Source($shortPath, file_get_contents($fullPath))];
115+
}
116+
}
117+
118+
/**
119+
* @param iterable<Envelope> $envelopes
120+
*/
121+
private static function assertEnvelopesMatchNdJsonFile(iterable $envelopes, string $expectedfile): void
122+
{
123+
$output = fopen('php://memory', 'w');
124+
NdJsonStreamWriter::fromFileHandle($output)->writeEnvelopes($envelopes);
125+
rewind($output);
126+
127+
$actual = stream_get_contents($output);
128+
$expected = file_get_contents($expectedfile);
129+
130+
// rather than compare the full file, compare line by line to get better JSON diffs on error
131+
$actualLines = explode("\n", $actual);
132+
$expectedLines = explode("\n", $expected);
133+
134+
self::assertSame(count($actualLines), count($expectedLines));
135+
136+
foreach ($actualLines as $i => $actualLine) {
137+
if ($actualLine !== '') {
138+
self::assertJsonStringEqualsJsonString($expectedLines[$i], $actualLine);
139+
} else {
140+
self::assertEquals($expectedLines[$i], '');
141+
}
142+
}
143+
}
144+
}
File renamed without changes.

0 commit comments

Comments
 (0)