diff --git a/src/Console/Command/ShowCommand.php b/src/Console/Command/ShowCommand.php index 3c42ac613..fb946fb41 100644 --- a/src/Console/Command/ShowCommand.php +++ b/src/Console/Command/ShowCommand.php @@ -62,12 +62,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int do { $i = 0; - foreach ($stream as $message) { + while (!$stream->end()) { $i++; $currentCount++; + $message = $stream->current(); + + if (!$message) { + break 2; + } + $console->message($this->serializer, $message); + $stream->next(); + if ($i >= $limit) { break; } diff --git a/src/Store/ArrayStream.php b/src/Store/ArrayStream.php index 9404fe1a1..eff035107 100644 --- a/src/Store/ArrayStream.php +++ b/src/Store/ArrayStream.php @@ -65,6 +65,16 @@ public function index(): int|null return $this->index; } + public function next(): void + { + $this->iterator->next(); + } + + public function end(): bool + { + return !$this->iterator->valid(); + } + public function current(): Message|null { return $this->iterator->current() ?: null; diff --git a/src/Store/DoctrineDbalStoreStream.php b/src/Store/DoctrineDbalStoreStream.php index 745d7d57b..3546294b8 100644 --- a/src/Store/DoctrineDbalStoreStream.php +++ b/src/Store/DoctrineDbalStoreStream.php @@ -42,6 +42,16 @@ public function close(): void $this->result->free(); } + public function next(): void + { + $this->generator->next(); + } + + public function end(): bool + { + return !$this->generator->valid(); + } + public function current(): Message|null { return $this->generator->current() ?: null; diff --git a/src/Store/Stream.php b/src/Store/Stream.php index ccbffae80..9fa960e50 100644 --- a/src/Store/Stream.php +++ b/src/Store/Stream.php @@ -12,8 +12,12 @@ interface Stream extends Traversable { public function close(): void; + public function next(): void; + public function current(): Message|null; + public function end(): bool; + /** @return positive-int|0|null */ public function position(): int|null; diff --git a/tests/Unit/Store/ArrayStreamTest.php b/tests/Unit/Store/ArrayStreamTest.php index 0873d75c6..c5396202f 100644 --- a/tests/Unit/Store/ArrayStreamTest.php +++ b/tests/Unit/Store/ArrayStreamTest.php @@ -26,13 +26,14 @@ public function testEmpty(): void self::assertSame(null, $stream->position()); self::assertSame(null, $stream->current()); self::assertSame(null, $stream->index()); + self::assertSame(true, $stream->end()); $array = iterator_to_array($stream); self::assertSame([], $array); } - public function testWithMessages(): void + public function testOneMessage(): void { $message = Message::create( new ProfileCreated( @@ -48,17 +49,17 @@ public function testWithMessages(): void self::assertSame(1, $stream->index()); self::assertSame(0, $stream->position()); self::assertSame($message, $stream->current()); + self::assertSame(false, $stream->end()); - $array = iterator_to_array($stream); + $stream->next(); self::assertSame(1, $stream->index()); self::assertSame(0, $stream->position()); self::assertSame(null, $stream->current()); - - self::assertSame($messages, $array); + self::assertSame(true, $stream->end()); } - public function testPosition(): void + public function testMultipleMessages(): void { $messages = [ Message::create( @@ -85,10 +86,28 @@ public function testPosition(): void self::assertSame(1, $stream->index()); self::assertSame(0, $stream->position()); + self::assertSame($messages[0], $stream->current()); + self::assertSame(false, $stream->end()); + + $stream->next(); - iterator_to_array($stream); + self::assertSame(2, $stream->index()); + self::assertSame(1, $stream->position()); + self::assertSame($messages[1], $stream->current()); + self::assertSame(false, $stream->end()); + + $stream->next(); self::assertSame(3, $stream->index()); self::assertSame(2, $stream->position()); + self::assertSame($messages[2], $stream->current()); + self::assertSame(false, $stream->end()); + + $stream->next(); + + self::assertSame(3, $stream->index()); + self::assertSame(2, $stream->position()); + self::assertSame(null, $stream->current()); + self::assertSame(true, $stream->end()); } }