diff --git a/test/IteratorMockTrait.php b/test/IteratorMockTrait.php new file mode 100644 index 000000000..3058e4a38 --- /dev/null +++ b/test/IteratorMockTrait.php @@ -0,0 +1,106 @@ +array = $items; + $iteratorData->position = 0; + + $iteratorMock = $this->createMock($originalClassName); + + $iteratorMock + ->expects($this->any()) + ->method('rewind') + ->will( + $this->returnCallback( + static function() use ($iteratorData): void { + $iteratorData->position = 0; + } + ) + ); + + $iteratorMock + ->expects($this->any()) + ->method('current') + ->will( + $this->returnCallback( + static function() use ($iteratorData) { + return $iteratorData->array[$iteratorData->position]; + } + ) + ); + + $iteratorMock + ->expects($this->any()) + ->method('key') + ->will( + $this->returnCallback( + static function() use ($iteratorData) { + return $iteratorData->position; + } + ) + ); + + $iteratorMock + ->expects($this->any()) + ->method('next') + ->will( + $this->returnCallback( + static function() use ($iteratorData): void { + ++$iteratorData->position; + } + ) + ); + + $iteratorMock + ->expects($this->any()) + ->method('valid') + ->will( + $this->returnCallback( + static function() use ($iteratorData): bool { + return isset($iteratorData->array[$iteratorData->position]); + } + ) + ); + + $iteratorMock + ->expects($this->any()) + ->method('count') + ->will( + $this->returnCallback( + static function() use ($iteratorData): int { + return count($iteratorData->array); + } + ) + ); + + return $iteratorMock; + } +}