-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathCckTest.php
89 lines (72 loc) · 2.47 KB
/
CckTest.php
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
<?php
namespace Cucumber\Messages;
use Cucumber\Messages\Streams\NdJson\NdJsonStreamReader;
use Cucumber\Messages\Streams\NdJson\NdJsonStreamWriter;
use Exception;
use Generator;
use PHPUnit\Framework\TestCase;
class CckTest extends TestCase
{
/** @dataProvider provideJsonLines */
public function testAllNdJsonSurvivesDecodingThenEncoding(string $json): void
{
$envelope = Envelope::fromJson($json);
$newJson = $envelope->asJson();
self::assertJsonStringEqualsJsonString($json, $newJson);
}
/** @dataProvider provideNdJsonFilenames */
public function testAllFileStreamsSurviveDecodingThenEncoding(string $filename): void
{
$sourceHandle = fopen($filename, 'r');
$destHandle = fopen('php://memory', 'w');
$reader = NdJsonStreamReader::fromFileHandle($sourceHandle);
$writer = NdJsonStreamWriter::fromFileHandle($destHandle);
$writer->writeEnvelopes($reader->envelopes());
rewind($sourceHandle);
rewind($destHandle);
while (!feof($sourceHandle)) {
$sourceLine = fgets($sourceHandle);
$destLine = fgets($destHandle);
if (!$sourceLine && !$destLine) {
break;
}
self::assertJsonStringEqualsJsonString($sourceLine, $destLine);
}
// we exhausted source so dest should also be at end
self::assertTrue(feof($destHandle));
}
/**
* @return Generator<string, array{0: string}>
*/
public function provideJsonLines(): Generator
{
foreach ($this->getSampleFiles() as $filename) {
foreach (file($filename) ?: [] as $lineNumber => $line) {
// key is provided for better error messages
$key = realpath($filename) . ':' . $lineNumber;
yield $key => [$line];
}
}
}
/**
* @return Generator<string, array{0: string}>
*/
public function provideNdJsonFilenames(): Generator
{
foreach ($this->getSampleFiles() as $filename) {
yield $filename => [$filename];
}
}
/**
* @return list<string>
*/
private function getSampleFiles(): array
{
$files = glob(pattern:__DIR__ . '/cck-samples/**/*.ndjson');
// glob doesn't error if dir is empty
if ($files === []) {
throw new Exception('CCK sample files not found. Perhaps you want to run the "unit" testsuite?');
}
return $files;
}
}