Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/Util/PhpUnit/IterableMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

trait IterableMockTrait
{
private function addIterableValuesToMock(MockObject $mock, array $returnedValues): MockObject
{
private function addIterableValuesToMock(
MockObject $mock,
array $returnedValues,
int $expectedIterations = 1
): MockObject {
if (false === $mock instanceof \Iterator) {
throw new \Exception('Mock object not implements \Iterator');
}
Expand All @@ -20,34 +23,42 @@ private function addIterableValuesToMock(MockObject $mock, array $returnedValues

$nValues = \count($returnedValues);

$totalNValues = $nValues * $expectedIterations;
$totalReturnValues = $returnedValues;
for ($i = 1; $i < $expectedIterations; $i++) {
\array_push($totalReturnValues, ...$returnedValues);
}

$mock
->expects(
new InvokedCount($nValues),
new InvokedCount($totalNValues),
)
->method(self::returnCurrentElementMethod())
->willReturn(... $returnedValues)
->willReturn(...$totalReturnValues)
;

$mock
->expects(
new InvokedCount($nValues + 1),
new InvokedCount($totalNValues + $expectedIterations),
)
->method(self::currentPositionValidMethod())
->willReturn(
... $this->valuesThatCheckValidWillReturn($nValues),
... $this->valuesThatCheckValidWillReturn($nValues, $expectedIterations),
)
;

return $mock;
}

private function valuesThatCheckValidWillReturn(int $nValues): array
private function valuesThatCheckValidWillReturn(int $nValues, int $expectedIterations): array
{
$values = [];
for($i = 0; $i < $nValues; $i++) {
$values[] = true;
for ($j = 0; $j < $expectedIterations; $j++) {
for ($i = 0; $i < $nValues; $i++) {
$values[] = true;
}
$values[] = false;
}
$values[] = false;

return $values;
}
Expand Down