ArrayFunctionsTest   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 456
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 165
dl 0
loc 456
rs 9.52
c 0
b 0
f 0
wmc 36

31 Methods

Rating   Name   Duplication   Size   Complexity  
A testArrayFindKey() 0 7 2
A testArrayFlatten() 0 28 2
A testArrayContainsAll() 0 4 1
A testArrayContainsAllAssocWithStrict() 0 11 1
A arrayFlattenGlueProvider() 0 32 1
A testArrayContainsAllWithStrict() 0 4 1
A testArrayFindKeyWithKey() 0 7 1
A testArrayWithout() 0 6 1
A testArrayContainsAnyAssocWithNested() 0 15 1
A testArrayContainsAllAssocWithEmpty() 0 3 1
A testArrayContainsAnyWithNested() 0 15 1
A testArrayContainsAnyWithEmpty() 0 3 1
A testArrayOnly() 0 6 1
A testArrayContainsAny() 0 4 1
A testArrayContainsAllAssoc() 0 10 1
A testArrayFind() 0 7 2
A testArrayContainsAnyAssocWithStrict() 0 11 1
A testArrayFindWithKey() 0 7 1
A testArrayFindKeyWithNotFound() 0 7 1
A testArrayContainsAllAssocWithDifferentKeys() 0 5 1
A testArrayFindWithNotFound() 0 7 1
A testArrayContainsAnyAssocWithDifferentKeys() 0 5 1
A testArrayJoinPretty() 0 3 1
A arrayJoinPrettyArrayProvider() 0 8 1
A testArrayContainsAllAssocWithNested() 0 15 1
A testArrayContainsAllWithEmpty() 0 3 1
A testArrayContainsAllWithNested() 0 18 1
A testArrayFindWithBoth() 0 7 2
A testArrayFindKeyWithBoth() 0 7 2
A testArrayContainsAnyWithStrict() 0 4 1
A testArrayContainsAnyAssoc() 0 10 1
1
<?php
2
3
namespace Jasny\Tests;
4
5
use PHPStan\Testing\TestCase;
6
7
use function Jasny\array_only;
8
use function Jasny\array_without;
9
use function Jasny\array_contains_all;
10
use function Jasny\array_contains_all_assoc;
11
use function Jasny\array_contains_any;
12
use function Jasny\array_contains_any_assoc;
13
use function Jasny\array_find;
14
use function Jasny\array_find_key;
15
use function Jasny\array_flatten;
16
use function Jasny\array_join_pretty;
17
18
/**
19
 * Test array functions
20
 * @coversNothing
21
 */
22
class ArrayFunctionsTest extends TestCase
23
{
24
    /**
25
     * @covers Jasny\array_only
26
     */
27
    public function testArrayOnly()
28
    {
29
        $array = ['baz' => null, 'foo' => 1, 'bar' => 20, 'qux' => 99];
30
        $expect = ['foo' => 1, 'bar' => 20];
31
        
32
        $this->assertSame($expect, array_only($array, ['bar', 'foo', 'jazz']));
33
    }
34
    
35
    /**
36
     * @covers Jasny\array_without
37
     */
38
    public function testArrayWithout()
39
    {
40
        $array = ['baz' => null, 'foo' => 1, 'bar' => 20, 'qux' => 99];
41
        $expect = ['baz' => null, 'qux' => 99];
42
        
43
        $this->assertSame($expect, array_without($array, ['bar', 'foo', 'jazz']));
44
    }
45
    
46
    
47
    /**
48
     * @covers Jasny\array_contains_all
49
     */
50
    public function testArrayContainsAll()
51
    {
52
        $this->assertTrue(array_contains_all(['foo', 'bar', 'baz', 'top'], ['top', 'bar']));
53
        $this->assertFalse(array_contains_all(['foo', 'baz', 'top'], ['top', 'bar']));
54
    }
55
56
    /**
57
     * @covers Jasny\array_contains_all
58
     */
59
    public function testArrayContainsAllWithEmpty()
60
    {
61
        $this->assertTrue(array_contains_all(['foo', 'bar', 'baz', 'top'], []));
62
    }
63
64
    /**
65
     * @covers Jasny\array_contains_all
66
     */
67
    public function testArrayContainsAllWithNested()
68
    {
69
        $this->assertTrue(array_contains_all(
70
            [['hello', 'world'], 'q', (object)['a' => 'b']],
71
            ['q', ['hello', 'world']]
72
        ));
73
        $this->assertTrue(array_contains_all(
74
            [['hello', 'world'], 'q', (object)['a' => 'b']],
75
            [(object)['a' => 'b']]
76
        ));
77
        
78
        $this->assertFalse(array_contains_all(
79
            [['hello', 'world'], 'q', (object)['a' => 'b']],
80
            ['q', ['hello']]
81
        ));
82
        $this->assertFalse(array_contains_all(
83
            [['hello', 'world'], 'q', (object)['a' => 'b']],
84
            ['q', ['hello', 'world', '!']]
85
        ));
86
    }
87
    
88
    /**
89
     * @covers Jasny\array_contains_all
90
     */
91
    public function testArrayContainsAllWithStrict()
92
    {
93
        $this->assertTrue(array_contains_all(['foo', 'bar', true], [1, 'bar']));
94
        $this->assertFalse(array_contains_all(['foo', 'bar', true], [1, 'bar'], true));
95
    }
96
    
97
    
98
    /**
99
     * @covers Jasny\array_contains_all_assoc
100
     */
101
    public function testArrayContainsAllAssoc()
102
    {
103
        $this->assertTrue(array_contains_all_assoc(
104
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => 3],
105
            ['top' => 3, 'bar' => 9]
106
        ));
107
        
108
        $this->assertFalse(array_contains_all_assoc(
109
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => 3],
110
            ['top' => 3, 'bar' => 7]
111
        ));
112
    }
113
114
    /**
115
     * @covers Jasny\array_contains_all_assoc
116
     */
117
    public function testArrayContainsAllAssocWithDifferentKeys()
118
    {
119
        $this->assertFalse(array_contains_all_assoc(
120
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => 3],
121
            ['top' => 3, 'wuz' => 7]
122
        ));
123
    }
124
125
    /**
126
     * @covers Jasny\array_contains_all_assoc
127
     */
128
    public function testArrayContainsAllAssocWithEmpty()
129
    {
130
        $this->assertTrue(array_contains_all_assoc(['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => 3], []));
131
    }
132
133
    /**
134
     * @covers Jasny\array_contains_all_assoc
135
     */
136
    public function testArrayContainsAllAssocWithNested()
137
    {
138
        $this->assertTrue(array_contains_all_assoc(
139
            ['greet' => ['hello', 'world'], 'x' => 'q', 'item' => (object)['a' => 'b']],
140
            ['x' => 'q', 'greet' => ['hello', 'world']]
141
        ));
142
        
143
        $this->assertTrue(array_contains_all_assoc(
144
            ['greet' => ['hello', 'world'], 'x' => 'q', 'item' => (object)['a' => 'b']],
145
            ['x' => 'q', 'item' => (object)['a' => 'b']]
146
        ));
147
        
148
        $this->assertFalse(array_contains_all_assoc(
149
            ['greet' => ['hello', 'world'], 'x' => 'q', 'item' => (object)['a' => 'b']],
150
            ['x' => 'q', 'greet' => ['hello', 'world', '!']]
151
        ));
152
    }
153
    
154
    /**
155
     * @covers Jasny\array_contains_all_assoc
156
     */
157
    public function testArrayContainsAllAssocWithStrict()
158
    {
159
        $this->assertTrue(array_contains_all_assoc(
160
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => true],
161
            ['top' => 1, 'bar' => 9]
162
        ));
163
        
164
        $this->assertFalse(array_contains_all_assoc(
165
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => true],
166
            ['top' => 1, 'bar' => 9],
167
            true
168
        ));
169
    }
170
171
172
    /**
173
     * @covers Jasny\array_contains_any
174
     */
175
    public function testArrayContainsAny()
176
    {
177
        $this->assertTrue(array_contains_any(['foo', 'baz', 'top'], ['top', 'bar']));
178
        $this->assertFalse(array_contains_any(['foo', 'baz'], ['top', 'bar']));
179
    }
180
181
    /**
182
     * @covers Jasny\array_contains_any
183
     */
184
    public function testArrayContainsAnyWithEmpty()
185
    {
186
        $this->assertFalse(array_contains_any(['foo', 'baz', 'top'], []));
187
    }
188
189
    /**
190
     * @covers Jasny\array_contains_any
191
     */
192
    public function testArrayContainsAnyWithNested()
193
    {
194
        $this->assertTrue(array_contains_any(
195
            [['hello', 'world'], 'q', (object)['a' => 'b']],
196
            ['x', ['hello', 'world']]
197
        ));
198
        $this->assertTrue(array_contains_any(
199
            [['hello', 'world'], 'q', (object)['a' => 'b']],
200
            [(object)['a' => 'b']]
201
        ));
202
203
        $this->assertFalse(array_contains_any([['hello', 'world'], 'q', (object)['a' => 'b']], ['x', ['hello']]));
204
        $this->assertFalse(array_contains_any(
205
            [['hello', 'world'], 'q', (object)['a' => 'b']],
206
            ['x', ['hello', 'world', '!']]
207
        ));
208
    }
209
210
    /**
211
     * @covers Jasny\array_contains_any
212
     */
213
    public function testArrayContainsAnyWithStrict()
214
    {
215
        $this->assertTrue(array_contains_any(['foo', true], [1, 'bar']));
216
        $this->assertFalse(array_contains_any(['foo', true], [1, 'bar'], true));
217
    }
218
219
220
    /**
221
     * @covers Jasny\array_contains_any_assoc
222
     */
223
    public function testArrayContainsAnyAssoc()
224
    {
225
        $this->assertTrue(array_contains_any_assoc(
226
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => 3],
227
            ['taf' => 3, 'bar' => 9]
228
        ));
229
230
        $this->assertFalse(array_contains_any_assoc(
231
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => 3],
232
            ['taf' => 3, 'bar' => 7]
233
        ));
234
    }
235
236
    /**
237
     * @covers Jasny\array_contains_any_assoc
238
     */
239
    public function testArrayContainsAnyAssocWithDifferentKeys()
240
    {
241
        $this->assertFalse(array_contains_any_assoc(
242
            ['foo' => 7, 'bar' => 9, 'baz' => 12, 'top' => 3],
243
            ['taf' => 3, 'wuz' => 7]
244
        ));
245
    }
246
247
    /**
248
     * @covers Jasny\array_contains_any_assoc
249
     */
250
    public function testArrayContainsAnyAssocWithNested()
251
    {
252
        $this->assertTrue(array_contains_any_assoc(
253
            ['greet' => ['hello', 'world'], 'x' => 'q', 'item' => (object)['a' => 'b']],
254
            ['x' => 'x', 'greet' => ['hello', 'world']]
255
        ));
256
257
        $this->assertTrue(array_contains_any_assoc(
258
            ['greet' => ['hello', 'world'], 'x' => 'q', 'item' => (object)['a' => 'b']],
259
            ['x' => 'x', 'item' => (object)['a' => 'b']]
260
        ));
261
262
        $this->assertFalse(array_contains_any_assoc(
263
            ['greet' => ['hello', 'world'], 'x' => 'q', 'item' => (object)['a' => 'b']],
264
            ['x' => 'x', 'greet' => ['hello', 'world', '!']]
265
        ));
266
    }
267
268
    /**
269
     * @covers Jasny\array_contains_any_assoc
270
     */
271
    public function testArrayContainsAnyAssocWithStrict()
272
    {
273
        $this->assertTrue(array_contains_any_assoc(
274
            ['foo' => 7, 'baz' => 12, 'top' => true],
275
            ['top' => 1, 'bar' => 9]
276
        ));
277
278
        $this->assertFalse(array_contains_any_assoc(
279
            ['foo' => 7, 'baz' => 12, 'top' => true],
280
            ['top' => 1, 'bar' => 9],
281
            true
282
        ));
283
    }
284
285
286
    /**
287
     * @covers Jasny\array_find
288
     */
289
    public function testArrayFind()
290
    {
291
        $value = array_find(['foo' => 8, 'wuz' => 42, 'bar' => 99, 'qux' => 111], function($item) {
292
            return $item > 10 && $item < 100;
293
        });
294
295
        $this->assertEquals(42, $value);
296
    }
297
298
    /**
299
     * @covers Jasny\array_find
300
     */
301
    public function testArrayFindWithKey()
302
    {
303
        $value = array_find(['foo' => 8, 'wuz' => 42, 'bar' => 99, 'qux' => 111], function($key) {
304
            return strpos($key, 'u') !== false;
305
        }, ARRAY_FILTER_USE_KEY);
306
307
        $this->assertEquals(42, $value);
308
    }
309
310
    /**
311
     * @covers Jasny\array_find
312
     */
313
    public function testArrayFindWithBoth()
314
    {
315
        $value = array_find(['foo' => 8, 'bar' => 99, 'wuz' => 42, 'qux' => 111], function($item, $key) {
316
            return strpos($key, 'u') !== false && $item > 10;
317
        }, ARRAY_FILTER_USE_BOTH);
318
319
        $this->assertEquals(42, $value);
320
    }
321
322
    /**
323
     * @covers Jasny\array_find
324
     */
325
    public function testArrayFindWithNotFound()
326
    {
327
        $value = array_find(['foo' => 8, 'wuz' => 42, 'bar' => 99, 'qux' => 111], function($item) {
328
            return $item > 100000;
329
        });
330
331
        $this->assertFalse($value);
332
    }
333
334
335
    /**
336
     * @covers Jasny\array_find_key
337
     */
338
    public function testArrayFindKey()
339
    {
340
        $key = array_find_key(['foo' => 8, 'wuz' => 42, 'bar' => 99, 'qux' => 111], function($item) {
341
            return $item > 10 && $item < 100;
342
        });
343
344
        $this->assertEquals('wuz', $key);
345
    }
346
347
    /**
348
     * @covers Jasny\array_find_key
349
     */
350
    public function testArrayFindKeyWithKey()
351
    {
352
        $key = array_find_key(['foo' => 8, 'wuz' => 42, 'bar' => 99, 'qux' => 111], function($key) {
353
            return strpos($key, 'u') !== false;
354
        }, ARRAY_FILTER_USE_KEY);
355
356
        $this->assertEquals('wuz', $key);
357
    }
358
359
    /**
360
     * @covers Jasny\array_find_key
361
     */
362
    public function testArrayFindKeyWithBoth()
363
    {
364
        $key = array_find_key(['foo' => 8, 'bar' => 99, 'wuz' => 42, 'qux' => 111], function($item, $key) {
365
            return strpos($key, 'u') !== false && $item > 10;
366
        }, ARRAY_FILTER_USE_BOTH);
367
368
        $this->assertEquals('wuz', $key);
369
    }
370
371
    /**
372
     * @covers Jasny\array_find_key
373
     */
374
    public function testArrayFindKeyWithNotFound()
375
    {
376
        $key = array_find_key(['foo' => 8, 'wuz' => 42, 'bar' => 99, 'qux' => 111], function($item) {
377
            return $item > 100000;
378
        });
379
380
        $this->assertFalse($key);
381
    }
382
383
384
    public function arrayFlattenGlueProvider()
385
    {
386
        $expectDot = [
387
            'animal.mammel' => [
388
                'ape',
389
                'bear'
390
            ],
391
            'animal.reptile' => 'chameleon',
392
            'colors.red' => 60,
393
            'colors.green' => 100,
394
            'colors.blue' => 0,
395
            'topic' => 'green',
396
            'a.b.c.d' => 42
397
        ];
398
399
        $expectDash = [
400
            'animal-mammel' => [
401
                'ape',
402
                'bear'
403
            ],
404
            'animal-reptile' => 'chameleon',
405
            'colors-red' => 60,
406
            'colors-green' => 100,
407
            'colors-blue' => 0,
408
            'topic' => 'green',
409
            'a-b-c-d' => 42
410
        ];
411
        
412
        return [
413
            [null, $expectDot],
414
            ['.', $expectDot],
415
            ['-', $expectDash]
416
        ];
417
    }
418
    
419
    /**
420
     * @covers Jasny\array_flatten
421
     * @dataProvider arrayFlattenGlueProvider
422
     * 
423
     * @param string $glue
424
     * @param array  $expect
425
     */
426
    public function testArrayFlatten($glue, array $expect)
427
    {
428
        $values = [
429
            'animal' => [
430
                'mammel' => [
431
                    'ape',
432
                    'bear'
433
                ],
434
                'reptile' => 'chameleon'
435
            ],
436
            'colors' => [
437
                'red' => 60,
438
                'green' => 100,
439
                'blue' => 0
440
            ],
441
            'topic' => 'green',
442
            'a' => [
443
                'b' => [
444
                    'c' => [
445
                        'd' => 42
446
                    ]
447
                ]
448
            ]
449
        ];
450
451
        $flattened = isset($glue) ? array_flatten($values, $glue) : array_flatten($values);
452
        
453
        $this->assertEquals($expect, $flattened);
454
    }
455
456
457
    public function arrayJoinPrettyArrayProvider()
458
    {
459
        return [
460
            [[], ''],
461
            [['foo'], 'foo'],
462
            [['foo', 'bar'], 'foo&bar'],
463
            [['foo', 'bar', 'zoo'], 'foo,bar&zoo'],
464
            [[11, 22, 33, 44], '11,22,33&44']
465
        ];
466
    }
467
    
468
    /**
469
     * @covers Jasny\array_join_pretty
470
     * @dataProvider arrayJoinPrettyArrayProvider
471
     * 
472
     * @param array  $array
473
     * @param string $expect
474
     */
475
    public function testArrayJoinPretty($array, $expect)
476
    {
477
        $this->assertEquals($expect, array_join_pretty(',', '&', $array));
478
    }
479
}
480