ArrayUtilsTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 139
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A listProvider() 0 26 1
A testIsSequential() 0 3 1
A testMerge() 0 3 1
A arrayProvider() 0 84 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Util;
5
6
use PHPUnit\Framework\TestCase;
7
8
final class ArrayUtilsTest extends TestCase
9
{
10
    /**
11
     * @param array $data
12
     * @param bool $expected
13
     *
14
     * @dataProvider listProvider
15
     */
16
    public function testIsSequential(array $data, bool $expected): void
17
    {
18
        $this->assertSame($expected, ArrayUtils::isSequential($data));
19
    }
20
21
    public function listProvider(): array
22
    {
23
        return [
24
            [
25
                [1, 2, 3],
26
                true
27
            ],
28
            [
29
                ['a', 'b', 'c'],
30
                true
31
            ],
32
            [
33
                ['a', 'b', 'c', null, 'd'],
34
                true
35
            ],
36
            [
37
                ['a' => 'b'],
38
                false
39
            ],
40
            [
41
                [1 => 'a', 2 => 'b'],
42
                false
43
            ],
44
            [
45
                [1 => 'a', 2 => 'b', 0 => 'd'],
46
                true
47
            ],
48
        ];
49
    }
50
51
    /**
52
     * @param array $firstData
53
     * @param array $secondData
54
     * @param array $merged
55
     *
56
     * @dataProvider arrayProvider
57
     */
58
    public function testMerge(array $firstData, array $secondData, array $merged): void
59
    {
60
        $this->assertSame($merged, ArrayUtils::merge($firstData, $secondData));
61
    }
62
63
    public function arrayProvider(): array
64
    {
65
        return [
66
            // test equal lists (result is unique)
67
            [
68
                [1, 2],
69
                [1, 2],
70
                [1, 2],
71
            ],
72
            // test associated lists
73
            [
74
                [1, 2],
75
                [1 => 3],
76
                [1, 3],
77
            ],
78
            // test combined
79
            [
80
                ['key' => [1, 2]],
81
                ['key' => [2, 3, 4]],
82
                ['key' => [1, 2, 3, 4]], // result is unique
83
            ],
84
            // test mixed
85
            [
86
                ['a' => 'z', 'bar' => 'baz'],
87
                [1, 2],
88
                ['a' => 'z', 'bar' => 'baz', 1, 2],
89
            ],
90
            // test mixed 2
91
            [
92
                [1 => 2, 3 => 4],
93
                [1, 2],
94
                [1 => 2, 3 => 4, 1],
95
            ],
96
            // test lists 2
97
            [
98
                [1, 3, 5],
99
                [1, 2, 4, 6],
100
                [1, 3, 5, 2, 4, 6],
101
            ],
102
            // test deep structured
103
            [
104
                [
105
                    1 => [
106
                        2 => [
107
                            3 => 4
108
                        ],
109
                    ],
110
                ],
111
                [
112
                    1 => [
113
                        2 => [
114
                            3
115
                        ],
116
                    ],
117
                    5
118
                ],
119
                [
120
                    1 => [
121
                        2 => [
122
                            3 => 4,
123
                            3
124
                        ],
125
                    ],
126
                    5
127
                ],
128
            ],
129
            // test 2 lists
130
            [
131
                [
132
                    [
133
                        'name' => 'A',
134
                    ],
135
                ],
136
                [
137
                    [
138
                        'name' => 'B',
139
                    ],
140
                ],
141
                [
142
                    [
143
                        'name' => 'A',
144
                    ],
145
                    [
146
                        'name' => 'B',
147
                    ],
148
                ],
149
            ],
150
        ];
151
    }
152
}
153