GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Failed Conditions
Pull Request — master (#10)
by Chad
01:25
created

ArraysTest::getNested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Defines the \DominionEnterprises\Util\ArraysTest class
4
 */
5
6
namespace DominionEnterprises\Util;
7
8
use DominionEnterprises\Util\Arrays as A;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * @coversDefaultClass \DominionEnterprises\Util\Arrays
13
 */
14
final class ArraysTest extends TestCase
15
{
16
    /**
17
     * @test
18
     * @covers ::get
19
     */
20
    public function get()
21
    {
22
        $array = ['a' => 'foo', 'b' => 'bar'];
23
        $this->assertSame('foo', A::get($array, 'a'));
24
        $this->assertSame(null, A::get($array, 'c'));
25
        $this->assertSame('baz', A::get($array, 'c', 'baz'));
26
    }
27
28
    /**
29
     * @test
30
     * @covers ::getIfSet
31
     */
32
    public function getIfSet()
33
    {
34
        $array = ['a' => 'foo', 'b' => null];
35
        $this->assertSame('foo', A::getIfSet($array, 'a'));
36
        $this->assertSame('bar', A::getIfSet($array, 'b', 'bar'));
37
        $this->assertSame(null, A::getIfSet($array, 'c'));
38
        $this->assertSame('bar', A::getIfSet($array, 'c', 'bar'));
39
    }
40
41
    /**
42
     * @test
43
     * @covers ::copyIfKeysExist
44
     */
45
    public function copyIfKeysExist()
46
    {
47
        $source = ['a' => 'foo', 'b' => 'bar'];
48
49
        $result = [];
50
        A::copyIfKeysExist($source, $result, ['foo' => 'a', 'bar' => 'b']);
51
52
        $this->assertSame(['foo' => 'foo', 'bar' => 'bar'], $result);
53
    }
54
55
    /**
56
     * Verify behavior with numeric array $keyMap
57
     *
58
     * @test
59
     * @covers ::copyIfKeysExist
60
     */
61 View Code Duplication
    public function copyIfKeysExistNumericKeyMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $source = ['a' => 'foo', 'b' => 'bar', 'd' => 'baz'];
64
        $result = [];
65
        A::copyIfKeysExist($source, $result, ['a', 'b', 'c']);
66
        $this->assertSame(['a' => 'foo', 'b' => 'bar'], $result);
67
    }
68
69
    /**
70
     * Verify basic behavior of copyIfSet()
71
     *
72
     * @test
73
     * @covers ::copyIfSet
74
     */
75
    public function copyIfSet()
76
    {
77
        $source = ['a' => 'foo', 'b' => null, 'd' => 'baz'];
78
        $result = [];
79
        A::copyIfSet($source, $result, ['alpha' => 'a', 'beta' => 'b', 'charlie' => 'c', 'delta' => 'd']);
80
        $this->assertSame(['alpha' => 'foo', 'delta' => 'baz'], $result);
81
    }
82
83
    /**
84
     * Verify behavior of copyIfSet() with numeric array $keyMap
85
     *
86
     * @test
87
     * @covers ::copyIfSet
88
     */
89 View Code Duplication
    public function copyIfSetNumericKeyMap()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $source = ['a' => 'foo', 'b' => null, 'd' => 'baz'];
92
        $result = [];
93
        A::copyIfSet($source, $result, ['a', 'b', 'c', 'd']);
94
        $this->assertSame(['a' => 'foo', 'd' => 'baz'], $result);
95
    }
96
97
    /**
98
     * @test
99
     * @covers ::tryGet
100
     */
101
    public function tryGetNullKey()
102
    {
103
        $value = 'filler';
104
        $this->assertFalse(A::tryGet([], null, $value));
105
        $this->assertSame(null, $value);
106
    }
107
108
    /**
109
     * @test
110
     * @covers ::tryGet
111
     */
112
    public function tryGetClassForKey()
113
    {
114
        $value = 'filler';
115
        $this->assertFalse(A::tryGet([], new \stdClass(), $value));
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a string|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
        $this->assertSame(null, $value);
117
    }
118
119
    /**
120
     * @test
121
     * @covers ::tryGet
122
     */
123
    public function tryGetValueStringKey()
124
    {
125
        $value = 'filler';
126
        $this->assertTrue(A::tryGet(['a' => 1], 'a', $value));
127
        $this->assertSame(1, $value);
128
    }
129
130
    /**
131
     * @test
132
     * @covers ::tryGet
133
     */
134
    public function tryGetValueIntegerKey()
135
    {
136
        $value = 'filler';
137
        $this->assertTrue(A::tryGet([1.1, 2.2], 0, $value));
138
        $this->assertSame(1.1, $value);
139
    }
140
141
    /**
142
     * @test
143
     * @covers ::project
144
     */
145
    public function projectBasicUse()
146
    {
147
        $expected = [2, 'boo' => 4];
148
        $result = A::project([['key1' => 1, 'key2' => 2], 'boo' => ['key1' => 3, 'key2' => 4]], 'key2');
149
150
        $this->assertSame($expected, $result);
151
    }
152
153
    /**
154
     * @test
155
     * @covers ::project
156
     * @expectedException \InvalidArgumentException
157
     */
158
    public function projectStrictKeyFail()
159
    {
160
        A::project([['key1' => 1, 'key2' => 2], ['key1' => 3]], 'key2');
161
    }
162
163
    /**
164
     * @test
165
     * @covers ::project
166
     */
167
    public function projectStrictKeyFalse()
168
    {
169
        $expected = [1 => 4];
170
        $result = A::project([['key1' => 1], ['key1' => 3, 'key2' => 4]], 'key2', false);
171
172
        $this->assertSame($expected, $result);
173
    }
174
175
    /**
176
     * @test
177
     * @covers ::project
178
     * @expectedException \InvalidArgumentException
179
     * @expectedExceptionMessage $strictKeyCheck was not a bool
180
     */
181
    public function projectStrictKeyNotBool()
182
    {
183
        A::project([], 'not under test', 1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
184
    }
185
186
    /**
187
     * @test
188
     * @covers ::project
189
     * @expectedException \InvalidArgumentException
190
     * @expectedExceptionMessage a value in $input was not an array
191
     */
192
    public function projectInputValueNotArray()
193
    {
194
        A::project([1], 'not under test');
195
    }
196
197
    /**
198
     * Verifies basic usage for where() with exact matching
199
     *
200
     * @test
201
     * @covers ::where
202
     */
203
    public function whereBasicUsage()
204
    {
205
        $people = [
206
            ['name' => 'Tom', 'score' => '0'],
207
            ['name' => 'Dick', 'score' => 0],
208
            ['name' => 'Jane'],
209
        ];
210
211
        $expected = [['name' => 'Dick', 'score' => 0]];
212
        $result = A::where($people, ['score' => 0]);
213
        $this->assertSame($expected, $result);
214
    }
215
216
    /**
217
     * Verifies that where() returns empty array when nothing matches
218
     *
219
     * @test
220
     * @covers ::where
221
     */
222
    public function whereReturnsEmptyArray()
223
    {
224
        $people = [
225
            ['name' => 'Tom', 'score' => '0'],
226
            ['name' => 'Dick', 'score' => 0],
227
            ['name' => 'Harry', 'score' => 0.0],
228
        ];
229
230
        $result = A::where($people, ['score' => false]);
231
        $this->assertSame([], $result);
232
    }
233
234
    /**
235
     * Verifies use of multiple conditions
236
     *
237
     * @test
238
     * @covers ::where
239
     */
240
    public function whereWithMultipleConditions()
241
    {
242
        $people = [
243
            ['name' => 'Tom', 'score' => 1, 'extra' => 'abc'],
244
            ['name' => 'Dick', 'score' => 1, 'extra' => false],
245
            ['name' => 'Dick', 'score' => 0, 'extra' => 123],
246
        ];
247
248
        $expected = [['name' => 'Dick', 'score' => 1, 'extra' => false]];
249
        $result = A::where($people, ['name' => 'Dick', 'score' => 1]);
250
        $this->assertSame($expected, $result);
251
    }
252
253
    /**
254
     * Verifies use of multiple conditions
255
     *
256
     * @test
257
     * @covers ::where
258
     */
259
    public function whereReturnsMultipleResults()
260
    {
261
        $array = [
262
            ['key 1' => 'a', 'key 2' => 'b'],
263
            ['key 1' => 'c', 'key 2' => 'd'],
264
            ['key 1' => 'a', 'key 2' => 'c'],
265
        ];
266
267
        $expected = [
268
            ['key 1' => 'a', 'key 2' => 'b'],
269
            ['key 1' => 'a', 'key 2' => 'c'],
270
        ];
271
272
        $result = A::where($array, ['key 1' => 'a']);
273
        $this->assertSame($expected, $result);
274
    }
275
276
    /**
277
     * @test
278
     * @covers ::where
279
     * @expectedException \InvalidArgumentException
280
     * @expectedExceptionMessage a value in $array was not an array
281
     */
282
    public function whereInputValueNotArray()
283
    {
284
        A::where([1], []);
0 ignored issues
show
Documentation introduced by
array(1) is of type array<integer,integer,{"0":"integer"}>, but the function expects a array<integer,array>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
285
    }
286
287
    /**
288
     * Verifies that embedInto works well with adding new items into an existing array.
289
     *
290
     * @test
291
     * @covers ::embedInto
292
     */
293
    public function embedIntoBasicUse()
294
    {
295
        $this->assertSame(
296
            [
297
                ['request' => ['image' => 'foo'], 'result' => ['exception' => 'exception 1']],
298
                ['request' => ['image' => 'bar'], 'result' => ['exception' => 'exception 2']],
299
            ],
300
            A::embedInto(
301
                [['exception' => 'exception 1'], ['exception' => 'exception 2']],
302
                'result',
303
                [['request' => ['image' => 'foo']], ['request' => ['image' => 'bar']]]
304
            )
305
        );
306
    }
307
308
    /**
309
     * Verifies that embedInto works well with creating new records.
310
     *
311
     * @test
312
     * @covers ::embedInto
313
     */
314
    public function embedIntoEmptyDestination()
315
    {
316
        $this->assertSame(
317
            [['request' => ['image' => 'foo']], ['request' => ['image' => 'bar']]],
318
            A::embedInto([['image' => 'foo'], ['image' => 'bar']], 'request')
319
        );
320
    }
321
322
    /**
323
     * Verifies that embedInto requires string for fieldname
324
     *
325
     * @test
326
     * @covers ::embedInto
327
     * @expectedException InvalidArgumentException
328
     * @expectedExceptionMessage $fieldName was not a string
329
     */
330
    public function embedIntoNumericFieldName()
331
    {
332
        A::embedInto([], 5);
333
    }
334
335
    /**
336
     * Verifies that embedInto requires destination entries to be arrays.
337
     *
338
     * @test
339
     * @covers ::embedInto
340
     * @expectedException InvalidArgumentException
341
     * @expectedExceptionMessage a value in $destination was not an array
342
     */
343
    public function embedIntoNonArrayDestinationItems()
344
    {
345
        A::embedInto(['one' => 0], 'result', ['one' => 0]);
346
    }
347
348
    /**
349
     * Verifies that embedInto refuses to overwrite field names.
350
     *
351
     * @test
352
     * @covers ::embedInto
353
     * @expectedException Exception
354
     */
355
    public function embedIntoExistingFieldName()
356
    {
357
        A::embedInto(['new'], 'result', [['result' => 'old']]);
358
    }
359
360
    /**
361
     * Verifies that embedInto does nothing with 0 items to embed.
362
     *
363
     * @test
364
     * @covers ::embedInto
365
     */
366
    public function embedIntoNoItems()
367
    {
368
        $this->assertSame([['result' => 'foo']], A::embedInto([], 'result', [['result' => 'foo']]));
369
    }
370
371
    /**
372
     * @test
373
     * @covers ::embedInto
374
     */
375
    public function embedIntoOverwrite()
376
    {
377
        $this->assertSame([['key' => true]], A::embedInto([true], 'key', [['key' => false]], true));
378
    }
379
380
    /**
381
     * @test
382
     * @covers ::embedInto
383
     * @expectedException \InvalidArgumentException
384
     * @expectedExceptionMessage $overwrite was not a bool
385
     */
386
    public function embedIntoOverwriteNotBool()
387
    {
388
        A::embedInto([], 'key', [], 1);
0 ignored issues
show
Documentation introduced by
1 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
389
    }
390
391
    /**
392
     * Basic usage of fillIfKeysExist()
393
     *
394
     * @test
395
     * @covers ::fillIfKeysExist
396
     */
397
    public function fillIfKeysExist()
398
    {
399
        $template = ['a' => null, 'b' => null, 'c' => null, 'd' => null, 'e' => null];
400
401
        $actual = A::fillIfKeysExist($template, ['a' => 1, 'c' => 1, 'e' => 1]);
402
403
        $expected = ['a' => 1, 'b' => null, 'c' => 1, 'd' => null, 'e' => 1];
404
405
        $this->assertSame($expected, $actual);
406
    }
407
408
    /**
409
     * Basic usage of extract()
410
     *
411
     * @test
412
     * @covers ::extract
413
     */
414 View Code Duplication
    public function extract()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
415
    {
416
        $input = [
417
            ['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'],
418
            ['extra' => 123, 'key' => 'baz', 'value' => 'fez'],
419
            ['value' => 'duplicate1', 'extra' => true, 'key' => 'boo'],
420
            ['extra' => true, 'key' => 'noValue'],
421
            ['value' => 'duplicate2', 'extra' => true, 'key' => 'boo'],
422
        ];
423
424
        $expected = ['foo' => 'bar', 'baz' => 'fez', 'boo' => 'duplicate2', 'noValue' => null];
425
426
        $this->assertSame($expected, A::extract($input, 'key', 'value'));
427
    }
428
429
    /**
430
     * Basic usage of extract() with 'takeFirst' option
431
     *
432
     * @test
433
     * @covers ::extract
434
     */
435 View Code Duplication
    public function extractTakeFirst()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
436
    {
437
        $input = [
438
            ['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'],
439
            ['extra' => 123, 'key' => 'baz', 'value' => 'fez'],
440
            ['value' => 'duplicate1', 'extra' => true, 'key' => 'boo'],
441
            ['extra' => true, 'key' => 'noValue'],
442
            ['value' => 'duplicate2', 'extra' => true, 'key' => 'boo'],
443
        ];
444
445
        $expected = ['foo' => 'bar', 'baz' => 'fez', 'boo' => 'duplicate1', 'noValue' => null];
446
447
        $this->assertSame($expected, A::extract($input, 'key', 'value', 'takeFirst'));
448
    }
449
450
    /**
451
     * Basic usage of extract() with 'throw' option
452
     *
453
     * @test
454
     * @covers ::extract
455
     * @expectedException \Exception
456
     * @expectedExceptionMessage Duplicate entry for 'boo' found.
457
     */
458
    public function extractThrowOnDuplicate()
459
    {
460
        $input = [
461
            ['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'],
462
            ['extra' => 123, 'key' => 'baz', 'value' => 'fez'],
463
            ['value' => 'duplicate1', 'extra' => true, 'key' => 'boo'],
464
            ['extra' => true, 'key' => 'noValue'],
465
            ['value' => 'duplicate2', 'extra' => true, 'key' => 'boo'],
466
        ];
467
468
        A::extract($input, 'key', 'value', 'throw');
469
    }
470
471
    /**
472
     * Verify behavior when a single dimensional array is given to extract().
473
     *
474
     * @test
475
     * @covers ::extract
476
     * @expectedException \InvalidArgumentException
477
     * @expectedExceptionMessage $arrays was not a multi-dimensional array
478
     */
479
    public function extractWithSingleDimensionalArray()
480
    {
481
        A::extract(['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'], 'key', 'value');
482
    }
483
484
    /**
485
     * Verify behavior when $arrays contain a invalid key value in the supplied $keyIndex field.
486
     *
487
     * @test
488
     * @covers ::extract
489
     * @expectedException \UnexpectedValueException
490
     * @expectedExceptionMessage Value for $arrays[1][key] was not a string or integer
491
     */
492
    public function extractWithInvalidKeyValue()
493
    {
494
        $input = [
495
            ['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'],
496
            ['extra' => 123, 'key' => [], 'value' => 'fez'],
497
        ];
498
499
        A::extract($input, 'key', 'value', 'throw');
500
    }
501
502
    /**
503
     * Verify behavior when $keyIndex is not a string or integer
504
     *
505
     * @test
506
     * @covers ::extract
507
     * @expectedException \InvalidArgumentException
508
     * @expectedExceptionMessage $keyIndex was not a string or integer
509
     */
510
    public function extractWithInvalidKeyIndex()
511
    {
512
        A::extract([], true, 'value');
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
513
    }
514
515
    /**
516
     * Verify behavior when $valueIndex is not a string or integer
517
     *
518
     * @test
519
     * @covers ::extract
520
     * @expectedException \InvalidArgumentException
521
     * @expectedExceptionMessage $valueIndex was not a string or integer
522
     */
523
    public function extractWithInvalidValueIndex()
524
    {
525
        A::extract([], 'key', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
526
    }
527
528
    /**
529
     * Verify behavior when $duplicateBehavior is not valid
530
     *
531
     * @test
532
     * @covers ::extract
533
     * @expectedException \InvalidArgumentException
534
     * @expectedExceptionMessage $duplicateBehavior was not 'takeFirst', 'takeLast', or 'throw'
535
     */
536
    public function extractWithInvalidDuplicateBehavior()
537
    {
538
        A::extract([], 'key', 'value', 'invalid');
539
    }
540
541
    /**
542
     * Verify basic behavior of getFirstSet()
543
     *
544
     * @test
545
     * @covers ::getFirstSet
546
     */
547
    public function getFirstSet()
548
    {
549
        $this->assertSame('bar', A::getFirstSet(['foo', null, 'bar'], [1, 2]));
550
    }
551
552
    /**
553
     * Verify getFirstSet() returns default value
554
     *
555
     * @test
556
     * @covers ::getFirstSet
557
     */
558
    public function getFirstSetWithDefault()
559
    {
560
        $this->assertSame('baz', A::getFirstSet(['foo', null, 'bar'], [1, 4], 'baz'));
561
    }
562
563
    /**
564
     * Verifiy basic behavior of partition()
565
     *
566
     * @test
567
     * @covers ::partition
568
     */
569
    public function partition()
570
    {
571
        $this->assertSame([['a'], ['b'], ['c']], A::partition(['a', 'b', 'c'], 3));
572
    }
573
574
    /**
575
     * Verify partition() behavior when $input array contains less items than than $partitionCount.
576
     *
577
     * @test
578
     * @covers ::partition
579
     */
580
    public function partitionInputLessThanPartitionCount()
581
    {
582
        $this->assertSame([['a'], ['b'], ['c']], A::partition(['a', 'b', 'c'], 4));
583
    }
584
585
    /**
586
     * Verify remainder of $input array is front-loaded in partition().
587
     *
588
     * @test
589
     * @covers ::partition
590
     */
591
    public function partitionWithRemainder()
592
    {
593
        $this->assertSame([['a', 'b'], ['c'], ['d']], A::partition(['a', 'b', 'c', 'd'], 3));
594
    }
595
596
    /**
597
     * Verify remainder of $input array is front-loaded in partition().
598
     *
599
     * @test
600
     * @covers ::partition
601
     */
602
    public function partitionWithMultipleRemainder()
603
    {
604
        $this->assertSame([['a', 'b'], ['c', 'd'], ['e']], A::partition(['a', 'b', 'c', 'd', 'e'], 3));
605
    }
606
607
    /**
608
     * Verify partition() handles empty $input array.
609
     *
610
     * @test
611
     * @covers ::partition
612
     */
613
    public function partitionEmptyInput()
614
    {
615
        $this->assertSame([], A::partition([], 2));
616
    }
617
618
    /**
619
     * Verifiy behavior of partition() with $partitionCount of 1.
620
     *
621
     * @test
622
     * @covers ::partition
623
     */
624
    public function partitionOnePartition()
625
    {
626
        $this->assertSame([['a', 'b', 'c']], A::partition(['a', 'b', 'c'], 1));
627
    }
628
629
    /**
630
     * Verifiy partition() throws with negative $partitionCount.
631
     *
632
     * @test
633
     * @covers ::partition
634
     * @expectedException \InvalidArgumentException
635
     * @expectedExceptionMessage $partitionCount must be a positive integer
636
     */
637
    public function partitionNegativePartitionCount()
638
    {
639
        A::partition(['a', 'b', 'c'], -1);
640
    }
641
642
    /**
643
     * Verifiy partition() throws with 0 $partitionCount.
644
     *
645
     * @test
646
     * @covers ::partition
647
     * @expectedException \InvalidArgumentException
648
     * @expectedExceptionMessage $partitionCount must be a positive integer
649
     */
650
    public function partitionZeroPartitionCount()
651
    {
652
        A::partition(['a', 'b', 'c'], 0);
653
    }
654
655
    /**
656
     * Verifiy partition() throws with non-integer $partitionCount.
657
     *
658
     * @test
659
     * @covers ::partition
660
     * @expectedException \InvalidArgumentException
661
     * @expectedExceptionMessage $partitionCount must be a positive integer
662
     */
663
    public function partitionNonIntegerPartitionCount()
664
    {
665
        A::partition(['a', 'b', 'c'], 'not an int');
666
    }
667
668
    /**
669
     * Verifiy partition() preserves numeric keys.
670
     *
671
     * @test
672
     * @covers ::partition
673
     */
674
    public function partitionPreserveNumericKeys()
675
    {
676
        $this->assertSame(
677
            [[0 => 'a', 1 => 'b'], [2 => 'c', 3 => 'd'], [4 => 'e']],
678
            A::partition(['a', 'b', 'c', 'd', 'e'], 3, true)
679
        );
680
    }
681
682
    /**
683
     * Verifiy partition() preserves associative keys.
684
     *
685
     * @test
686
     * @covers ::partition
687
     */
688
    public function partitionPreserveAssociativeKeys()
689
    {
690
        $this->assertSame(
691
            [['a' => 0, 'b' => 1], ['c' => 2, 'd' => 3], ['e' => 4]],
692
            A::partition(['a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4], 3)
693
        );
694
    }
695
696
    /**
697
     * Verifiy partition() throws with non-boolean $preserveKeys.
698
     *
699
     * @test
700
     * @covers ::partition
701
     * @expectedException \InvalidArgumentException
702
     * @expectedExceptionMessage $preserveKeys must be a boolean value
703
     */
704
    public function partitionNonBoolPreserveKeys()
705
    {
706
        A::partition(['a', 'b', 'c'], 3, 'not a bool');
0 ignored issues
show
Documentation introduced by
'not a bool' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
707
    }
708
709
    /**
710
     * Verify basic behavior of unsetAll().
711
     *
712
     * @test
713
     * @covers ::unsetAll
714
     */
715
    public function unsetAll()
716
    {
717
        $array = ['a', 'b', 'c'];
718
        A::unsetAll($array, [0, 2]);
719
        $this->assertSame([1 => 'b'], $array);
720
    }
721
722
    /**
723
     * Verify behavior of unsetAll() with empty array.
724
     *
725
     * @test
726
     * @covers ::unsetAll
727
     */
728
    public function unsetAllEmptyArray()
729
    {
730
        $array = [];
731
        A::unsetAll($array, [0, 2]);
732
        // array unchanged
733
        $this->assertSame([], $array);
734
    }
735
736
    /**
737
     * Verify behavior of unsetAll() with empty keys.
738
     *
739
     * @test
740
     * @covers ::unsetAll
741
     */
742 View Code Duplication
    public function unsetAllEmptyKeys()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
743
    {
744
        $array = ['a', 'b', 'c'];
745
        A::unsetAll($array, []);
746
        // array unchanged
747
        $this->assertSame(['a', 'b', 'c'], $array);
748
    }
749
750
    /**
751
     * Verify behavior of unsetAll() with keys that don't exist
752
     *
753
     * @test
754
     * @covers ::unsetAll
755
     */
756 View Code Duplication
    public function unsetAllKeyNotFound()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
757
    {
758
        $array = ['a', 'b', 'c'];
759
        A::unsetAll($array, [3, 4]);
760
        // array unchanged
761
        $this->assertSame(['a', 'b', 'c'], $array);
762
    }
763
764
    /**
765
     * Verify basic behavior of nullifyEmptyStrings().
766
     * @test
767
     * @covers ::nullifyEmptyStrings
768
     */
769
    public function nullifyEmptyStrings()
770
    {
771
        $array = ['a' => '', 'b' => true, 'c' => "\n\t", 'd' => "\tstring with whitespace\n"];
772
        A::nullifyEmptyStrings($array);
773
        $this->assertSame(['a' => null, 'b' => true, 'c' => null, 'd' => "\tstring with whitespace\n"], $array);
774
    }
775
776
    /**
777
     * Verify behavior of nullifyEmptyStrings() with empty input.
778
     * @test
779
     * @covers ::nullifyEmptyStrings
780
     */
781
    public function nullifyEmptyStringsEmptyArray()
782
    {
783
        $array = [];
784
        A::nullifyEmptyStrings($array);
785
        $this->assertSame([], $array);
786
    }
787
788
    /**
789
     * Verify basic functionality of getNested.
790
     *
791
     * @test
792
     * @covers ::getNested
793
     *
794
     * @return void
795
     */
796
    public function getNested()
797
    {
798
        $array = ['db' => ['host' => 'localhost', 'login' => [ 'username' => 'scott', 'password' => 'tiger']]];
799
        $this->assertSame('scott', A::getNested($array, 'db.login.username'));
800
    }
801
802
    /**
803
     * Verify behavior when the given delimitedKey does not exist in the given array.
804
     *
805
     * @test
806
     * @covers ::getNested
807
     *
808
     * @return void
809
     */
810
    public function getNestedPathNotFound()
811
    {
812
        $array = ['db' => ['host' => 'localhost', 'login' => [ 'username' => 'scott', 'password' => 'tiger']]];
813
        $this->assertNull(A::getNested($array, 'db.notfound.username'));
814
    }
815
816
    /**
817
     * Verify functionality of changeKeyCase().
818
     *
819
     * @test
820
     * @covers ::changeKeyCase
821
     * @dataProvider changeKeyCaseData
822
     *
823
     * @return void
824
     */
825
    public function changeKeyCase($input, $case, $expected)
826
    {
827
        $this->assertSame($expected, A::changeKeyCase($input, $case));
828
    }
829
830
    /**
831
     * Dataprovider for changeKeyCase test.
832
     *
833
     * @return array
834
     */
835
    public function changeKeyCaseData()
836
    {
837
        $lowerUnderscore = [
838
            'first_and_last_name' => 'John Doe',
839
            'email_address' => '[email protected]',
840
            'age' => 35,
841
        ];
842
843
        $upperUnderscore = [
844
            'FIRST_AND_LAST_NAME' => 'John Doe',
845
            'EMAIL_ADDRESS' => '[email protected]',
846
            'AGE' => 35,
847
        ];
848
849
        $camelCaps = [
850
            'firstAndLastName' => 'John Doe',
851
            'emailAddress' => '[email protected]',
852
            'age' => 35,
853
        ];
854
855
        $underscore = [
856
            'first_And_Last_Name' => 'John Doe',
857
            'email_Address' => '[email protected]',
858
            'age' => 35,
859
        ];
860
861
        $lower = [
862
            'firstandlastname' => 'John Doe',
863
            'emailaddress' => '[email protected]',
864
            'age' => 35,
865
        ];
866
867
        $upper = [
868
            'FIRSTANDLASTNAME' => 'John Doe',
869
            'EMAILADDRESS' => '[email protected]',
870
            'AGE' => 35,
871
        ];
872
873
        return [
874
            'upper to lower' => [$upper, A::CASE_LOWER, $lower],
875
            'lower to upper' => [$lower, A::CASE_UPPER, $upper],
876
            'underscore to camel' => [$lowerUnderscore, A::CASE_CAMEL_CAPS, $camelCaps],
877
            'camel to underscore' => [$camelCaps, A::CASE_UNDERSCORE, $underscore],
878
            'camel to upper underscore' => [$camelCaps, A::CASE_UNDERSCORE | A::CASE_UPPER, $upperUnderscore],
879
            'camel to lower underscore' => [$camelCaps, A::CASE_UNDERSCORE | A::CASE_LOWER, $lowerUnderscore],
880
            'lower underscore to upper camel' => [$lowerUnderscore, A::CASE_CAMEL_CAPS | A::CASE_UPPER, $upper],
881
        ];
882
    }
883
884
    /**
885
     * Verify basic behavior of flatten().
886
     *
887
     * @test
888
     * @covers ::flatten
889
     *
890
     * @return void
891
     */
892 View Code Duplication
    public function flatten()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
893
    {
894
        $array = [
895
            'db' => [
896
                'host' => 'localhost',
897
                'login' => [
898
                    'username' => 'scott',
899
                    'password' => 'tiger'
900
                ]
901
            ]
902
        ];
903
904
        $expected = [
905
            'db.host' => 'localhost',
906
            'db.login.username' => 'scott',
907
            'db.login.password' => 'tiger',
908
        ];
909
910
        $this->assertSame($expected, A::flatten($array));
911
    }
912
913
    /**
914
     * Verify behavior of flatten() with custom delimiter.
915
     *
916
     * @test
917
     * @covers ::flatten
918
     *
919
     * @return void
920
     */
921 View Code Duplication
    public function flattenWithCustomDelimiter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
922
    {
923
        $array = [
924
            'db' => [
925
                'host' => 'localhost',
926
                'login' => [
927
                    'username' => 'scott',
928
                    'password' => 'tiger'
929
                ]
930
            ]
931
        ];
932
933
        $expected = [
934
            'db/host' => 'localhost',
935
            'db/login/username' => 'scott',
936
            'db/login/password' => 'tiger',
937
        ];
938
939
        $this->assertSame($expected, A::flatten($array, '/'));
940
    }
941
942
    /**
943
     * @test
944
     * @covers ::getAllWhereKeyExists
945
     *
946
     * @return void
947
     */
948
    public function getAllWhereKeyExists()
949
    {
950
        $input = [
951
            [
952
                'id' => 1,
953
                'code' => 'foo',
954
                'extra' => 'abc',
955
            ],
956
            [
957
                'id' => 2,
958
                'code' => 'bar',
959
            ],
960
            [
961
                'id' => 3,
962
                'code' => 'baz',
963
                'extra' => 'xyz',
964
            ],
965
        ];
966
967
        $this->assertSame(
968
            [
969
                0 => [
970
                    'id' => 1,
971
                    'code' => 'foo',
972
                    'extra' => 'abc',
973
                ],
974
                2 => [
975
                    'id' => 3,
976
                    'code' => 'baz',
977
                    'extra' => 'xyz',
978
                ],
979
            ],
980
            A::getAllWhereKeyExists($input, 'extra')
981
        );
982
    }
983
}
984