Skip to content

Commit

Permalink
fix upcasting iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Feb 11, 2017
1 parent 778730a commit 6797c6d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 15 deletions.
43 changes: 32 additions & 11 deletions src/Upcasting/UpcastingIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ final class UpcastingIterator implements Iterator
*/
private $storedMessages = [];

private $position = 0;

public function __construct(Upcaster $upcaster, Iterator $iterator)
{
$this->upcaster = $upcaster;
Expand All @@ -44,29 +46,47 @@ public function current(): ?Message
return reset($this->storedMessages);
}

$messages = $this->upcaster->upcast($this->innerIterator->current());
$current = $this->innerIterator->current();

if (null === $current) {
return null;
}

if (empty($messages)) {
$this->next();
$this->storedMessages = $this->upcaster->upcast($current);

if ($this->valid()) {
return $this->current();
while (empty($this->storedMessages)) {
$this->innerIterator->next();

if (! $this->innerIterator->valid()) {
return null;
}
}

$this->storedMessages = $messages;
$this->storedMessages = $this->upcaster->upcast($this->innerIterator->current());
}

return reset($messages);
return reset($this->storedMessages);
}

public function next(): void
{
++$this->position;

if (! empty($this->storedMessages)) {
array_shift($this->storedMessages);
}

if (empty($this->storedMessages)) {
if (! empty($this->storedMessages)) {
return;
}

while (empty($this->storedMessages)) {
$this->innerIterator->next();

if (! $this->innerIterator->valid()) {
return;
}

$this->storedMessages = $this->upcaster->upcast($this->innerIterator->current());
}
}

Expand All @@ -75,17 +95,18 @@ public function next(): void
*/
public function key()
{
return $this->innerIterator->key();
return $this->position;
}

public function valid(): bool
{
return $this->innerIterator->valid();
return null !== $this->current();
}

public function rewind(): void
{
$this->storedMessages = [];
$this->innerIterator->rewind();
$this->position = 0;
}
}
48 changes: 44 additions & 4 deletions tests/Upcasting/UpcastingIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function it_iterates(): void
$upcastedMessage2 = $upcastedMessage2->reveal();

$message1 = $this->prophesize(Message::class);
$message1->metadata()->willReturn([])->shouldBeCalled();
$message1->withAddedMetadata('key', 'value')->willReturn($upcastedMessage1)->shouldBeCalled();
$message1->withAddedMetadata('key', 'another_value')->willReturn($upcastedMessage2)->shouldBeCalled();
$message1->metadata()->willReturn(['foo' => 'baz'])->shouldBeCalled();
$message1 = $message1->reveal();

$message2 = $this->prophesize(Message::class);
$message2->metadata()->willReturn(['foo' => 'baz'])->shouldBeCalled();
$message2->metadata()->willReturn([])->shouldBeCalled();
$message2->withAddedMetadata('key', 'value')->willReturn($upcastedMessage1)->shouldBeCalled();
$message2->withAddedMetadata('key', 'another_value')->willReturn($upcastedMessage2)->shouldBeCalled();
$message2 = $message2->reveal();

$message3 = $this->prophesize(Message::class);
Expand All @@ -46,10 +46,15 @@ public function it_iterates(): void
$message3->withAddedMetadata('key', 'another_value')->shouldNotBeCalled();
$message3 = $message3->reveal();

$message4 = $this->prophesize(Message::class);
$message4->metadata()->willReturn(['foo' => 'baz'])->shouldBeCalled();
$message4 = $message4->reveal();

$iterator = new \ArrayIterator([
$message1,
$message2,
$message3,
$message4,
]);

$upcastingIterator = new UpcastingIterator($this->createUpcaster(), $iterator);
Expand All @@ -69,6 +74,41 @@ public function it_iterates(): void
$this->assertSame($upcastedMessage2, $upcastingIterator->current());
$upcastingIterator->next();
$this->assertSame($message3, $upcastingIterator->current());
$upcastingIterator->next();
$this->assertFalse($upcastingIterator->valid());
$this->assertNull($upcastingIterator->current());
}

/**
* @test
*/
public function it_iterates_on_iterator_with_removed_messages_only(): void
{
$message = $this->prophesize(Message::class);
$message->metadata()->willReturn(['foo' => 'baz'])->shouldBeCalled();
$message = $message->reveal();

$iterator = new \ArrayIterator([$message]);

$upcastingIterator = new UpcastingIterator($this->createUpcaster(), $iterator);

$this->assertEquals(0, $upcastingIterator->key());
$this->assertFalse($upcastingIterator->valid());
$this->assertNull($upcastingIterator->current());
}

/**
* @test
*/
public function it_iterates_over_empty_iterator(): void
{
$iterator = new \ArrayIterator();

$upcastingIterator = new UpcastingIterator($this->createUpcaster(), $iterator);

$this->assertEquals(0, $upcastingIterator->key());
$this->assertFalse($upcastingIterator->valid());
$this->assertNull($upcastingIterator->current());
}

protected function createUpcaster(): SingleEventUpcaster
Expand Down

0 comments on commit 6797c6d

Please sign in to comment.