-
-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PHP - Introduce Mutation testing #52
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
build | ||
acceptance | ||
vendor | ||
composer.lock | ||
.phpunit.cache | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,6 @@ GHERKIN_RAZOR = gherkin-php.razor | |
SOURCE_FILES = $(shell find . -name "*.php" | grep -v $(GHERKIN_PARSER)) | ||
|
||
GHERKIN = bin/gherkin | ||
GHERKIN_GENERATE_TOKENS = bin/gherkin-generate-tokens | ||
|
||
GOOD_FEATURE_FILES = $(shell find ../testdata/good -name "*.feature") | ||
BAD_FEATURE_FILES = $(shell find ../testdata/bad -name "*.feature") | ||
|
||
TOKENS = $(patsubst ../testdata/%,acceptance/testdata/%.tokens,$(GOOD_FEATURE_FILES)) | ||
ASTS = $(patsubst ../testdata/%,acceptance/testdata/%.ast.ndjson,$(GOOD_FEATURE_FILES)) | ||
PICKLES = $(patsubst ../testdata/%,acceptance/testdata/%.pickles.ndjson,$(GOOD_FEATURE_FILES)) | ||
SOURCES = $(patsubst ../testdata/%,acceptance/testdata/%.source.ndjson,$(GOOD_FEATURE_FILES)) | ||
ERRORS = $(patsubst ../testdata/%,acceptance/testdata/%.errors.ndjson,$(BAD_FEATURE_FILES)) | ||
|
||
.DEFAULT_GOAL = help | ||
|
||
|
@@ -39,12 +29,13 @@ clean: ## Remove all build artifacts and files generated by the acceptance tests | |
|
||
.DELETE_ON_ERROR: | ||
|
||
acceptance: .built $(TOKENS) $(ASTS) $(PICKLES) $(ERRORS) $(SOURCES) ## Build acceptance test dir and compare results with reference | ||
acceptance: .built ## Test parser against test data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove the goal entirety. Do update the |
||
vendor/bin/phpunit --testsuite acceptance | ||
|
||
.built: vendor $(SOURCE_FILES) | ||
.built: vendor/autoload.php $(SOURCE_FILES) | ||
touch $@ | ||
|
||
vendor: composer.json | ||
vendor/autoload.php: composer.json | ||
composer update | ||
|
||
$(GHERKIN_PARSER): $(GHERKIN_RAZOR) ../gherkin.berp | ||
|
@@ -57,28 +48,3 @@ $(GHERKIN_PARSER): $(GHERKIN_RAZOR) ../gherkin.berp | |
|
||
$(GHERKIN_LANGUAGES_JSON): | ||
cp ../gherkin-languages.json $@ | ||
|
||
acceptance/testdata/%.tokens: ../testdata/% ../testdata/%.tokens | ||
mkdir -p $(@D) | ||
$(GHERKIN_GENERATE_TOKENS) $< > $@ | ||
diff --unified $<.tokens $@ | ||
|
||
acceptance/testdata/%.ast.ndjson: ../testdata/% ../testdata/%.ast.ndjson | ||
mkdir -p $(@D) | ||
$(GHERKIN) --no-source --no-pickles --predictable-ids $< | jq --sort-keys --compact-output "." > $@ | ||
diff --unified <(jq "." $<.ast.ndjson) <(jq "." $@) | ||
|
||
acceptance/testdata/%.pickles.ndjson: ../testdata/% ../testdata/%.pickles.ndjson | ||
mkdir -p $(@D) | ||
$(GHERKIN) --no-source --no-ast --predictable-ids $< | jq --sort-keys --compact-output "." > $@ | ||
diff --unified <(jq "." $<.pickles.ndjson) <(jq "." $@) | ||
|
||
acceptance/testdata/%.source.ndjson: ../testdata/% ../testdata/%.source.ndjson | ||
mkdir -p $(@D) | ||
$(GHERKIN) --no-ast --no-pickles --predictable-ids $< | jq --sort-keys --compact-output "." > $@ | ||
diff --unified <(jq "." $<.source.ndjson) <(jq "." $@) | ||
|
||
acceptance/testdata/%.errors.ndjson: ../testdata/% ../testdata/%.errors.ndjson | ||
mkdir -p $(@D) | ||
$(GHERKIN) --no-source --predictable-ids $< | jq --sort-keys --compact-output "." > $@ | ||
diff --unified <(jq "." $<.errors.ndjson) <(jq "." $@) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"$schema": "vendor/infection/infection/resources/schema.json", | ||
"source": { | ||
"directories": [ | ||
"src", | ||
] | ||
}, | ||
"mutators": { | ||
"@default": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cucumber\Gherkin; | ||
|
||
use Cucumber\Messages\Envelope; | ||
use Cucumber\Messages\Source; | ||
use Cucumber\Messages\Streams\NdJson\NdJsonStreamWriter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class TestDataTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideGoodFeatureFiles | ||
*/ | ||
public function testTokensAreSameAsTestData(string $fullPath): void | ||
{ | ||
$result = (new Parser(new TokenFormatterBuilder()))->parse( | ||
$fullPath, | ||
new StringTokenScanner(file_get_contents($fullPath)), | ||
new TokenMatcher(), | ||
); | ||
|
||
self::assertStringEqualsFile($fullPath . '.tokens', $result); | ||
} | ||
|
||
/** | ||
* @dataProvider provideGoodFeatureFiles | ||
*/ | ||
public function testAstsAreSameAsTestData(string $fullPath, Source $source): void | ||
{ | ||
$envelopes = (new GherkinParser( | ||
predictableIds: true, | ||
includeSource: false, | ||
includeGherkinDocument: true, | ||
includePickles: false, | ||
))->parse([$source]); | ||
|
||
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.ast.ndjson'); | ||
} | ||
|
||
/** | ||
* @dataProvider provideGoodFeatureFiles | ||
*/ | ||
public function testSourcesAreSameAsTestData(string $fullPath, Source $source): void | ||
{ | ||
$envelopes = (new GherkinParser( | ||
predictableIds: true, | ||
includeSource: true, | ||
includeGherkinDocument: false, | ||
includePickles: false, | ||
))->parse([$source]); | ||
|
||
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.source.ndjson'); | ||
} | ||
|
||
/** | ||
* @dataProvider provideGoodFeatureFiles | ||
*/ | ||
public function testPicklesAreSameAsTestData(string $fullPath, Source $source): void | ||
{ | ||
$envelopes = (new GherkinParser( | ||
predictableIds: true, | ||
includeSource: false, | ||
includeGherkinDocument: false, | ||
includePickles: true, | ||
))->parse([$source]); | ||
|
||
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.pickles.ndjson'); | ||
} | ||
|
||
/** | ||
* @dataProvider provideBadFeatureFiles | ||
*/ | ||
public function testErrorsAreSameAsTestData(string $fullPath, Source $source): void | ||
{ | ||
$envelopes = (new GherkinParser( | ||
predictableIds: true, | ||
includeSource: false, | ||
includeGherkinDocument: false, | ||
includePickles: true, | ||
))->parse([$source]); | ||
|
||
self::assertEnvelopesMatchNdJsonFile($envelopes, $fullPath . '.errors.ndjson'); | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{0: string, 1: Source}> | ||
*/ | ||
public function provideGoodFeatureFiles(): iterable | ||
{ | ||
return $this->provideFeatureFiles("good"); | ||
} | ||
|
||
/** | ||
* @return iterable<string, array{0: string, 1: Source}> | ||
*/ | ||
public function provideBadFeatureFiles(): iterable | ||
{ | ||
return $this->provideFeatureFiles("bad"); | ||
} | ||
|
||
/** | ||
* @param 'good'|'bad' $subDir | ||
* | ||
* @return iterable<string, array{0: string, 1: Source}> | ||
*/ | ||
private function provideFeatureFiles(string $subDir): iterable | ||
{ | ||
foreach (glob(__DIR__ . "/../../../testdata/$subDir/*.feature") as $fullPath) { | ||
$shortPath = substr($fullPath, strlen(__DIR__ . '/../../')); | ||
|
||
yield $shortPath => [$fullPath, new Source($shortPath, file_get_contents($fullPath))]; | ||
} | ||
} | ||
|
||
/** | ||
* @param iterable<Envelope> $envelopes | ||
*/ | ||
private static function assertEnvelopesMatchNdJsonFile(iterable $envelopes, string $expectedfile): void | ||
{ | ||
$output = fopen('php://memory', 'w'); | ||
NdJsonStreamWriter::fromFileHandle($output)->writeEnvelopes($envelopes); | ||
rewind($output); | ||
|
||
$actual = stream_get_contents($output); | ||
$expected = file_get_contents($expectedfile); | ||
|
||
// rather than compare the full file, compare line by line to get better JSON diffs on error | ||
$actualLines = explode("\n", $actual); | ||
$expectedLines = explode("\n", $expected); | ||
|
||
self::assertSame(count($actualLines), count($expectedLines)); | ||
|
||
foreach ($actualLines as $i => $actualLine) { | ||
if ($actualLine !== '') { | ||
self::assertJsonStringEqualsJsonString($expectedLines[$i], $actualLine); | ||
} else { | ||
self::assertEquals($expectedLines[$i], ''); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can run all the tests in a regular run. The separation between acceptance and unit existed only because we had different methods for testing.