-
Notifications
You must be signed in to change notification settings - Fork 8
/
eachTest.php
190 lines (173 loc) · 3.71 KB
/
eachTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* @covers Dash\each
* @covers Dash\Curry\each
*/
class eachTest extends PHPUnit\Framework\TestCase
{
/**
* @dataProvider cases
*/
public function test($iterable, $expected)
{
$self = $this;
$iterated = [];
$iteratee = function ($value, $key, $iterable2) use ($self, $iterable, &$iterated) {
$self->assertSame($iterable, $iterable2);
$iterated[] = $key . ' is ' . $value;
};
$returned = Dash\each($iterable, $iteratee);
$this->assertSame($expected, $iterated);
$this->assertSame($iterable, $returned);
}
/**
* @dataProvider cases
*/
public function testCurried($iterable, $expected)
{
$self = $this;
$iterated = [];
$iteratee = function ($value, $key, $iterable2) use ($self, $iterable, &$iterated) {
$self->assertSame($iterable, $iterable2);
$iterated[] = $key . ' is ' . $value;
};
$each = Dash\Curry\each($iteratee);
$returned = $each($iterable);
$this->assertSame($expected, $iterated);
$this->assertSame($iterable, $returned);
}
public function cases()
{
return [
'With null' => [
'iterable' => null,
'expected' => [],
],
'With an empty array' => [
'iterable' => [],
'expected' => [],
],
'With an indexed array' => [
'iterable' => [
'first',
'second',
'third',
],
'expected' => [
'0 is first',
'1 is second',
'2 is third',
],
],
'With an associative array' => [
'iterable' => [
'a' => 'first',
'b' => 'second',
'c' => 'third',
],
'expected' => [
'a is first',
'b is second',
'c is third',
],
],
'With an empty stdClass' => [
'iterable' => (object) [],
'expected' => [],
],
'With an stdClass' => [
'iterable' => (object) [
'a' => 'first',
'b' => 'second',
'c' => 'third',
],
'expected' => [
'a is first',
'b is second',
'c is third',
],
],
'With an empty ArrayObject' => [
'iterable' => new ArrayObject([]),
'expected' => [],
],
'With an ArrayObject' => [
'iterable' => new ArrayObject([
'a' => 'first',
'b' => 'second',
'c' => 'third',
]),
'expected' => [
'a is first',
'b is second',
'c is third',
],
],
];
}
public function testIterateeArgs()
{
$iterable = ['a' => 1, 'b' => 2, 'c' => 3];
$iterated = [];
$iteratee = function ($value, $key, $passedIterable) use (&$iterated, $iterable) {
$iterated[$key] = $value;
$this->assertSame($iterable, $passedIterable);
};
$returned = Dash\each($iterable, $iteratee);
$this->assertSame($iterable, $iterated);
$this->assertSame($iterable, $returned);
}
public function testEarlyExit()
{
$iterated = [];
Dash\each(['a', 'b', 'c', 'd'], function ($value) use (&$iterated) {
$iterated[] = $value;
if ($value === 'b') {
return false;
}
});
$this->assertSame(['a', 'b'], $iterated);
}
/**
* @dataProvider casesTypeAssertions
*/
public function testTypeAssertions($iterable, $type)
{
$this->expectException(InvalidArgumentException::class);
try {
Dash\each($iterable, function () {
});
} catch (Exception $e) {
$this->assertSame(
"Dash\\each expects iterable or stdClass or null but was given $type",
$e->getMessage()
);
throw $e;
}
}
public function casesTypeAssertions()
{
return [
'With an empty string' => [
'iterable' => '',
'type' => 'string',
],
'With a string' => [
'iterable' => 'hello',
'type' => 'string',
],
'With a zero number' => [
'iterable' => 0,
'type' => 'integer',
],
'With a number' => [
'iterable' => 3.14,
'type' => 'double',
],
'With a DateTime' => [
'iterable' => new DateTime(),
'type' => 'DateTime',
],
];
}
}