-
Notifications
You must be signed in to change notification settings - Fork 8
/
findTest.php
379 lines (354 loc) · 12 KB
/
findTest.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* @covers Dash\find
* @covers Dash\Curry\find
*/
class findTest extends PHPUnit\Framework\TestCase
{
/**
* @dataProvider cases
*/
public function test($iterable, $predicate, $expected)
{
$this->assertEquals($expected, Dash\find($iterable, $predicate));
}
/**
* @dataProvider cases
*/
public function testCurried($iterable, $predicate, $expected)
{
$find = Dash\Curry\find($predicate);
$this->assertEquals($expected, $find($iterable));
}
public function cases()
{
return [
'With null' => [
'iterable' => null,
'predicate' => 'Dash\isOdd',
'expected' => null,
],
'With an empty array' => [
'iterable' => [],
'predicate' => 'Dash\isOdd',
'expected' => null,
],
/*
With indexed array
*/
'With an indexed array with no elements that satisfy the predicate' => [
'iterable' => [2, 4, 6, 8],
'predicate' => 'Dash\isOdd',
'expected' => null,
],
'With an indexed array with one element that satisfies the predicate' => [
'iterable' => [2, 4, 5, 6],
'predicate' => 'Dash\isOdd',
'expected' => [2, 5],
],
'With an indexed array with several elements that satisfy the predicate' => [
'iterable' => [2, 3, 4, 7],
'predicate' => 'Dash\isOdd',
'expected' => [1, 3],
],
'With an indexed array with all elements that satisfy the predicate' => [
'iterable' => [1, 3, 5, 7],
'predicate' => 'Dash\isOdd',
'expected' => [0, 1],
],
'With an indexed array and matchesProperty($field) shorthand' => [
'iterable' => [
['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false],
['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true],
['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false],
['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true],
],
'predicate' => 'active',
'expected' => [1, ['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true]],
],
'With an indexed array and matchesProperty($field, $value) shorthand' => [
'iterable' => [
['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false],
['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true],
['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false],
['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true],
],
'predicate' => ['active', false],
'expected' => [0, ['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false]],
],
/*
With associative array
*/
'With an associative array with no elements that satisfy the predicate' => [
'iterable' => ['a' => 2, 'b' => 4, 'c' => 6, 'd' => 8],
'predicate' => 'Dash\isOdd',
'expected' => null,
],
'With an associative array with one element that satisfies the predicate' => [
'iterable' => ['a' => 2, 'b' => 4, 'c' => 5, 'd' => 6],
'predicate' => 'Dash\isOdd',
'expected' => ['c', 5],
],
'With an associative array with several elements that satisfy the predicate' => [
'iterable' => ['a' => 2, 'b' => 3, 'c' => 4, 'd' => 7],
'predicate' => 'Dash\isOdd',
'expected' => ['b', 3],
],
'With an associative array with all elements that satisfy the predicate' => [
'iterable' => ['a' => 1, 'b' => 3, 'c' => 5, 'd' => 7],
'predicate' => 'Dash\isOdd',
'expected' => ['a', 1],
],
'With an associative array and matchesProperty($field) shorthand' => [
'iterable' => [
'a' => ['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false],
'b' => ['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true],
'c' => ['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false],
'd' => ['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true],
],
'predicate' => 'active',
'expected' => ['b', ['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true]],
],
'With an associative array and matchesProperty($field, $value) shorthand' => [
'iterable' => [
'a' => ['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false],
'b' => ['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true],
'c' => ['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false],
'd' => ['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true],
],
'predicate' => ['active', false],
'expected' => ['a', ['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false]],
],
/*
With stdClass
*/
'With an empty stdClass' => [
'iterable' => (object) [],
'predicate' => 'Dash\isOdd',
'expected' => null,
],
'With an stdClass with no elements that satisfy the predicate' => [
'iterable' => (object) ['a' => 2, 'b' => 4, 'c' => 6],
'predicate' => 'Dash\isOdd',
'expected' => null,
],
'With an stdClass with one element that satisfies the predicate' => [
'iterable' => (object) ['a' => 2, 'b' => 3, 'c' => 6],
'predicate' => 'Dash\isOdd',
'expected' => ['b', 3],
],
'With an stdClass with several elements that satisfy the predicate' => [
'iterable' => (object) ['a' => 2, 'b' => 3, 'c' => 4, 'd' => 7],
'predicate' => 'Dash\isOdd',
'expected' => ['b', 3],
],
'With an stdClass with all elements that satisfy the predicate' => [
'iterable' => (object) ['a' => 1, 'b' => 3, 'c' => 5],
'predicate' => 'Dash\isOdd',
'expected' => ['a', 1],
],
'With an stdClass and matchesProperty($field) shorthand' => [
'iterable' => (object) [
'a' => (object) ['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false],
'b' => (object) ['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true],
'c' => (object) ['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false],
'd' => (object) ['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true],
],
'predicate' => 'active',
'expected' => ['b', (object) ['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true]],
],
'With an stdClass and matchesProperty($field, $value) shorthand' => [
'iterable' => (object) [
'a' => (object) ['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false],
'b' => (object) ['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true],
'c' => (object) ['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false],
'd' => (object) ['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true],
],
'predicate' => ['active', false],
'expected' => ['a', (object) ['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false]],
],
/*
With ArrayObject
*/
'With an empty ArrayObject' => [
'iterable' => new ArrayObject([]),
'predicate' => 'Dash\isOdd',
'expected' => null,
],
'With an ArrayObject with no elements that satisfy the predicate' => [
'iterable' => new ArrayObject(['a' => 2, 'b' => 4, 'c' => 6]),
'predicate' => 'Dash\isOdd',
'expected' => null,
],
'With an ArrayObject with one element that satisfies the predicate' => [
'iterable' => new ArrayObject(['a' => 2, 'b' => 3, 'c' => 6]),
'predicate' => 'Dash\isOdd',
'expected' => ['b', 3]
],
'With an ArrayObject with several elements that satisfy the predicate' => [
'iterable' => new ArrayObject(['a' => 2, 'b' => 3, 'c' => 4, 'd' => 7]),
'predicate' => 'Dash\isOdd',
'expected' => ['b', 3],
],
'With an ArrayObject with all elements that satisfy the predicate' => [
'iterable' => new ArrayObject(['a' => 1, 'b' => 3, 'c' => 5]),
'predicate' => 'Dash\isOdd',
'expected' => ['a', 1],
],
'With an ArrayObject and matchesProperty($field) shorthand' => [
'iterable' => new ArrayObject([
'a' => new ArrayObject(['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false]),
'b' => new ArrayObject(['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true]),
'c' => new ArrayObject(['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false]),
'd' => new ArrayObject(['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true]),
]),
'predicate' => 'active',
'expected' => [
'b',
new ArrayObject(['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true])
],
],
'With an ArrayObject and matchesProperty($field, $value) shorthand' => [
'iterable' => new ArrayObject([
'a' => new ArrayObject(['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false]),
'b' => new ArrayObject(['name' => 'Jane', 'age' => 27, 'gender' => 'female', 'active' => true]),
'c' => new ArrayObject(['name' => 'Kane', 'age' => 33, 'gender' => 'male', 'active' => false]),
'd' => new ArrayObject(['name' => 'Pete', 'age' => 35, 'gender' => 'male', 'active' => true]),
]),
'predicate' => ['active', false],
'expected' => [
'a',
new ArrayObject(['name' => 'John', 'age' => 30, 'gender' => 'male', 'active' => false])
],
],
];
}
public function testPredicateArgs()
{
$iterable = ['a' => 2, 'b' => 4, 'c' => 7];
$iterated = [];
$predicate = function ($value, $key, $passedIterable) use (&$iterated, $iterable) {
$iterated[$key] = $value;
$this->assertSame($iterable, $passedIterable);
return $value % 2 !== 0;
};
$result = Dash\find($iterable, $predicate);
$this->assertSame(['c', 7], $result);
$this->assertSame($iterable, $iterated);
}
public function testShortCircuiting()
{
$iterated = [];
$predicate = function ($value, $key) use (&$iterated) {
$iterated[$key] = $value;
return $value % 2 !== 0;
};
$iterable = ['a' => 2, 'b' => 3, 'c' => 4];
$result = Dash\find($iterable, $predicate);
$this->assertSame(['b', 3], $result);
$this->assertSame(['a' => 2, 'b' => 3], $iterated);
}
/**
* @dataProvider casesDefaultPredicate
*/
public function testDefaultPredicate($iterable, $expected)
{
$this->assertSame($expected, Dash\find($iterable));
}
public function casesDefaultPredicate()
{
return [
'With an empty array' => [
'iterable' => [],
'expected' => null,
],
'With an array of truthy values' => [
'iterable' => ['a', true, 1],
'expected' => [0, 'a'],
],
'With an array of falsey values' => [
'iterable' => [0, false, ''],
'expected' => null,
],
'With an array of values with mixed truthiness' => [
'iterable' => [0, false, true],
'expected' => [2, true],
],
];
}
/**
* @dataProvider casesTypeAssertions
*/
public function testTypeAssertions($iterable, $type)
{
$this->expectException(InvalidArgumentException::class);
try {
Dash\find($iterable);
} catch (Exception $e) {
$this->assertSame(
"Dash\\find 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',
],
];
}
public function testExamples()
{
$this->assertSame(
['b', 2],
Dash\find(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], 'Dash\isEven')
);
$this->assertSame(
['c', 3],
Dash\find(
['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4],
function ($value, $key) {
return $value > 1 && $key !== 'b';
}
)
);
$this->assertSame(
[3, 'a'],
Dash\find([0, null, false, 'a', true])
);
$data = [
['name' => 'John', 'active' => false],
['name' => 'Mary', 'active' => true],
['name' => 'Pete', 'active' => true],
['name' => 'Jane', 'active' => false],
];
$this->assertSame(
[1, ['name' => 'Mary', 'active' => true]],
Dash\find($data, 'active')
);
$this->assertSame(
[0, ['name' => 'John', 'active' => false]],
Dash\find($data, ['active', false])
);
}
}