Skip to content

Commit 1e2e1d9

Browse files
wundiiawu
andauthored
fix: ReadEventLine to support flexible payload types (#90)
Co-authored-by: awu <andreas.wunderwald@westpress.de>
1 parent bad7a18 commit 1e2e1d9

3 files changed

Lines changed: 72 additions & 35 deletions

File tree

composer.lock

Lines changed: 29 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Stream/ReadEventLine.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
final readonly class ReadEventLine
88
{
9+
/**
10+
* For the property payload, we agreed on mixed, as a wide variety of types can be transmitted.
11+
* Complex data types such as objects or resources are excluded.
12+
*/
913
public function __construct(
1014
public string $type,
11-
public array $payload,
15+
public mixed $payload,
1216
) {
1317
}
1418
}

tests/Stream/NdJsonTest.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,68 @@ final class NdJsonTest extends TestCase
1616
public function testReadStreamYieldsEventLines(): void
1717
{
1818
$json1 = json_encode([
19-
'type' => 'event1',
19+
'type' => 'row',
2020
'payload' => [
2121
'foo' => 'bar',
2222
],
2323
]) . "\n";
2424
$json2 = json_encode([
25-
'type' => 'event2',
25+
'type' => 'row',
2626
'payload' => [
2727
'baz' => 'qux',
2828
],
2929
]) . "\n";
30+
$json3 = json_encode([
31+
'type' => 'row',
32+
'payload' => 'io.test.v1',
33+
]) . "\n";
34+
$json4 = json_encode([
35+
'type' => 'row',
36+
'payload' => 4,
37+
]) . "\n";
38+
$json5 = json_encode([
39+
'type' => 'row',
40+
'payload' => 1.2,
41+
]) . "\n";
42+
$json6 = json_encode([
43+
'type' => 'row',
44+
'payload' => true,
45+
]) . "\n";
3046

3147
$stream = $this->createMock(Stream::class);
3248
$stream->method('getIterator')
33-
->willReturn(new ArrayIterator([$json1, $json2]));
49+
->willReturn(new ArrayIterator([$json1, $json2, $json3, $json4, $json5, $json6]));
3450

3551
$events = iterator_to_array(NdJson::readStream($stream));
3652

37-
$this->assertCount(2, $events);
53+
$this->assertCount(6, $events);
3854
$this->assertInstanceOf(ReadEventLine::class, $events[0]);
39-
$this->assertSame('event1', $events[0]->type);
55+
$this->assertSame('row', $events[0]->type);
4056
$this->assertSame([
4157
'foo' => 'bar',
4258
], $events[0]->payload);
4359

4460
$this->assertInstanceOf(ReadEventLine::class, $events[1]);
45-
$this->assertSame('event2', $events[1]->type);
61+
$this->assertSame('row', $events[1]->type);
4662
$this->assertSame([
4763
'baz' => 'qux',
4864
], $events[1]->payload);
65+
66+
$this->assertInstanceOf(ReadEventLine::class, $events[2]);
67+
$this->assertSame('row', $events[2]->type);
68+
$this->assertSame('io.test.v1', $events[2]->payload);
69+
70+
$this->assertInstanceOf(ReadEventLine::class, $events[3]);
71+
$this->assertSame('row', $events[3]->type);
72+
$this->assertSame(4, $events[3]->payload);
73+
74+
$this->assertInstanceOf(ReadEventLine::class, $events[4]);
75+
$this->assertSame('row', $events[4]->type);
76+
$this->assertEqualsWithDelta(1.2, $events[4]->payload, PHP_FLOAT_EPSILON);
77+
78+
$this->assertInstanceOf(ReadEventLine::class, $events[5]);
79+
$this->assertSame('row', $events[5]->type);
80+
$this->assertTrue($events[5]->payload);
4981
}
5082

5183
public function testReadStreamSkipsEmptyLines(): void

0 commit comments

Comments
 (0)