1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use function Jasny\iterable_group; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \Jasny\iterable_group |
10
|
|
|
*/ |
11
|
|
|
class IterableGroupTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
use ProvideIterablesTrait; |
14
|
|
|
use LazyExecutionIteratorTrait; |
15
|
|
|
|
16
|
|
|
public function provider() |
17
|
|
|
{ |
18
|
|
|
$objects = [ |
19
|
|
|
(object)['type' => 'one'], |
20
|
|
|
(object)['type' => 'two'], |
21
|
|
|
(object)['type' => 'one'], |
22
|
|
|
(object)['type' => 'three'], |
23
|
|
|
(object)['type' => 'one'], |
24
|
|
|
(object)['type' => 'two'] |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
return $this->provideIterables($objects); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @dataProvider provider |
32
|
|
|
*/ |
33
|
|
|
public function test($iterable, $objects) |
34
|
|
|
{ |
35
|
|
|
$iterator = iterable_group($iterable, function(\stdClass $object) { |
36
|
|
|
return $object->type; |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
$result = iterator_to_array($iterator); |
40
|
|
|
|
41
|
|
|
$expected = [ |
42
|
|
|
'one' => [ |
43
|
|
|
$objects[0], |
44
|
|
|
$objects[2], |
45
|
|
|
$objects[4] |
46
|
|
|
], |
47
|
|
|
'two' => [ |
48
|
|
|
$objects[1], |
49
|
|
|
$objects[5] |
50
|
|
|
], |
51
|
|
|
'three' => [ |
52
|
|
|
$objects[3] |
53
|
|
|
] |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
$this->assertSame($expected, $result); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testMixed() |
60
|
|
|
{ |
61
|
|
|
$parents = [ |
62
|
|
|
new \stdClass(), |
63
|
|
|
new \stdClass(), |
64
|
|
|
null |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$objects = [ |
68
|
|
|
(object)['type' => $parents[0]], |
69
|
|
|
(object)['type' => $parents[1]], |
70
|
|
|
(object)['type' => $parents[0]], |
71
|
|
|
(object)['type' => null], |
72
|
|
|
(object)['type' => $parents[0]], |
73
|
|
|
(object)['type' => $parents[1]] |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
$iterator = iterable_group($objects, function(\stdClass $object) { |
77
|
|
|
return $object->type; |
78
|
|
|
}); |
79
|
|
|
|
80
|
|
|
$resultKeys = []; |
81
|
|
|
$resultValues = []; |
82
|
|
|
|
83
|
|
|
foreach ($iterator as $key => $value) { |
84
|
|
|
$resultKeys[] = $key; |
85
|
|
|
$resultValues[] = $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$expectedValues = [ |
89
|
|
|
[ |
90
|
|
|
$objects[0], |
91
|
|
|
$objects[2], |
92
|
|
|
$objects[4] |
93
|
|
|
], |
94
|
|
|
[ |
95
|
|
|
$objects[1], |
96
|
|
|
$objects[5] |
97
|
|
|
], |
98
|
|
|
[ |
99
|
|
|
$objects[3] |
100
|
|
|
] |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
$this->assertSame($parents, $resultKeys); |
104
|
|
|
$this->assertSame($expectedValues, $resultValues); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testKey() |
108
|
|
|
{ |
109
|
|
|
$values = ['alpha' => 'one', 'bat' => 'two', 'apple' => 'three', 'cat' => 'four', 'air' => 'five', |
110
|
|
|
'beast' => 'six']; |
111
|
|
|
|
112
|
|
|
$iterator = iterable_group($values, function($value, $key) { |
113
|
|
|
return substr($key, 0, 1); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
$result = iterator_to_array($iterator); |
117
|
|
|
|
118
|
|
|
$expected = [ |
119
|
|
|
'a' => ['one', 'three', 'five'], |
120
|
|
|
'b' => ['two', 'six'], |
121
|
|
|
'c' => ['four'] |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
$this->assertSame($expected, $result); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testEmpty() |
128
|
|
|
{ |
129
|
|
|
$iterator = iterable_group(new \EmptyIterator(), function() {}); |
130
|
|
|
|
131
|
|
|
$result = iterator_to_array($iterator); |
132
|
|
|
|
133
|
|
|
$this->assertEquals([], $result); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Test that nothing happens when not iterating |
138
|
|
|
*/ |
139
|
|
|
public function testLazyExecution() |
140
|
|
|
{ |
141
|
|
|
$iterator = $this->createLazyExecutionIterator(); |
142
|
|
|
|
143
|
|
|
iterable_group($iterator, function() {}); |
144
|
|
|
|
145
|
|
|
$this->assertTrue(true, "No warning"); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|