forked from Daniel-Marynicz/BehatParallelExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockIterator.php
49 lines (42 loc) · 1.39 KB
/
MockIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace DMarynicz\Tests;
use ArrayIterator;
use Iterator;
use PHPUnit\Framework\MockObject\MockObject;
trait MockIterator
{
/**
* @param MockObject|Iterator<mixed> $iterator
* @param array<mixed> $items
* @param bool $includeCallsToKey
*/
public function mockIteratorItems(Iterator $iterator, array $items, $includeCallsToKey = false): void
{
$arrayIterator = new ArrayIterator($items);
$iterator
->method('current')
->willReturnCallback(static function () use ($arrayIterator) {
return $arrayIterator->current();
});
$iterator
->method('key')
->willReturnCallback(static function () use ($arrayIterator) {
return $arrayIterator->key();
});
$iterator
->method('next')
->willReturnCallback(static function () use ($arrayIterator): void {
$arrayIterator->next();
});
$iterator
->method('rewind')
->willReturnCallback(static function () use ($arrayIterator): void {
$arrayIterator->rewind();
});
$iterator
->method('valid')
->willReturnCallback(static function () use ($arrayIterator): bool {
return $arrayIterator->valid();
});
}
}