Passed
Push — master ( 15bdaa...c2dfbc )
by Saulius
10:02
created

ArrayTest::getVariousArraysForGetArrayValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper;
14
15
use PHPUnit\Framework\TestCase;
16
use Sauls\Component\Helper\Stubs\DummyObject;
17
use Sauls\Component\Helper\Stubs\StringObject;
18
19
class ArrayTest extends TestCase
20
{
21
    /**
22
     * @test
23
     * @dataProvider getArraysToMergeData
24
     */
25
    public function should_merge_two_given_arrays_into_one(array $expectedArray, array $array1, array $array2)
26
    {
27
        $this->assertSame($expectedArray, array_merge($array1, $array2));
28
    }
29
30
    public function getArraysToMergeData(): array
31
    {
32
        return [
33
            [
34
                [],
35
                [],
36
                [],
37
            ],
38
            [
39
                ['test', 'me', 'me', 'test'],
40
                ['test', 'me'],
41
                ['me', 'test'],
42
            ],
43
            [
44
                ['test', 'test'],
45
                [0 => 'test'],
46
                ['0' => 'test'],
47
            ],
48
            [
49
                ['test', 'test2'],
50
                [0 => 'test'],
51
                [0 => 'test2'],
52
            ],
53
            [
54
                ['test1' => ['me', 'other']],
55
                ['test1' => ['me']],
56
                ['test1' => ['other']],
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * @test
63
     * @dataProvider getVariousArraysForGetArrayValue()
64
     *
65
     * @param mixed      $expected
66
     * @param mixed      $array
67
     * @param mixed      $key
68
     * @param mixed|null $default
69
     */
70
    public function should_get_array_value_by_given_values($expected, $array, $key, $default = null)
71
    {
72
        $this->assertSame($expected, array_get_value($array, $key, $default));
73
    }
74
75
    /**
76
     * @throws Exception\PropertyNotAccessibleException
77
     */
78
    public function getVariousArraysForGetArrayValue(): array
79
    {
80
        return [
81
            ['test', ['test'], 0],
82
            [null, 'Hello World!', 11],
83
            [null, 89, 11],
84
            ['yep', ['test' => ['me' => ['nested' => 'yep']]], 'test.me.nested'],
85
            ['yep', ['test' => ['me' => ['nested' => 'yep']]], ['test', 'me', 'nested']],
86
            [
87
                'It works!',
88
                ['a' => 'c', 'g' => configure_object(new \stdClass, ['apache' => 'It works!'])],
89
                'g.apache',
90
            ],
91
            [
92
                31,
93
                [
94
                    'a' => 20,
95
                    'c' => 11,
96
                ],
97
                function ($array, $default) {
98
                    return $array['a'] + $array['c'];
99
                },
100
            ],
101
            [null, ['what?'], 25],
102
            ['no value', ['super', 'duper'], 'no', 'no value'],
103
        ];
104
    }
105
106
107
    /**
108
     * @test
109
     * @dataProvider getArraysForArrayAssign
110
     *
111
     * @param mixed $expected
112
     * @param mixed $array
113
     * @param mixed $key
114
     * @param mixed $value
115
     * @param mixed $getKeyName
116
     * @param mixed $getKeyValueDefault
117
     */
118
    public function should_assign_array_value_by_given_values(
119
        $expected,
120
        $array,
121
        $key,
122
        $value,
123
        $getKeyName,
124
        $getKeyValueDefault = null
125
    ) {
126
        array_set_value($array, $key, $value);
127
128
        $this->assertSame($expected, array_get_value($array, $getKeyName, $getKeyValueDefault));
129
    }
130
131
    public function getArraysForArrayAssign(): array
132
    {
133
        return [
134
            [null, [], null, [], 'blabla', null],
135
            [
136
                'yes!',
137
                ['a' => 'no', 'b' => 'maybe'],
138
                null,
139
                [
140
                    'g' => 'yes!',
141
                ],
142
                'g',
143
            ],
144
        ];
145
    }
146
147
    /**
148
     * @test
149
     * @dataProvider getVariousArraysForSetArrayValue
150
     *
151
     * @param mixed $expected
152
     * @param mixed $array
153
     * @param mixed $key
154
     * @param mixed $value
155
     */
156
    public function should_set_array_value_by_given_values($expected, $array, $key, $value)
157
    {
158
        array_set_value($array, $key, $value);
159
160
        $this->assertSame($expected, array_get_value($array, $key));
161
    }
162
163
    public function getVariousArraysForSetArrayValue(): array
164
    {
165
        return [
166
            ['Hello', ['a' => 11, 'b' => 9], 'a', 'Hello'],
167
            [25, ['a' => 11, 'b' => 9], 'c', 25],
168
            [
169
                'Sandbox mode',
170
                [
171
                    'a' => 11,
172
                ],
173
                'b.v.g',
174
                'Sandbox mode',
175
            ],
176
            [
177
                'World of blocks',
178
                [
179
                    'World' => 'Hi!',
180
                    'deep' => [
181
                        'not so deep',
182
                    ],
183
                ],
184
                ['deep', 'below', 'block'],
185
                'World of blocks',
186
            ],
187
            [
188
                ['a', 'b', 'c'],
189
                [
190
                    'numbers' => [1, 2, 4],
191
                    'letters' => ['x', 'v', 'z'],
192
                    'more' => 'f',
193
                ],
194
                'more.letters',
195
                ['a', 'b', 'c'],
196
            ],
197
        ];
198
    }
199
200
    /**
201
     * @test
202
     */
203
    public function should_merge_two_large_arrays()
204
    {
205
        $array1 = [
206
            'a' => 1,
207
            'b' => [
208
                'c' => 'hello',
209
            ],
210
            'c' => 'g',
211
            'v' => [
212
                'u' => [
213
                    'd' => 'f',
214
                ],
215
            ],
216
            12 => 53,
217
        ];
218
        $array2 = [
219
            'a' => 25,
220
            'b' => [
221
                'c' => 'world',
222
            ],
223
            11 => 'Yupi!',
224
            'v' => [
225
                'u' => [
226
                    't' => 23,
227
                ],
228
            ],
229
            'c' => 111,
230
            12 => 49,
231
        ];
232
233
        $this->assertSame([
234
            'a' => 25,
235
            'b' => [
236
                'c' => 'world',
237
            ],
238
            'c' => 111,
239
            'v' => [
240
                'u' => [
241
                    'd' => 'f',
242
                    't' => 23,
243
                ],
244
            ],
245
            12 => 53,
246
            11 => 'Yupi!',
247
            13 => 49,
248
        ], array_merge($array1, $array2));
249
    }
250
251
    /**
252
     * @test
253
     * @dataProvider getArrayRemoveKeyData
254
     *
255
     * @param mixed      $expected
256
     * @param mixed      $key
257
     * @param null|mixed $default
258
     */
259
    public function should_remove_array_element_by_given_key(
260
        $expected,
261
        array $array,
262
        $key,
263
        $default = null,
264
        array $expectedArray = []
265
    ) {
266
        $this->assertSame($expected, array_remove_key($array, $key, $default));
267
268
        if (!empty($expectedArray)) {
269
            $this->assertEquals($expectedArray, $array);
270
        }
271
    }
272
273
    public function getArrayRemoveKeyData(): array
274
    {
275
        return [
276
            [null, [], 'test'],
277
            [5, [], 'age', 5],
278
            [83, [11, 22, 83], 2],
279
            [22, [11, 22, 83], 1, null, [0 => 11, 2 => 83]],
280
            [
281
                'yiw!',
282
                [
283
                    'a' => [
284
                        'b' => 'yiw!',
285
                    ],
286
                ],
287
                'a.b',
288
                null,
289
                [
290
                    'a' => [],
291
                ],
292
            ],
293
            [
294
                'is it working?',
295
                [
296
                    'a' => 'Hello',
297
                    'b' => [1, 2],
298
                    'c' => [
299
                        'g' => [
300
                            'y' => 'function',
301
                            'v' => 'is it working?',
302
                            'x' => 'factor',
303
                        ],
304
                    ],
305
                    'd' => [],
306
                ],
307
                'c.g.v',
308
                null,
309
                [
310
                    'a' => 'Hello',
311
                    'b' => [1, 2],
312
                    'c' => [
313
                        'g' => [
314
                            'y' => 'function',
315
                            'x' => 'factor',
316
                        ],
317
                    ],
318
                    'd' => [],
319
                ],
320
            ],
321
            [
322
                'x',
323
                [
324
                    'a' => [
325
                        'b' => [
326
                            'c' => 'x',
327
                        ],
328
                    ],
329
                ],
330
                ['a', 'b', 'c'],
331
                null,
332
                [
333
                    'a' => [
334
                        'b' => [],
335
                    ],
336
                ],
337
            ],
338
            [
339
                null,
340
                [
341
                    'c' => [
342
                        'x' => [
343
                            'v' => 'ttt',
344
                            'b' => 11,
345
                        ],
346
                    ],
347
                ],
348
                'c.x.a',
349
            ],
350
        ];
351
    }
352
353
    /**
354
     * @test
355
     * @dataProvider getArrayRemoveValueData
356
     *
357
     * @param mixed $expected
358
     * @param mixed $value
359
     */
360
    public function should_remove_array_element_by_given_value(
361
        $expected,
362
        array $array,
363
        $value,
364
        array $expectedArray = []
365
    ) {
366
        $this->assertSame($expected, array_remove_value($array, $value));
367
368
        if (!empty($expectedArray)) {
369
            $this->assertEquals($expectedArray, $array);
370
        }
371
    }
372
373
    public function getArrayRemoveValueData(): array
374
    {
375
        return [
376
            [[], [], 'value', []],
377
            [
378
                ['x' => 11],
379
                [
380
                    'y' => 25,
381
                    'x' => 11,
382
                ],
383
                11,
384
                [
385
                    'y' => 25,
386
                ],
387
            ],
388
            [
389
                [
390
                    'marry' => 'carson',
391
                    'john' => 'carson',
392
                ],
393
                [
394
                    'johan' => 'doe',
395
                    'marry' => 'carson',
396
                    'tim' => 'west',
397
                    'andy' => 'gear',
398
                    'john' => 'carson',
399
                ],
400
                'carson',
401
                [
402
                    'johan' => 'doe',
403
                    'tim' => 'west',
404
                    'andy' => 'gear',
405
                ],
406
            ],
407
        ];
408
    }
409
410
    /**
411
     * @test
412
     * @dataProvider getArrayKeyExistsData()
413
     *
414
     * @param mixed $expected
415
     * @param mixed $key
416
     */
417
    public function should_check_if_given_key_exists_in_array($expected, $array, $key, bool $caseSensitive = true)
418
    {
419
        $this->assertSame($expected, array_key_exists($array, $key, $caseSensitive));
420
    }
421
422
    public function getArrayKeyExistsData(): array
423
    {
424
        return [
425
            [false, [], 'x'],
426
            [
427
                true,
428
                [
429
                    'x' => 11,
430
                    'y' => 12,
431
                ],
432
                'y'
433
            ],
434
            [
435
                false,
436
                [
437
                    'x' => 11,
438
                    'y' => 12,
439
                ],
440
                'Y',
441
            ],
442
            [
443
                true,
444
                [
445
                    'x' => 11,
446
                    'Y' => 12,
447
                ],
448
                'Y',
449
                false
450
            ],
451
            [
452
                true,
453
                [
454
                    'a' => [
455
                        'b' => 'value'
456
                    ],
457
                    'c' => 44,
458
                ],
459
                'a.b'
460
            ],
461
            [
462
                false,
463
                [
464
                    'a' => [
465
                        'b' => 'value'
466
                    ],
467
                    'c' => 44,
468
                ],
469
                'a.B'
470
            ],
471
            [
472
                true,
473
                [
474
                    'a' => [
475
                        'B' => 'value'
476
                    ],
477
                    'c' => 44,
478
                ],
479
                'a.B',
480
                false
481
            ],
482
        ];
483
    }
484
485
    /**
486
     * @test
487
     * @expectedException \TypeError
488
     */
489
    public function should_throw_error_when_not_array_passed_to_array_key_exists()
490
    {
491
        array_key_exists('I am a string.', 'value');
492
    }
493
494
    /**
495
     * @test
496
     * @dataProvider getArrayDeepSearchData
497
     *
498
     */
499
    public function should_return_array_deep_search_array_with_path_arrays_to_value($expected, array $array, $value)
500
    {
501
        $this->assertSame($expected, array_deep_search($array, $value));
502
    }
503
504
    /**
505
     * @return array
506
     */
507
    public function getArrayDeepSearchData(): array
508
    {
509
        return [
510
            [[], [], ''],
511
            [
512
                [],
513
                [
514
                    'a' => 'b',
515
                    'c' => 'd',
516
                    'g' => [
517
                        'v' => 'm',
518
                    ]
519
                ],
520
                'o'
521
            ],
522
            [
523
                [
524
                    0 => [
525
                        0 => 'c'
526
                    ],
527
                ],
528
                [
529
                    'a' => 'b',
530
                    'c' => 'd',
531
                    'g' => [
532
                        'v' => 'm',
533
                    ]
534
                ],
535
                'd'
536
            ],
537
            [
538
                [
539
                    0 => [
540
                        0 => 'g',
541
                        1 => 'v',
542
                    ]
543
                ],
544
                [
545
                    'a' => 'b',
546
                    'c' => 'd',
547
                    'g' => [
548
                        'v' => 'm',
549
                    ]
550
                ],
551
                'm'
552
            ],
553
            [
554
                [
555
                    0 => [
556
                        0 => 'j',
557
                        1 => 'f',
558
                        2 => 'cd',
559
                    ],
560
                    1 => [
561
                        0 => 'g',
562
                        1 => 'v',
563
                    ]
564
                ],
565
                [
566
                    'a' => 'b',
567
                    'j' => [
568
                        'f' => [
569
                            'cd' => 'm',
570
                        ],
571
                    ],
572
                    'c' => 'd',
573
                    'g' => [
574
                        'v' => 'm',
575
                    ]
576
                ],
577
                'm'
578
            ],
579
        ];
580
    }
581
582
    /**
583
     * @test
584
     * @dataProvider getArrayDeppSearchValueData
585
     *
586
     * @param mixed $key
587
     * @param mixed $value
588
     * @param mixed $searchValue
589
     */
590
    public function should_return_array_deep_search_value_path_array(array $expected, $key, $value, $searchValue)
591
    {
592
        $this->assertSame($expected, array_deep_search_value($key, $value, $searchValue));
593
    }
594
595
    public function getArrayDeppSearchValueData(): array
596
    {
597
        return [
598
            [
599
                [], 'test', [], 'test',
600
            ],
601
            [
602
                [0 => 'test'],
603
                'test',
604
                [
605
                    'x' => 1,
606
                    'y' => 2,
607
                    'test' => 11,
608
                ],
609
                11,
610
            ],
611
            [
612
                [
613
                    0 => 'test',
614
                    1 => 'p',
615
                    2 => 'a',
616
                ],
617
                'test',
618
                [
619
                    'y' => 'u',
620
                    'test' => [
621
                        'o' => 11,
622
                        'p' => [
623
                            'a' => 23
624
                        ],
625
                    ],
626
                ],
627
                23
628
            ]
629
        ];
630
    }
631
632
    /**
633
     * @test
634
     * @dataProvider getArrayFlattenData
635
     */
636
    public function should_flatten_given_array($expected, $array)
637
    {
638
        $this->assertSame($expected, array_flatten($array));
639
    }
640
641
    public function getArrayFlattenData(): array
642
    {
643
        return [
644
            [
645
                [], [],
646
            ],
647
            [
648
                [
649
                    0 => 11,
650
                    1 => 23,
651
                ],
652
                [
653
                    'a' => 11,
654
                    'b' => 23,
655
                ],
656
            ],
657
            [
658
                [
659
                    0 => 11,
660
                    1 => 28,
661
                    2 => 13,
662
                ],
663
                [
664
                    'a' => 11,
665
                    'b' => [
666
                        'c' => 28,
667
                        'a' => 13,
668
                    ],
669
                ],
670
            ],
671
            [
672
                [
673
                    0 => 15,
674
                    1 => 'hello',
675
                    2 => 'world',
676
                    3 => 'life',
677
                    4 => 'string object',
678
                    5 => 25,
679
                    6 => 98,
680
                ],
681
                [
682
                    'a' => 15,
683
                    'b' => [
684
                        'c' => 'hello',
685
                        'g' => [
686
                            'world', 'life'
687
                        ],
688
                        'h' => new StringObject(),
689
                    ],
690
                    'v' => 'life',
691
                    'm' => [
692
                        15, 25, 98
693
                    ],
694
                ],
695
            ]
696
        ];
697
    }
698
699
    /**
700
     * @test
701
     * @expectedException \RuntimeException
702
     */
703
    public function should_throw_exception_when_object_cannot_be_converted_to_string()
704
    {
705
        array_flatten([
706
           'a' => 11,
707
           'b' => new DummyObject(),
708
        ]);
709
    }
710
711
    /**
712
     * @test
713
     */
714
    public function should_check_if_multiple_array_keys_exists()
715
    {
716
        $array = [
717
            'k1' => 11,
718
            'k2' => 11,
719
            'k3' => 11,
720
            'x' => [
721
                'break' => [
722
                    'deeper' => 'secret project',
723
                ],
724
            ],
725
            'k4' => 11,
726
            'k5' => 11,
727
        ];
728
729
        $this->assertFalse(array_multiple_keys_exists([], ['theKey']));
730
        $this->assertFalse(array_multiple_keys_exists([], []));
731
        $this->assertFalse(array_multiple_keys_exists($array, ['one']));
732
        $this->assertTrue(array_multiple_keys_exists($array, ['k1']));
733
        $this->assertTrue(array_multiple_keys_exists($array, ['k1', 'k2']));
734
        $this->assertTrue(array_multiple_keys_exists($array, ['k1', 'x.break.deeper']));
735
    }
736
}
737