|
3 | 3 | namespace MongoDB\Tests\Model;
|
4 | 4 |
|
5 | 5 | use ArrayIterator;
|
| 6 | +use Generator; |
| 7 | +use Iterator; |
| 8 | +use IteratorAggregate; |
6 | 9 | use MongoDB\Model\CallbackIterator;
|
7 | 10 | use MongoDB\Tests\TestCase;
|
8 | 11 |
|
9 |
| -use function array_keys; |
10 | 12 | use function iterator_to_array;
|
11 |
| -use function strrev; |
12 | 13 |
|
13 | 14 | class CallbackIteratorTest extends TestCase
|
14 | 15 | {
|
15 |
| - public function testArrayIteration(): void |
| 16 | + /** @dataProvider provideTests */ |
| 17 | + public function testIteration($expected, $source, $callback): void |
16 | 18 | {
|
17 |
| - $expectedKey = 0; |
18 |
| - |
19 |
| - $original = [1, 2, 3]; |
20 |
| - |
21 | 19 | $callbackIterator = new CallbackIterator(
|
22 |
| - new ArrayIterator($original), |
23 |
| - function ($value, $key) use (&$expectedKey) { |
24 |
| - $this->assertSame($expectedKey, $key); |
25 |
| - $expectedKey++; |
26 |
| - |
27 |
| - return $value * 2; |
28 |
| - } |
| 20 | + $source, |
| 21 | + $callback |
29 | 22 | );
|
30 | 23 |
|
31 |
| - $this->assertSame([2, 4, 6], iterator_to_array($callbackIterator)); |
| 24 | + $this->assertEquals($expected, iterator_to_array($callbackIterator)); |
32 | 25 | }
|
33 | 26 |
|
34 |
| - public function testHashIteration(): void |
| 27 | + public static function provideTests(): Generator |
35 | 28 | {
|
36 |
| - $expectedKey = 0; |
| 29 | + $listIterator = new ArrayIterator([1, 2, 3]); |
| 30 | + $hashIterator = new ArrayIterator(['a' => 1, 'b' => 2, 'c' => 3]); |
37 | 31 |
|
38 |
| - $original = ['a' => 1, 'b' => 2, 'c' => 3]; |
39 |
| - $expectedKeys = array_keys($original); |
| 32 | + $iteratorAggregate = new class ($listIterator) implements IteratorAggregate |
| 33 | + { |
| 34 | + private $iterator; |
40 | 35 |
|
41 |
| - $callbackIterator = new CallbackIterator( |
42 |
| - new ArrayIterator($original), |
43 |
| - function ($value, $key) use (&$expectedKey, $expectedKeys) { |
44 |
| - $this->assertSame($expectedKeys[$expectedKey], $key); |
45 |
| - $expectedKey++; |
| 36 | + public function __construct(Iterator $iterator) |
| 37 | + { |
| 38 | + $this->iterator = $iterator; |
| 39 | + } |
46 | 40 |
|
47 |
| - return $value * 2; |
| 41 | + public function getIterator(): Iterator |
| 42 | + { |
| 43 | + return $this->iterator; |
48 | 44 | }
|
49 |
| - ); |
| 45 | + }; |
50 | 46 |
|
51 |
| - $this->assertSame(['a' => 2, 'b' => 4, 'c' => 6], iterator_to_array($callbackIterator)); |
52 |
| - } |
| 47 | + yield 'List with closure' => [ |
| 48 | + 'expected' => [2, 4, 6], |
| 49 | + 'source' => $listIterator, |
| 50 | + 'callback' => function ($value, $key) use ($listIterator) { |
| 51 | + self::assertSame($listIterator->key(), $key); |
53 | 52 |
|
54 |
| - public function testWithCallable(): void |
55 |
| - { |
56 |
| - $original = ['foo', 'bar', 'baz']; |
| 53 | + return $value * 2; |
| 54 | + }, |
| 55 | + ]; |
57 | 56 |
|
58 |
| - $callbackIterator = new CallbackIterator( |
59 |
| - new ArrayIterator($original), |
60 |
| - [self::class, 'reverseValue'] |
61 |
| - ); |
| 57 | + yield 'List with callable' => [ |
| 58 | + 'expected' => [2, 4, 6], |
| 59 | + 'source' => $listIterator, |
| 60 | + 'callback' => [self::class, 'doubleValue'], |
| 61 | + ]; |
| 62 | + |
| 63 | + yield 'Hash with closure' => [ |
| 64 | + 'expected' => ['a' => 2, 'b' => 4, 'c' => 6], |
| 65 | + 'source' => $hashIterator, |
| 66 | + 'callback' => function ($value, $key) use ($hashIterator) { |
| 67 | + self::assertSame($hashIterator->key(), $key); |
62 | 68 |
|
63 |
| - $this->assertSame(['oof', 'rab', 'zab'], iterator_to_array($callbackIterator)); |
| 69 | + return $value * 2; |
| 70 | + }, |
| 71 | + ]; |
| 72 | + |
| 73 | + yield 'IteratorAggregate with closure' => [ |
| 74 | + 'expected' => [2, 4, 6], |
| 75 | + 'source' => $iteratorAggregate, |
| 76 | + 'callback' => function ($value, $key) use ($listIterator) { |
| 77 | + self::assertSame($listIterator->key(), $key); |
| 78 | + |
| 79 | + return $value * 2; |
| 80 | + }, |
| 81 | + ]; |
64 | 82 | }
|
65 | 83 |
|
66 |
| - public static function reverseValue($value, $key) |
| 84 | + public static function doubleValue($value, $key) |
67 | 85 | {
|
68 |
| - return strrev($value); |
| 86 | + return $value * 2; |
69 | 87 | }
|
70 | 88 | }
|
0 commit comments