Passed
Branch develop (bae466)
by Paul
06:21
created

ArrTest::test_get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Tests;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use WP_UnitTestCase;
0 ignored issues
show
Bug introduced by
The type WP_UnitTestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * Test case for the Plugin.
10
 * @group plugin
11
 */
12
class ArrTest extends WP_UnitTestCase
13
{
14
    public function test_compare()
15
    {
16
        $this->assertTrue(Arr::compare(['one' => ['two']], ['one' => ['two']]));
17
        $this->assertFalse(Arr::compare(['one' => ['two']], ['one' => 'two']));
18
    }
19
20
    public function test_consolidate()
21
    {
22
        $this->assertEquals(Arr::consolidate(''), []);
23
        $this->assertEquals(Arr::consolidate('0'), []);
24
        $this->assertEquals(Arr::consolidate('1'), []);
25
        $this->assertEquals(Arr::consolidate('1,2'), []);
26
        $this->assertEquals(Arr::consolidate((object)[]), []);
27
        $this->assertEquals(Arr::consolidate(0), []);
28
        $this->assertEquals(Arr::consolidate(1), []);
29
        $this->assertEquals(Arr::consolidate([]), []);
30
        $this->assertEquals(Arr::consolidate(false), []);
31
        $this->assertEquals(Arr::consolidate(true), []);
32
        $this->assertEquals(Arr::consolidate((object)[1]), [1]);
33
    }
34
35
    public function test_convert_from_string()
36
    {
37
        $this->assertEquals(Arr::convertFromString(',a,,b,c,1,,'), ['a','b','c','1']);
38
    }
39
40
    public function test_flatten()
41
    {
42
        $test = ['one' => ['two' => ['three' => ['x', 'y', 'z']]]];
43
        $this->assertEquals(Arr::flatten($test),
44
            ['one.two.three' => ['x', 'y', 'z']]
45
        );
46
        $this->assertEquals(Arr::flatten($test, true),
47
            ['one.two.three' => '[x, y, z]']
48
        );
49
        $this->assertEquals(Arr::flatten($test, true, 'test'),
50
            ['test.one.two.three' => '[x, y, z]']
51
        );
52
    }
53
54
    public function test_get()
55
    {
56
        $values1 = ['parent' => ['child' => 'toys']];
57
        $values2 = ['parent' => ['child' => (object) ['toys' => 123]]];
58
        $this->assertEquals(Arr::get($values1, 'parent.child'), 'toys');
59
        $this->assertEquals(Arr::get($values1, 'parent.child.toys', 'fallback'), 'fallback');
60
        $this->assertTrue(is_object(Arr::get($values2, 'parent.child')));
61
    }
62
63
    public function test_get_as()
64
    {
65
        $values1 = ['parent' => ['child' => 'toys', 'number' => '2.3', 'version' => '2.0.0']];
66
        $this->assertEquals(Arr::getAs('array', $values1, 'parent.child'), ['toys']);
67
        $this->assertEquals(Arr::getAs('bool', $values1, 'parent.child'), false);
68
        $this->assertEquals(Arr::getAs('float', $values1, 'parent.child'), 0);
69
        $this->assertEquals(Arr::getAs('float', $values1, 'parent.number'), 2.3);
70
        $this->assertEquals(Arr::getAs('float', $values1, 'parent.version'), 0);
71
        $this->assertEquals(Arr::getAs('int', $values1, 'parent.child'), 0);
72
        $this->assertEquals(Arr::getAs('int', $values1, 'parent.number'), 2);
73
        $this->assertEquals(Arr::getAs('int', $values1, 'parent.version'), 0);
74
        $this->assertEquals(Arr::getAs('object', $values1, 'parent.child'), (object) ['toys']);
75
        $this->assertEquals(Arr::getAs('string', $values1, 'parent.child'), 'toys');
76
    }
77
78
    public function test_insert_after()
79
    {
80
        $array1 = ['1','2','3'];
81
        $array2 = ['a' => 1, 'b' => 2, 'c' => 3];
82
        $this->assertEquals(Arr::insertAfter(1, $array1, ['a','b']), ['1','2','a','b','3']);
83
        $this->assertEquals(Arr::insertAfter(9, $array1, ['a','b']), ['1','2','3','a','b']);
84
        $this->assertEquals(Arr::insertAfter('b', $array2, ['z' => 13]), ['a' => 1, 'b' => 2, 'z' => 13, 'c' => 3]);
85
        $this->assertEquals(Arr::insertAfter('z', $array2, ['z' => 13]), ['a' => 1, 'b' => 2, 'c' => 3, 'z' => 13]);
86
    }
87
88
    public function test_insert_before()
89
    {
90
        $array1 = ['1','2','3'];
91
        $array2 = ['a' => 1, 'b' => 2, 'c' => 3];
92
        $this->assertEquals(Arr::insertBefore(1, $array1, ['a','b']), ['1','a','b','2','3']);
93
        $this->assertEquals(Arr::insertBefore(9, $array1, ['a','b']), ['1','2','3','a','b']);
94
        $this->assertEquals(Arr::insertBefore('b', $array2, ['z' => 13]), ['a' => 1, 'z' => 13, 'b' => 2, 'c' => 3]);
95
        $this->assertEquals(Arr::insertBefore('z', $array2, ['z' => 13]), ['a' => 1, 'b' => 2, 'c' => 3, 'z' => 13]);
96
    }
97
98
    public function test_is_indexed_and_flat()
99
    {
100
        $this->assertFalse(Arr::isIndexedAndFlat('not an array'));
101
        $this->assertFalse(Arr::isIndexedAndFlat([[]]));
102
        $this->assertTrue(Arr::isIndexedAndFlat([]));
103
        $this->assertTrue(Arr::isIndexedAndFlat([1, 2, 3]));
104
    }
105
106
    public function test_prefix_keys()
107
    {
108
        $array = ['_a' => '', 'b' => ''];
109
        $this->assertEquals(Arr::prefixKeys($array), ['_a' => '', '_b' => '']);
110
    }
111
112
    public function test_prepend()
113
    {
114
        $array1 = ['1','2','3'];
115
        $array2 = ['a' => 1, 'b' => 2, 'c' => 3];
116
        $this->assertEquals(Arr::prepend($array1, 13), ['13','1','2','3']);
117
        $this->assertEquals(Arr::prepend($array2, 13, 'z'), ['z' => 13, 'a' => 1, 'b' => 2, 'c' => 3]);
118
    }
119
120
    public function test_reindex()
121
    {
122
        $array = ['1','2','3','4','5'];
123
        unset($array[3]);
124
        $this->assertEquals(Arr::reindex($array), ['1','2','3','5']);
125
    }
126
127
    public function test_remove()
128
    {
129
        $array = [
130
            'indexed',
131
            'emptyString' => '',
132
            'array' => ['string' => 'string'],
133
        ];
134
        $this->assertEquals(Arr::remove($array), $array);
135
        $this->assertEquals(Arr::remove($array, ''), $array);
136
        $this->assertEquals(Arr::remove($array, '0'), 
137
            ['emptyString' => '', 'array' => ['string' => 'string']]
138
        );
139
        $this->assertEquals(Arr::remove($array, 0), 
140
            ['emptyString' => '', 'array' => ['string' => 'string']]
141
        );
142
        $this->assertEquals(Arr::remove($array, 'array'),
143
            ['indexed', 'emptyString' => '']
144
        );
145
        $this->assertEquals(Arr::remove($array, 'array.string'),
146
            ['indexed', 'emptyString' => '', 'array' => []]
147
        );
148
    }
149
150
    public function test_remove_empty_values()
151
    {
152
        $array = [
153
            'emptyString' => '',
154
            'emptyArray' => [],
155
            'array' => [
156
                'string' => 'string',
157
                'emptyString' => [],
158
            ],
159
        ];
160
        $this->assertEquals(Arr::removeEmptyValues($array),
161
            ['array' => ['string' => 'string']]
162
        );
163
    }
164
165
    public function test_restrict_keys()
166
    {
167
        $array = [
168
            'user_a' => ['id' => 1, 'name' => 'Alice'],
169
            0 => ['id' => 2, 'name' => 'Bob'],
170
            '' => ['id' => 5, 'name' => 'Grace'],
171
            'string_id' => ['id' => '001', 'name' => 'Frank'],
172
            null => ['id' => 4, 'name' => 'David'],
173
            1 => ['id' => 3, 'name' => 'Charlie'],
174
        ];
175
        // Test case 1: Restrict to non-existent keys
176
        $result = Arr::restrictKeys($array, ['non_existent']);
177
        $this->assertEquals([], $result);
178
        // Test case 2: Empty allowed keys
179
        $result = Arr::restrictKeys($array, []);
180
        $this->assertEquals([], $result);
181
        // Test case 3: Empty array
182
        $result = Arr::restrictKeys([], ['user_a']);
183
        $this->assertEquals([], $result);
184
        // Test case 4: Empty string key (returns null key value due to collision)
185
        $result = Arr::restrictKeys($array, ['']);
186
        $this->assertEquals(['' => ['id' => 4, 'name' => 'David']], $result);
187
        // Test case 5: Restrict to existing keys
188
        $result = Arr::restrictKeys($array, ['user_a', 'string_id']);
189
        $this->assertEquals([
190
            'user_a' => ['id' => 1, 'name' => 'Alice'],
191
            'string_id' => ['id' => '001', 'name' => 'Frank'],
192
        ], $result);
193
        // Test case 6: Mixed existing and non-existent keys
194
        $result = Arr::restrictKeys($array, ['user_a', 0, 'missing']);
195
        $this->assertEquals([
196
            'user_a' => ['id' => 1, 'name' => 'Alice'],
197
            0 => ['id' => 2, 'name' => 'Bob'],
198
        ], $result);
199
        // Test case 7: Numeric and null keys
200
        $result = Arr::restrictKeys($array, [0, 1, null]);
201
        $this->assertEquals([
202
            0 => ['id' => 2, 'name' => 'Bob'],
203
            1 => ['id' => 3, 'name' => 'Charlie'],
204
            null => ['id' => 4, 'name' => 'David'],
205
        ], $result);
206
    }
207
208
    public function test_search_by_key()
209
    {
210
        $haystack = [
211
            null => ['id' => 4, 'name' => 'David'],
212
            '' => ['id' => 5, 'name' => 'Grace'],
213
            'string_id' => ['id' => '001', 'name' => 'Frank'],
214
            'user_a' => ['id' => 1, 'name' => 'Alice'],
215
            0 => ['id' => 2, 'name' => 'Bob'],
216
            1 => ['id' => 3, 'name' => 'Charlie'],
217
        ];
218
        // Test case 1: Basic search with valid needle and key
219
        $result = Arr::searchByKey(2, $haystack, 'id');
220
        $this->assertEquals(['id' => 2, 'name' => 'Bob'], $result);
221
        // Test case 2: String needle with strict comparison
222
        $result = Arr::searchByKey('001', $haystack, 'id');
223
        $this->assertEquals(['id' => '001', 'name' => 'Frank'], $result);
224
        // Test case 3: Haystack with empty string key (with null key collision)
225
        $result = Arr::searchByKey(5, $haystack, 'id');
226
        $this->assertEquals(['id' => 5, 'name' => 'Grace'], $result);
227
        // Test case 4: Haystack with non-auto-incrementing keys
228
        $result = Arr::searchByKey(3, $haystack, 'id');
229
        $this->assertEquals(['id' => 3, 'name' => 'Charlie'], $result);
230
        // Test case 5: Search with non-existent needle
231
        $result = Arr::searchByKey(6, $haystack, 'id');
232
        $this->assertFalse($result);
233
        // Test case 6: Search with non-existent key
234
        $result = Arr::searchByKey('Alice', $haystack, 'email');
235
        $this->assertFalse($result);
236
        // Test case 7: Empty haystack
237
        $result = Arr::searchByKey(1, [], 'id');
238
        $this->assertFalse($result);
239
        // Test case 8: Non-array haystack
240
        $result = Arr::searchByKey(1, 'not an array', 'id');
0 ignored issues
show
Bug introduced by
'not an array' of type string is incompatible with the type array expected by parameter $haystack of GeminiLabs\SiteReviews\Helpers\Arr::searchByKey(). ( Ignorable by Annotation )

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

240
        $result = Arr::searchByKey(1, /** @scrutinizer ignore-type */ 'not an array', 'id');
Loading history...
241
        $this->assertFalse($result);
242
        // Test case 9: Haystack with mismatched keys and values count
243
        $result = Arr::searchByKey(1, array_merge($haystack, ['incomplete' => ['name' => 'Eve']]), 'id');
244
        $this->assertFalse($result);
245
        // Test case 10: Haystack with null key (converted to empty string by array_combine, overwritten by last empty string key)
246
        $result = Arr::searchByKey(4, $haystack, 'id');
247
        $this->assertFalse($result);
248
    }
249
250
    public function test_set()
251
    {
252
        $this->assertEquals(Arr::set([], 'number.thirteen', '13'),
253
            ['number' => ['thirteen' => '13']]
254
        );
255
        $this->assertEquals(Arr::set([], '', '13'),
256
            []
257
        );
258
    }
259
260
    public function test_unflatten()
261
    {
262
        $original = ['parent.child' => 'toys'];
263
        $converted = ['parent' => ['child' => 'toys']];
264
        $this->assertEquals(Arr::unflatten($original), $converted);
265
    }
266
267
    public function test_unique()
268
    {
269
        $array = ['1','3','2','3','4','3','5'];
270
        $array = Arr::unique($array);
271
        sort($array);
272
        $this->assertEquals($array, ['1','2','3','4','5']);
273
    }
274
275
    public function test_unique_int()
276
    {
277
        $array = ['1','3','2','a','4','3','5','0', '-1'];
278
        $test1 = Arr::uniqueInt($array);
279
        sort($test1);
280
        $this->assertEquals($test1, [1,2,3,4,5]);
281
        $test2 = Arr::uniqueInt($array, false);
282
        sort($test2);
283
        $this->assertEquals($test2, [-1,0,1,2,3,4,5]);
284
    }
285
286
    public function test_unprefix_keys()
287
    {
288
        $array = ['_a' => '', 'b' => ''];
289
        $this->assertEquals(Arr::unprefixKeys($array), ['a' => '', 'b' => '']);
290
    }
291
}
292