diff --git a/spec/loophp/collection/Iterator/RandomIteratorSpec.php b/spec/loophp/collection/Iterator/RandomIteratorSpec.php index c548877e1..83cf5d9bb 100644 --- a/spec/loophp/collection/Iterator/RandomIteratorSpec.php +++ b/spec/loophp/collection/Iterator/RandomIteratorSpec.php @@ -15,6 +15,38 @@ public function it_can_get_the_innerIterator() $this->getInnerIterator()->shouldBeAnInstanceOf(ArrayIterator::class); } + public function it_can_rewind() + { + $iterator = new ArrayIterator(['a']); + + $this->beConstructedWith($iterator); + + $this + ->valid() + ->shouldReturn(true); + + $this + ->current() + ->shouldReturn('a'); + + $this->next(); + + $this + ->valid() + ->shouldReturn(false); + + $this + ->rewind(); + + $this + ->valid() + ->shouldReturn(true); + + $this + ->current() + ->shouldReturn('a'); + } + public function it_is_initializable() { $this->shouldHaveType(RandomIterator::class); @@ -22,7 +54,7 @@ public function it_is_initializable() public function let() { - $iterator = new ArrayIterator(range(0, 4)); + $iterator = new ArrayIterator(range('a', 'c')); $this->beConstructedWith($iterator); } diff --git a/src/Iterator/RandomIterator.php b/src/Iterator/RandomIterator.php index cb0d3041f..36ea8eff3 100644 --- a/src/Iterator/RandomIterator.php +++ b/src/Iterator/RandomIterator.php @@ -80,6 +80,7 @@ public function next(): void public function rewind() { + $this->indexes = array_keys($this->iterator->getArrayCopy()); $this->iterator->rewind(); }