|
| 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