|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\IterableFunction\Tests\Unit; |
|
6
|
|
|
|
|
7
|
|
|
use IteratorAggregate; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @license GNU GPL v2+ |
|
12
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
13
|
|
|
*/ |
|
14
|
|
|
class IterableCombineTest extends TestCase { |
|
15
|
|
|
|
|
16
|
|
|
public function testTakesDifferentIterableTypes() { |
|
17
|
|
|
$combinedValues = iterable_merge( |
|
18
|
|
|
[ 1, 2 ], |
|
19
|
|
|
new \ArrayIterator( [ 3, 4 ] ), |
|
20
|
|
|
$this->newGeneratorAggregate( [ 5, 6 ] ) |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertCount( 6, $combinedValues ); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
private function newGeneratorAggregate( iterable $values ): \IteratorAggregate { |
|
27
|
|
|
return new class( $values ) implements IteratorAggregate { |
|
28
|
|
|
private $values; |
|
29
|
|
|
public function __construct( iterable $values ) { |
|
30
|
|
|
$this->values = $values; |
|
31
|
|
|
} |
|
32
|
|
|
public function getIterator() { |
|
33
|
|
|
yield from $this->values; |
|
34
|
|
|
} |
|
35
|
|
|
}; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testNumericKeysDoNotOverrideEachOther() { |
|
39
|
|
|
$combinedValues = iterable_merge( |
|
40
|
|
|
[ 1, 2 ], |
|
41
|
|
|
[ 3, 4 ] |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertSame( [ 1, 2, 3, 4 ], iterator_to_array( $combinedValues ) ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testStringKeysNotOverrideEachOther() { |
|
48
|
|
|
$combinedValues = iterable_merge( |
|
49
|
|
|
[ 'key' => 1 ], |
|
50
|
|
|
[ 'key' => 2 ], |
|
51
|
|
|
[ 'such' => 3 ] |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame( [ 'key' => 2, 'such' => 3 ], iterator_to_array( $combinedValues ) ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider arrayInputsProvider |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testEquivalencyWithArrayMergeAfterIteratorToArray( array ...$arrays ) { |
|
61
|
|
|
$this->assertSame( |
|
62
|
|
|
array_merge( ...$arrays ), |
|
63
|
|
|
iterator_to_array( iterable_merge( ...$arrays ) ) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function arrayInputsProvider() { |
|
68
|
|
|
yield 'empty arrays' => [ |
|
69
|
|
|
[], |
|
70
|
|
|
[] |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
yield 'numeric indexed arrays' => [ |
|
74
|
|
|
[ 'a', 'b' ], |
|
75
|
|
|
[ 'c', 'd' ] |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
|
|
yield 'special numeric indexes' => [ |
|
79
|
|
|
[ 10 => 'a', 'b' ], |
|
80
|
|
|
[ 'c', 20 => 'd' ] |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
yield 'string indexes mixed with numeric ones' => [ |
|
84
|
|
|
[ 'foo' => 'a', 'b' ], |
|
85
|
|
|
[ 'c', 'bar' => 'd' ] |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
yield 'duplicate keys' => [ |
|
89
|
|
|
[ 'foo' => 'a', 'b', 42 => 'x' ], |
|
90
|
|
|
[ 'c', 'foo' => 'd', 42 => 'y' ] |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
yield 'one array' => [ |
|
94
|
|
|
[ 'c', 'foo' => 'd', 42 => 'y' ] |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
yield 'three arrays' => [ |
|
98
|
|
|
[ 'such' ], |
|
99
|
|
|
[ 'foo' => 'bar' ], |
|
100
|
|
|
[ 42 ] |
|
101
|
|
|
]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testNoArgumentsResultInEmptyGenerator() { |
|
105
|
|
|
$this->assertSame( [], iterator_to_array( iterable_merge() ) ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: