|
| 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 | +} |
0 commit comments