1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use function Jasny\iterable_project; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Jasny\iterable_project |
10
|
|
|
*/ |
11
|
|
|
class IterableProjectTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
use LazyExecutionIteratorTrait; |
14
|
|
|
|
15
|
|
|
public function fullProvider() |
16
|
|
|
{ |
17
|
|
|
$arrays = [ |
18
|
|
|
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'], |
19
|
|
|
['three' => 'san', 'one' => 'yi', 'two' => 'er', 'five' => 'wu', 'four' => 'si'], |
20
|
|
|
['two' => 'twee', 'five' => 'vijf', 'one' => 'één', 'three' => 'drie', 'four' => 'vier'] |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
$objects = [ |
24
|
|
|
(object)['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'], |
25
|
|
|
(object)['three' => 'san', 'one' => 'yi', 'two' => 'er', 'five' => 'wu', 'four' => 'si'], |
26
|
|
|
(object)['two' => 'twee', 'five' => 'vijf', 'one' => 'één', 'three' => 'drie', 'four' => 'vier'] |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
$mixed = [ |
30
|
|
|
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'], |
31
|
|
|
(object)['three' => 'san', 'one' => 'yi', 'two' => 'er', 'five' => 'wu', 'four' => 'si'], |
32
|
|
|
new \ArrayObject(['two' => 'twee', 'five' => 'vijf', 'one' => 'één', 'three' => 'drie', 'four' => 'vier']) |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
return [ |
36
|
|
|
[$arrays], |
37
|
|
|
[$objects], |
38
|
|
|
[$mixed] |
39
|
|
|
]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @dataProvider fullProvider |
44
|
|
|
*/ |
45
|
|
|
public function test(array $values) |
46
|
|
|
{ |
47
|
|
|
$map = ['I' => 'one', 'II' => 'two', 'III' => 'three', 'IV' => 'four', 'V' => 'five']; |
48
|
|
|
|
49
|
|
|
$iterator = iterable_project($values, $map); |
50
|
|
|
|
51
|
|
|
$result = iterator_to_array($iterator); |
52
|
|
|
|
53
|
|
|
$expected = [ |
54
|
|
|
['I' => 'uno', 'II' => 'dos', 'III' => 'tres', 'IV' => 'cuatro', 'V' => 'cinco'], |
55
|
|
|
['I' => 'yi', 'II' => 'er', 'III' => 'san', 'IV' => 'si', 'V' => 'wu'], |
56
|
|
|
['I' => 'één', 'II' => 'twee', 'III' => 'drie', 'IV' => 'vier', 'V' => 'vijf'] |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$this->assertSame($expected, $result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @dataProvider fullProvider |
64
|
|
|
*/ |
65
|
|
|
public function testIterator(array $values) |
66
|
|
|
{ |
67
|
|
|
$inner = new \ArrayIterator($values); |
68
|
|
|
|
69
|
|
|
$map = ['I' => 'one', 'II' => 'two', 'III' => 'three', 'IV' => 'four', 'V' => 'five']; |
70
|
|
|
|
71
|
|
|
$iterator = iterable_project($inner, $map); |
72
|
|
|
|
73
|
|
|
$result = iterator_to_array($iterator); |
74
|
|
|
|
75
|
|
|
$expected = [ |
76
|
|
|
['I' => 'uno', 'II' => 'dos', 'III' => 'tres', 'IV' => 'cuatro', 'V' => 'cinco'], |
77
|
|
|
['I' => 'yi', 'II' => 'er', 'III' => 'san', 'IV' => 'si', 'V' => 'wu'], |
78
|
|
|
['I' => 'één', 'II' => 'twee', 'III' => 'drie', 'IV' => 'vier', 'V' => 'vijf'] |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
$this->assertSame($expected, $result); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function partialProvider() |
85
|
|
|
{ |
86
|
|
|
$arrays = [ |
87
|
|
|
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'], |
88
|
|
|
['three' => 'san', 'two' => 'er', 'five' => 'wu', 'four' => 'si'], |
89
|
|
|
['two' => 'twee', 'four' => 'vier'] |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$objects = [ |
93
|
|
|
(object)['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'], |
94
|
|
|
(object)['three' => 'san', 'two' => 'er', 'five' => 'wu', 'four' => 'si'], |
95
|
|
|
(object)['two' => 'twee', 'four' => 'vier'] |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
$mixed = [ |
99
|
|
|
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'], |
100
|
|
|
(object)['three' => 'san', 'two' => 'er', 'five' => 'wu', 'four' => 'si'], |
101
|
|
|
new \ArrayObject(['two' => 'twee', 'four' => 'vier']) |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
return [ |
105
|
|
|
[$arrays], |
106
|
|
|
[$objects], |
107
|
|
|
[$mixed] |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @dataProvider partialProvider |
113
|
|
|
*/ |
114
|
|
|
public function testFill(array $values) |
115
|
|
|
{ |
116
|
|
|
$map = ['one', 'three', 'four']; |
117
|
|
|
|
118
|
|
|
$iterator = iterable_project($values, $map); |
119
|
|
|
|
120
|
|
|
$result = iterator_to_array($iterator); |
121
|
|
|
|
122
|
|
|
$expected = [ |
123
|
|
|
['uno', 'tres', 'cuatro'], |
124
|
|
|
[null, 'san', 'si'], |
125
|
|
|
[null, null, 'vier'] |
126
|
|
|
]; |
127
|
|
|
|
128
|
|
|
$this->assertSame($expected, $result); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testScalars() |
132
|
|
|
{ |
133
|
|
|
$date = new \DateTime(); |
134
|
|
|
|
135
|
|
|
$values = [ |
136
|
|
|
['one' => 'uno', 'two' => 'dos', 'three' => 'tres', 'four' => 'cuatro', 'five' => 'cinco'], |
137
|
|
|
null, |
138
|
|
|
42, |
139
|
|
|
'hello', |
140
|
|
|
$date, |
141
|
|
|
(object)['two' => 'twee', 'five' => 'vijf', 'one' => 'één', 'three' => 'drie', 'four' => 'vier'] |
142
|
|
|
]; |
143
|
|
|
$inner = new \ArrayIterator($values); |
144
|
|
|
|
145
|
|
|
$map = ['one', 'three', 'four']; |
146
|
|
|
|
147
|
|
|
$iterator = iterable_project($inner, $map); |
148
|
|
|
|
149
|
|
|
$result = iterator_to_array($iterator); |
150
|
|
|
|
151
|
|
|
$expected = [ |
152
|
|
|
['uno', 'tres', 'cuatro'], |
153
|
|
|
null, |
154
|
|
|
42, |
155
|
|
|
'hello', |
156
|
|
|
$date, |
157
|
|
|
['één', 'drie', 'vier'] |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
$this->assertSame($expected, $result); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testEmpty() |
164
|
|
|
{ |
165
|
|
|
$map = ['I' => 'one', 'II' => 'two', 'III' => 'three', 'IV' => 'four', 'V' => 'five']; |
166
|
|
|
|
167
|
|
|
$iterator = iterable_project(new \EmptyIterator(), $map); |
168
|
|
|
$result = iterator_to_array($iterator); |
169
|
|
|
|
170
|
|
|
$this->assertEquals([], $result); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Test that nothing happens when not iterating |
175
|
|
|
*/ |
176
|
|
|
public function testLazyExecution() |
177
|
|
|
{ |
178
|
|
|
$iterator = $this->createLazyExecutionIterator(); |
179
|
|
|
|
180
|
|
|
iterable_project($iterator, ['foo']); |
181
|
|
|
|
182
|
|
|
$this->assertTrue(true, "No warning"); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|