Skip to content

Commit

Permalink
fix show command & add 'end'/'next' method in stream interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jan 15, 2024
1 parent b0e4be2 commit 7c6a22b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/Console/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Store/ArrayStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/Store/DoctrineDbalStoreStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Store/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
31 changes: 25 additions & 6 deletions tests/Unit/Store/ArrayStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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());
}
}

0 comments on commit 7c6a22b

Please sign in to comment.