Skip to content

Commit e7c7997

Browse files
wundiiawu
andauthored
fix: change writeEvents return type to array and update usage in tests (#23)
Co-authored-by: awu <andreas.wunderwald@westpress.de>
1 parent b8ca69e commit e7c7997

7 files changed

Lines changed: 48 additions & 49 deletions

src/Client.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function verifyApiToken(): void
7070
}
7171
}
7272

73-
public function writeEvents(array $events, array $preconditions = []): iterable
73+
public function writeEvents(array $events, array $preconditions = []): array
7474
{
7575
$requestBody = [
7676
'events' => $events,
@@ -95,7 +95,7 @@ public function writeEvents(array $events, array $preconditions = []): iterable
9595

9696
$body = $response->getStream()->getContents();
9797
if ($body === '') {
98-
return;
98+
return [];
9999
}
100100

101101
if (!json_validate($body)) {
@@ -107,8 +107,8 @@ public function writeEvents(array $events, array $preconditions = []): iterable
107107
throw new RuntimeException('Failed to read events, expected an array.');
108108
}
109109

110-
foreach ($data as $item) {
111-
$cloudEvent = new CloudEvent(
110+
$writtenEvents = array_map(
111+
static fn ($item): CloudEvent => new CloudEvent(
112112
$item['specversion'],
113113
$item['id'],
114114
new DateTimeImmutable($item['time']),
@@ -121,9 +121,11 @@ public function writeEvents(array $events, array $preconditions = []): iterable
121121
$item['predecessorhash'],
122122
$item['traceparent'] ?? null,
123123
$item['tracestate'] ?? null,
124-
);
125-
yield $cloudEvent;
126-
}
124+
),
125+
$data,
126+
);
127+
128+
return $writtenEvents;
127129
}
128130

129131
public function readEvents(string $subject, ReadEventsOptions $readEventsOptions): iterable

tests/ObserveEventsTest.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public function testObserverAllEventsFromTheGivenSubject(): void
5151
],
5252
);
5353

54-
iterator_count($this->client->writeEvents([
54+
$this->client->writeEvents([
5555
$firstEvent,
5656
$secondEvent,
57-
]));
57+
]);
5858

5959
$eventsObserved = [];
6060
$observeEventsOptions = new ObserveEventsOptions(
@@ -89,10 +89,10 @@ public function testObserversRecursively(): void
8989
],
9090
);
9191

92-
iterator_count($this->client->writeEvents([
92+
$this->client->writeEvents([
9393
$firstEvent,
9494
$secondEvent,
95-
]));
95+
]);
9696

9797
$eventsObserved = [];
9898
$observeEventsOptions = new ObserveEventsOptions(
@@ -127,10 +127,10 @@ public function testObserversWithLowerBound(): void
127127
],
128128
);
129129

130-
iterator_count($this->client->writeEvents([
130+
$this->client->writeEvents([
131131
$firstEvent,
132132
$secondEvent,
133-
]));
133+
]);
134134

135135
$eventsObserved = [];
136136
$observeEventsOptions = new ObserveEventsOptions(
@@ -172,10 +172,10 @@ public function testObserversFromLatestEvent(): void
172172
],
173173
);
174174

175-
iterator_count($this->client->writeEvents([
175+
$this->client->writeEvents([
176176
$firstEvent,
177177
$secondEvent,
178-
]));
178+
]);
179179

180180
$eventsObserved = [];
181181
$observeEventsOptions = new ObserveEventsOptions(
@@ -218,10 +218,10 @@ public function testObserverAllEventsWithAbortInLoop(): void
218218
],
219219
);
220220

221-
iterator_count($this->client->writeEvents([
221+
$this->client->writeEvents([
222222
$firstEvent,
223223
$secondEvent,
224-
]));
224+
]);
225225

226226
$eventsObserved = [];
227227
$observeEventsOptions = new ObserveEventsOptions(
@@ -253,8 +253,7 @@ public function testObserverAllEventsPerformanceBenchmark(): void
253253
);
254254
}
255255

256-
$count = iterator_count($this->client->writeEvents($events));
257-
$this->assertSame($eventCount, $count);
256+
$this->assertCount($eventCount, $this->client->writeEvents($events));
258257

259258
$eventsObserved = [];
260259
$observeEventsOptions = new ObserveEventsOptions(

tests/ReadEventTypesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public function testReadsAllEventTypes(): void
4242
],
4343
);
4444

45-
iterator_count($this->client->writeEvents([
45+
$this->client->writeEvents([
4646
$firstEvent,
4747
$secondEvent,
48-
]));
48+
]);
4949

5050
$eventTypesRead = [];
5151

tests/ReadEventsTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function testReadsAllEventsFromTheGivenSubject(): void
4949
],
5050
);
5151

52-
iterator_count($this->client->writeEvents([
52+
$this->client->writeEvents([
5353
$firstEvent,
5454
$secondEvent,
55-
]));
55+
]);
5656

5757
$eventsRead = [];
5858
$readEventsOptions = new ReadEventsOptions(false);
@@ -84,10 +84,10 @@ public function testReadsRecursively(): void
8484
],
8585
);
8686

87-
iterator_count($this->client->writeEvents([
87+
$this->client->writeEvents([
8888
$firstEvent,
8989
$secondEvent,
90-
]));
90+
]);
9191

9292
$eventsRead = [];
9393
$readEventsOptions = new ReadEventsOptions(true);
@@ -119,10 +119,10 @@ public function testReadsChronologically(): void
119119
],
120120
);
121121

122-
iterator_count($this->client->writeEvents([
122+
$this->client->writeEvents([
123123
$firstEvent,
124124
$secondEvent,
125-
]));
125+
]);
126126

127127
$eventsRead = [];
128128
$readEventsOptions = new ReadEventsOptions(
@@ -163,10 +163,10 @@ public function testReadsAntiChronologically(): void
163163
],
164164
);
165165

166-
iterator_count($this->client->writeEvents([
166+
$this->client->writeEvents([
167167
$firstEvent,
168168
$secondEvent,
169-
]));
169+
]);
170170

171171
$eventsRead = [];
172172
$readEventsOptions = new ReadEventsOptions(
@@ -207,10 +207,10 @@ public function testReadsWithLowerBound(): void
207207
],
208208
);
209209

210-
iterator_count($this->client->writeEvents([
210+
$this->client->writeEvents([
211211
$firstEvent,
212212
$secondEvent,
213-
]));
213+
]);
214214

215215
$eventsRead = [];
216216
$readEventsOptions = new ReadEventsOptions(
@@ -248,10 +248,10 @@ public function testReadsWithUpperBound(): void
248248
],
249249
);
250250

251-
iterator_count($this->client->writeEvents([
251+
$this->client->writeEvents([
252252
$firstEvent,
253253
$secondEvent,
254-
]));
254+
]);
255255

256256
$eventsRead = [];
257257
$readEventsOptions = new ReadEventsOptions(
@@ -289,10 +289,10 @@ public function testReadsFromLatestEvent(): void
289289
],
290290
);
291291

292-
iterator_count($this->client->writeEvents([
292+
$this->client->writeEvents([
293293
$firstEvent,
294294
$secondEvent,
295-
]));
295+
]);
296296

297297
$eventsRead = [];
298298
$readEventsOptions = new ReadEventsOptions(

tests/ReadSubjectsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public function testReadsAllSubjects(): void
4040
],
4141
);
4242

43-
iterator_count($this->client->writeEvents([
43+
$this->client->writeEvents([
4444
$firstEvent,
4545
$secondEvent,
46-
]));
46+
]);
4747

4848
$subjectsRead = [];
4949
foreach ($this->client->readSubjects('/') as $subject) {
@@ -77,10 +77,10 @@ public function testReadsAllSubjectsFromTheBaseSubject(): void
7777
],
7878
);
7979

80-
iterator_count($this->client->writeEvents([
80+
$this->client->writeEvents([
8181
$firstEvent,
8282
$secondEvent,
83-
]));
83+
]);
8484

8585
$subjectsRead = [];
8686
foreach ($this->client->readSubjects('/test') as $subject) {

tests/RunEventQlQueryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public function testReadsAllRowsTheQueryReturn(): void
4040
],
4141
);
4242

43-
iterator_count($this->client->writeEvents([
43+
$this->client->writeEvents([
4444
$firstEvent,
4545
$secondEvent,
46-
]));
46+
]);
4747

4848
$rowsRead = [];
4949
foreach ($this->client->runEventQlQuery('FROM e IN events PROJECT INTO e') as $row) {

tests/WriteEventsTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public function testWritesASingleEvent(): void
2929
$eventCandidate,
3030
]);
3131

32-
$writtenEvents = iterator_to_array($writtenEvents);
3332
$this->assertCount(1, $writtenEvents);
3433
$this->assertInstanceOf(CloudEvent::class, $writtenEvents[0]);
3534
$this->assertSame('0', $writtenEvents[0]->id);
@@ -60,7 +59,6 @@ public function testWritesMultipleEvents(): void
6059
$secondEvent,
6160
]);
6261

63-
$writtenEvents = iterator_to_array($writtenEvents);
6462
$this->assertCount(2, $writtenEvents);
6563
$this->assertInstanceOf(CloudEvent::class, $writtenEvents[0]);
6664
$this->assertSame('0', $writtenEvents[0]->id);
@@ -81,9 +79,9 @@ public function testSupportsTheIsSubjectPristinePrecondition(): void
8179
],
8280
);
8381

84-
iterator_count($this->client->writeEvents([
82+
$this->client->writeEvents([
8583
$firstEvent,
86-
]));
84+
]);
8785

8886
$secondEvent = new EventCandidate(
8987
source: 'https://www.eventsourcingdb.io',
@@ -117,9 +115,9 @@ public function testSupportsTheIsSubjectOnEventIdPrecondition(): void
117115
],
118116
);
119117

120-
iterator_count($this->client->writeEvents([
118+
$this->client->writeEvents([
121119
$firstEvent,
122-
]));
120+
]);
123121

124122
$secondEvent = new EventCandidate(
125123
source: 'https://www.eventsourcingdb.io',
@@ -152,9 +150,9 @@ public function testSupportsTheIsEventQlTruePrecondition(): void
152150
],
153151
);
154152

155-
iterator_count($this->client->writeEvents([
153+
$this->client->writeEvents([
156154
$firstEvent,
157-
]));
155+
]);
158156

159157
$secondEvent = new EventCandidate(
160158
source: 'https://www.eventsourcingdb.io',

0 commit comments

Comments
 (0)