Skip to content

Commit 79ea80a

Browse files
wundiiawu
andauthored
feat: implement runEventQlQuery method and add corresponding tests (#17)
Co-authored-by: awu <andreas.wunderwald@westpress.de>
1 parent 17ee21a commit 79ea80a

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

src/Client.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,44 @@ public function readEvents(string $subject, ReadEventsOptions $readEventsOptions
184184
}
185185
}
186186
}
187+
188+
public function runEventQlQuery(string $query): iterable
189+
{
190+
$requestBody = [
191+
'query' => $query,
192+
];
193+
194+
$response = $this->httpClient->post(
195+
'/api/v1/run-eventql-query',
196+
[
197+
'headers' => [
198+
'Authorization' => 'Bearer ' . $this->apiToken,
199+
'Content-Type' => 'application/json',
200+
],
201+
'json' => $requestBody,
202+
],
203+
);
204+
$status = $response->getStatusCode();
205+
206+
if ($status !== 200) {
207+
throw new RuntimeException(sprintf(
208+
"Failed to run EventQL query, got HTTP status code '%d', expected '200'",
209+
$status
210+
));
211+
}
212+
213+
foreach (NdJson::readStream($response->getBody()) as $eventLine) {
214+
switch ($eventLine->type) {
215+
case 'row':
216+
$row = $eventLine->payload;
217+
yield $row;
218+
219+
break;
220+
case 'error':
221+
throw new RuntimeException($eventLine->payload['error'] ?? 'unknown error');
222+
default:
223+
throw new RuntimeException("Failed to handle unsupported line type {$eventLine->type}");
224+
}
225+
}
226+
}
187227
}

tests/RunEventQlQueryTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Thenativeweb\Eventsourcingdb\EventCandidate;
7+
use Thenativeweb\Eventsourcingdb\Tests\ClientTestTrait;
8+
9+
final class RunEventQlQueryTest extends TestCase
10+
{
11+
use ClientTestTrait;
12+
13+
public function testNoRowsIfTheQueryDoesNotReturnAnyRows(): void
14+
{
15+
$didReadRows = false;
16+
foreach ($this->client->runEventQlQuery('FROM e IN events PROJECT INTO e') as $event) {
17+
$didReadRows = true;
18+
}
19+
20+
$this->assertFalse($didReadRows, 'Expected no rows to be read, but some were found.');
21+
}
22+
23+
public function testReadsAllRowsTheQueryReturn(): void
24+
{
25+
$firstEvent = new EventCandidate(
26+
source: 'https://www.eventsourcingdb.io',
27+
subject: '/test',
28+
type: 'io.eventsourcingdb.test',
29+
data: [
30+
'value' => 23,
31+
],
32+
);
33+
34+
$secondEvent = new EventCandidate(
35+
source: 'https://www.eventsourcingdb.io',
36+
subject: '/test',
37+
type: 'io.eventsourcingdb.test',
38+
data: [
39+
'value' => 42,
40+
],
41+
);
42+
43+
iterator_count($this->client->writeEvents([
44+
$firstEvent,
45+
$secondEvent,
46+
]));
47+
48+
$rowsRead = [];
49+
foreach ($this->client->runEventQlQuery('FROM e IN events PROJECT INTO e') as $row) {
50+
$rowsRead[] = $row;
51+
}
52+
53+
$this->assertCount(2, $rowsRead);
54+
$this->assertSame('0', $rowsRead[0]['id']);
55+
$this->assertSame(23, $rowsRead[0]['data']['value']);
56+
$this->assertSame('1', $rowsRead[1]['id']);
57+
$this->assertSame(42, $rowsRead[1]['data']['value']);
58+
}
59+
}

0 commit comments

Comments
 (0)