|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\IterableFunction\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @license GNU GPL v2+ |
|
11
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
12
|
|
|
*/ |
|
13
|
|
|
class IterableToArrayTest extends TestCase { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @dataProvider arrayProvider |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testGivenArray_itIsReturnedAsIs( array $array ) { |
|
19
|
|
|
$this->assertSame( $array, iterable_to_array( $array ) ); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function arrayProvider() { |
|
23
|
|
|
return [ |
|
24
|
|
|
[ [] ], |
|
25
|
|
|
[ [ 42 ] ], |
|
26
|
|
|
[ [ null, null, null ] ], |
|
27
|
|
|
[ [ [], (object)[], [], (object)[] ] ], |
|
28
|
|
|
[ [ 'foo' => 100, 'bar' => 200, 'baz' => 300 ] ], |
|
29
|
|
|
[ [ 10 => 'foo', 20 => 'bar', 30 => 'baz' ] ], |
|
30
|
|
|
[ [ 2 => 'foo', 'bar', 3 => 'baz' ] ], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @dataProvider traversableProvider |
|
36
|
|
|
*/ |
|
37
|
|
|
public function testGivenTraversable_itIsReturnedAsArray( array $expected, \Traversable $traversable ) { |
|
38
|
|
|
$this->assertSame( $expected, iterable_to_array( $traversable ) ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function traversableProvider() { |
|
42
|
|
|
return [ |
|
43
|
|
|
'empty iterator' => [ |
|
44
|
|
|
[], |
|
45
|
|
|
new \ArrayIterator() |
|
46
|
|
|
], |
|
47
|
|
|
'normal iterator' => [ |
|
48
|
|
|
[ 'a', 'b', 'c' ], |
|
49
|
|
|
new \ArrayIterator( [ 'a', 'b', 'c' ] ) |
|
50
|
|
|
], |
|
51
|
|
|
'iterator with keys' => [ |
|
52
|
|
|
[ 'a' => 10, 'b' => 20, 'c' => 30 ], |
|
53
|
|
|
new \ArrayIterator( [ 'a' => 10, 'b' => 20, 'c' => 30 ] ) |
|
54
|
|
|
], |
|
55
|
|
|
'iterator with some explicit keys' => [ |
|
56
|
|
|
[ 3 => null, 'a' => 10, 20, 'c' => 30 ], |
|
57
|
|
|
new \ArrayIterator( [ 3 => null, 'a' => 10, 20, 'c' => 30 ] ) |
|
58
|
|
|
], |
|
59
|
|
|
'Traversable instance / IteratorAggregate' => [ |
|
60
|
|
|
[ 'a' => 10, 'b' => 20, 'c' => 30 ], |
|
61
|
|
|
new class() implements \IteratorAggregate { |
|
62
|
|
|
public function getIterator() { |
|
63
|
|
|
return new \ArrayIterator( [ 'a' => 10, 'b' => 20, 'c' => 30 ] ); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
], |
|
67
|
|
|
'Generator instance' => [ |
|
68
|
|
|
[ 'a' => 10, 'b' => 20, 'c' => 30 ], |
|
69
|
|
|
( function() { |
|
70
|
|
|
yield 'a' => 10; |
|
71
|
|
|
yield 'b' => 20; |
|
72
|
|
|
yield 'c' => 30; |
|
73
|
|
|
} )() |
|
74
|
|
|
] |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|