ArraysTest::testRandom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Framework\Tests\Data;
4
5
use alphaz\Data\Arrays;
6
use PHPUnit\Framework\TestCase;
7
8
class ArraysTest extends TestCase
9
{
10
    public function testIsReallyArray()
11
    {
12
        $this->assertTrue(Arrays::isReallyArray([0, 1]));
13
        $this->assertTrue(Arrays::isReallyArray(['name' => 'someName', 'username' => '@username']));
14
15
        $this->assertFalse(Arrays::isReallyArray([]));
16
        $this->assertFalse(Arrays::isReallyArray(null));
17
        $this->assertFalse(Arrays::isReallyArray('abc'));
18
    }
19
20
    public function testIsSequential()
21
    {
22
        $this->assertFalse(Arrays::isSequential(['name' => 'Alex']));
23
        $this->assertTrue(Arrays::isSequential(['a', 'b']));
24
    }
25
26
    public function testIsAssoc()
27
    {
28
        $this->assertTrue(Arrays::isAssoc(['name' => 'Alex']));
29
        $this->assertFalse(Arrays::isAssoc(['a', 'b']));
30
    }
31
32
    public function testIsMulti()
33
    {
34
        $this->assertTrue(Arrays::isMulti(['members' => [
35
            'user1' => [
36
                'name' => 'Alex',
37
            ],
38
        ]]));
39
        $this->assertFalse(Arrays::isMulti(['name' => 'Alex']));
40
    }
41
42
    public function testGetType()
43
    {
44
        $this->assertSame('indexes', Arrays::getType([1, 2, 3]));
45
        $this->assertSame('assoc', Arrays::getType([
46
            'a' => 1,
47
            'b' => 2,
48
            'c' => 3,
49
        ]));
50
        $this->assertSame('multi', Arrays::getType([
51
            'a' => [
52
                'A' => 1,
53
                'B' => 2,
54
            ],
55
            'b' => [
56
                'A' => 1,
57
                'B' => 2,
58
            ],
59
        ]));
60
        $this->assertNotSame('assoc', Arrays::getType([1, 2, 3]));
61
        $this->assertNotSame('multi', Arrays::getType([
62
            'a' => 1,
63
            'b' => 2,
64
            'c' => 3,
65
        ]));
66
        $this->assertNotSame('indexes', Arrays::getType([
67
            'a' => [
68
                'A' => 1,
69
                'B' => 2,
70
            ],
71
            'b' => [
72
                'A' => 1,
73
                'B' => 2,
74
            ],
75
        ]));
76
    }
77
78
    public function testAdd()
79
    {
80
        $array = Arrays::add(['id' => 1001, 'name' => 'Alex'], 'username', 'alex01', null);
81
        $this->assertEquals(['id' => 1001, 'name' => 'Alex', 'username' => 'alex01'], $array);
82
    }
83
84
    public function testMultiToAssoc()
85
    {
86
        $arrays = ['members' => [
87
            'user' => [
88
                'name' => 'Alex',
89
            ],
90
        ],
91
        ];
92
        $assoc = Arrays::multiToAssoc($arrays);
93
        $this->assertEquals(['name' => 'Alex'], $assoc);
94
    }
95
96
    public function testDot()
97
    {
98
        $array = Arrays::dot(['foo' => ['bar' => 'baz']]);
99
        $this->assertEquals(['foo.bar' => 'baz'], $array);
100
        $array = Arrays::dot(['name' => 'Alex', 'languages' => ['cpp' => true]]);
101
        $this->assertEquals($array, ['name' => 'Alex', 'languages.cpp' => true]);
102
    }
103
104
    public function testDotWithAssocArray()
105
    {
106
        $array = Arrays::dot(['foo' => ['bar' => ['one', 'three', 'two']]], true);
107
        $this->assertEquals(['foo.bar' => ['one', 'three', 'two']], $array);
108
    }
109
110
    public function testMultiToAssocWithSpecificOpr()
111
    {
112
        $opr = '@';
113
        $array = Arrays::multiToAssocWithSpecificOpr(['foo' => ['bar' => 'baz']], $opr);
114
        $this->assertEquals(['foo@bar' => 'baz'], $array);
115
        $array = Arrays::multiToAssocWithSpecificOpr(['name' => 'Alex', 'languages' => ['cpp' => true]], $opr);
116
        $this->assertEquals($array, ['name' => 'Alex', 'languages@cpp' => true]);
117
    }
118
119
    public function testMultiToAssocWithSpecificOprWithAssocArray()
120
    {
121
        $opr = '@';
122
        $array = Arrays::multiToAssocWithSpecificOpr(['foo' => ['bar' => ['one', 'three', 'two']]], $opr, true);
123
        $this->assertEquals(['foo@bar' => ['one', 'three', 'two']], $array);
124
    }
125
126
    public function testExcept()
127
    {
128
        $array = ['name' => 'Alex', 'age' => 18];
129
        $this->assertEquals(['age' => 18], Arrays::except($array, ['name']));
130
    }
131
132
    public function testAppend()
133
    {
134
        $array = ['name' => 'Alex'];
135
        $this->assertEquals(['name' => 'Alex', 'age' => 18], Arrays::append($array, 18, 'age'));
136
    }
137
138
    public function testPrepend()
139
    {
140
        $array = ['name' => 'Alex'];
141
        $this->assertEquals(['age' => 18, 'name' => 'Alex'], Arrays::prepend($array, 18, 'age'));
142
    }
143
144
    public function testUnique()
145
    {
146
        $array = ['name' => 'bob', 'name' => 'Alex'];
147
        $this->assertEquals(['name' => 'Alex'], Arrays::unique($array));
148
    }
149
150
    public function testGet()
151
    {
152
        $array = ['member.user' => ['name' => 'Alex']];
153
        $this->assertEquals(['name' => 'Alex'], Arrays::get($array, 'member.user'));
154
    }
155
156
    public function testHas()
157
    {
158
        $array = ['member' => ['user' => ['name' => 'Alex']]];
159
        $this->assertTrue(Arrays::has($array, 'member.user.name', '.'));
160
        $this->assertFalse(Arrays::has($array, 'member.user.age', '.'));
161
        $this->assertTrue(Arrays::has(['' => 'some'], ['']));
162
        $this->assertFalse(Arrays::has([''], ''));
163
        $this->assertFalse(Arrays::has([], ''));
164
        $this->assertFalse(Arrays::has([], ['']));
165
    }
166
167
    public function testOnly()
168
    {
169
        $array = ['id' => 10, 'name' => 'Alex', 'age' => 18];
170
        $array = Arrays::subSetOfArray($array, ['name', 'age']);
171
        $this->assertEquals(['age' => 18, 'name' => 'Alex'], $array);
172
    }
173
174
    public function testPull()
175
    {
176
        $array = ['name' => 'Alex', 'age' => 18];
177
        $name = Arrays::pull($array, 'name');
178
        $this->assertEquals('Alex', $name);
179
    }
180
181
    public function testSet()
182
    {
183
        $array = ['members' => ['users' => ['name' => 'Alex']]];
184
        Arrays::set($array, 'members.users.name', 'Alex', '.');
185
        $this->assertEquals(['members' => ['users' => ['name' => 'Alex']]], $array);
186
    }
187
188
    public function testForget()
189
    {
190
        $array = ['members' => ['users' => ['name' => 'Alex']]];
191
        Arrays::forget($array, 'members.users', '.');
192
        $this->assertEquals(['members' => []], $array);
193
    }
194
195
    public function testArrayChangeCaseKey()
196
    {
197
        $array = ['Name' => 'Alex'];
198
        $this->assertSame(['name' => 'Alex'], Arrays::arrayChangeCaseKey($array));
199
        $this->assertNotSame(['Name' => 'Alex'], Arrays::arrayChangeCaseKey($array));
200
        $this->assertSame(['NAME' => 'Alex'], Arrays::arrayChangeCaseKey($array, CASE_UPPER));
201
        $this->assertNotSame(['name' => 'Alex'], Arrays::arrayChangeCaseKey($array, CASE_UPPER));
202
    }
203
204
    public function testArrayChangeCaseValue()
205
    {
206
        $array = ['name' => ['AleX', 'Peter']];
207
        $this->assertSame(['name' => ['alex', 'peter']], Arrays::arrayChangeCaseValue($array));
208
        $this->assertNotSame(['name' => ['Alex', 'Peter']], Arrays::arrayChangeCaseValue($array));
209
        $this->assertSame(['name' => ['ALEX', 'PETER']], Arrays::arrayChangeCaseValue($array, CASE_UPPER));
210
        $this->assertNotSame(['name' => ['Alex', 'Peter']], Arrays::arrayChangeCaseValue($array, CASE_UPPER));
211
    }
212
213
    public function removeDuplicatesDataProvider()
214
    {
215
        return [
216
            [
217
                [
218
                    'users' => [
219
                        'id'       => 1,
220
                        'name'     => 'Umer',
221
                        'username' => 'peter',
222
                    ],
223
                    [
224
                        'id'       => 2,
225
                        'name'     => 'Umer',
226
                        'username' => 'umer01',
227
                    ],
228
                    [
229
                        'id'       => 3,
230
                        'name'     => 'Peter Khot',
231
                        'username' => 'peter',
232
                    ],
233
                ],
234
                'username',
235
                [
236
                    0 => [
237
                        'id'       => 1,
238
                        'name'     => 'Umer',
239
                        'username' => 'peter',
240
                    ],
241
                    1 => [
242
                        'id'       => 2,
243
                        'name'     => 'Umer',
244
                        'username' => 'umer01',
245
                    ],
246
                ],
247
            ],
248
            [
249
                ['a' => 'green', 'red', 'b' => 'green', 'blue', 'red'],
250
                'username',
251
                ['a' => 'green', 0 => 'red', 1 => 'blue'],
252
            ],
253
            [
254
                [1, 2, 3, 4, 2, 5, 6, 3, 7, 8, 9],
255
                '',
256
                [
257
                    0  => 1,
258
                    1  => 2,
259
                    2  => 3,
260
                    3  => 4,
261
                    5  => 5,
262
                    6  => 6,
263
                    8  => 7,
264
                    9  => 8,
265
                    10 => 9,
266
                ],
267
            ],
268
        ];
269
    }
270
271
    /**
272
     * @dataProvider removeDuplicatesDataProvider
273
     */
274
    public function testRemoveDuplicates($dataSet, $specificKey, $expected)
275
    {
276
        $this->assertSame($expected, Arrays::removeDuplicates($dataSet, $specificKey));
277
    }
278
279
    public function testMostOccurring()
280
    {
281
        $dataSet = [1, 2, 3, 1, 4, 6, 3];
282
        $this->assertSame([1, 3], Arrays::mostOccurring($dataSet));
283
    }
284
285
    public function testLeastOccurring()
286
    {
287
        $dataSet = [1, 2, 1, 3, 1, 3];
288
        $this->assertSame([2], Arrays::leastOccurring($dataSet));
289
    }
290
291
    public function testQuery()
292
    {
293
        $this->assertSame('', Arrays::query([]));
294
        $this->assertSame('foo=bar', Arrays::query(['foo' => 'bar']));
295
        $this->assertSame('name=alex&age=18', Arrays::query(['name' => 'alex', 'age' => '18']));
296
    }
297
298
    public function testWhere()
299
    {
300
        $array = [100, '200', 300, '400', 500];
301
        $array = Arrays::where($array, function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

301
        $array = Arrays::where($array, function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
302
            return is_string($value);
303
        });
304
        $this->assertEquals([1 => '200', 3 => '400'], $array);
305
    }
306
307
    public function testShuffle()
308
    {
309
        $array1 = range(0, 100, 10);
310
        $array2 = range(0, 100, 10);
311
        Arrays::shuffle($array1);
312
        Arrays::shuffle($array2);
313
        $this->assertEquals($array1, $array2);
314
    }
315
316
    public function testRandom()
317
    {
318
        $random = Arrays::random(['foo', 'bar', 'baz'], 1);
319
        $this->assertContains($random[0], ['foo', 'bar', 'baz']);
320
    }
321
322
    public function testPluck()
323
    {
324
        $dataSet = [
325
            ['developer' => ['id' => 1, 'name' => 'Alex']],
326
            ['developer' => ['id' => 2, 'name' => 'Peter']],
327
        ];
328
        $this->assertSame(['Alex', 'Peter'], Arrays::pluck($dataSet, 'name'));
329
    }
330
}
331