Skip to content

Commit

Permalink
Fix bug in ClosureIterator::rewind().
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 25, 2019
1 parent bae14bf commit f3779ae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
13 changes: 13 additions & 0 deletions spec/drupol/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ public function it_can_flip(): void
$this::with($input)
->flip()
->shouldIterateAs($output);

$this::with(['a', 'b', 'c', 'd', 'a'])
->flip()
->flip()
->all()
->shouldIterateAs(['a', 'b', 'c', 'd', 'a']);
}

public function it_can_forget(): void
Expand Down Expand Up @@ -1096,6 +1102,13 @@ public function it_can_tail(): void
$this
->tail(100)
->shouldIterateAs(range('A', 'F'));

$this::with(['a', 'b', 'c', 'd', 'a'])
->flip()
->flip()
->tail(2)
->all()
->shouldIterateAs([3 => 'd', 4 => 'a']);
}

public function it_can_until(): void
Expand Down
24 changes: 24 additions & 0 deletions spec/drupol/collection/Iterator/ClosureIteratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ public function it_can_get_a_key(): void
->shouldReturn(0);
}

public function it_can_rewind()
{
$rdmString = static function () {
yield 'foo';
};

$this
->beConstructedWith($rdmString);

$this
->current()
->shouldReturn('foo');

$this
->next()
->current()
->shouldReturn(null);

$this
->rewind()
->current()
->shouldReturn('foo');
}

public function it_can_use_next(): void
{
$callback = static function () {
Expand Down
7 changes: 5 additions & 2 deletions src/Iterator/ClosureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ public function next()
}

/**
* {@inheritdoc}
* @return \drupol\collection\Iterator\ClosureIterator
*/
public function rewind(): void
public function rewind()
{
$this->generator = null;
$this->getGenerator();

return $this;
}

/**
Expand Down

0 comments on commit f3779ae

Please sign in to comment.