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
02:54 queued 33s
created

ArraysTest::partition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
10
/**
11
 * @coversDefaultClass \DominionEnterprises\Util\Arrays
12
 */
13
final class ArraysTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @test
17
     * @covers ::get
18
     */
19
    public function get()
20
    {
21
        $array = ['a' => 'foo', 'b' => 'bar'];
22
        $this->assertSame('foo', A::get($array, 'a'));
23
        $this->assertSame(null, A::get($array, 'c'));
24
        $this->assertSame('baz', A::get($array, 'c', 'baz'));
25
    }
26
27
    /**
28
     * @test
29
     * @covers ::getIfSet
30
     */
31
    public function getIfSet()
32
    {
33
        $array = ['a' => 'foo', 'b' => null];
34
        $this->assertSame('foo', A::getIfSet($array, 'a'));
35
        $this->assertSame('bar', A::getIfSet($array, 'b', 'bar'));
36
        $this->assertSame(null, A::getIfSet($array, 'c'));
37
        $this->assertSame('bar', A::getIfSet($array, 'c', 'bar'));
38
    }
39
40
    /**
41
     * @test
42
     * @covers ::copyIfKeysExist
43
     */
44
    public function copyIfKeysExist()
45
    {
46
        $source = ['a' => 'foo', 'b' => 'bar'];
47
48
        $result = [];
49
        A::copyIfKeysExist($source, $result, ['foo' => 'a', 'bar' => 'b']);
50
51
        $this->assertSame(['foo' => 'foo', 'bar' => 'bar'], $result);
52
    }
53
54
    /**
55
     * Verify behavior with numeric array $keyMap
56
     *
57
     * @test
58
     * @covers ::copyIfKeysExist
59
     */
60 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...
61
    {
62
        $source = ['a' => 'foo', 'b' => 'bar', 'd' => 'baz'];
63
        $result = [];
64
        A::copyIfKeysExist($source, $result, ['a', 'b', 'c']);
65
        $this->assertSame(['a' => 'foo', 'b' => 'bar'], $result);
66
    }
67
68
    /**
69
     * Verify basic behavior of copyIfSet()
70
     *
71
     * @test
72
     * @covers ::copyIfSet
73
     */
74
    public function copyIfSet()
75
    {
76
        $source = ['a' => 'foo', 'b' => null, 'd' => 'baz'];
77
        $result = [];
78
        A::copyIfSet($source, $result, ['alpha' => 'a', 'beta' => 'b', 'charlie' => 'c', 'delta' => 'd']);
79
        $this->assertSame(['alpha' => 'foo', 'delta' => 'baz'], $result);
80
    }
81
82
    /**
83
     * Verify behavior of copyIfSet() with numeric array $keyMap
84
     *
85
     * @test
86
     * @covers ::copyIfSet
87
     */
88 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...
89
    {
90
        $source = ['a' => 'foo', 'b' => null, 'd' => 'baz'];
91
        $result = [];
92
        A::copyIfSet($source, $result, ['a', 'b', 'c', 'd']);
93
        $this->assertSame(['a' => 'foo', 'd' => 'baz'], $result);
94
    }
95
96
    /**
97
     * @test
98
     * @covers ::tryGet
99
     */
100
    public function tryGetNullKey()
101
    {
102
        $value = 'filler';
103
        $this->assertFalse(A::tryGet([], null, $value));
104
        $this->assertSame(null, $value);
105
    }
106
107
    /**
108
     * @test
109
     * @covers ::tryGet
110
     */
111
    public function tryGetClassForKey()
112
    {
113
        $value = 'filler';
114
        $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...
115
        $this->assertSame(null, $value);
116
    }
117
118
    /**
119
     * @test
120
     * @covers ::tryGet
121
     */
122
    public function tryGetValueStringKey()
123
    {
124
        $value = 'filler';
125
        $this->assertTrue(A::tryGet(['a' => 1], 'a', $value));
126
        $this->assertSame(1, $value);
127
    }
128
129
    /**
130
     * @test
131
     * @covers ::tryGet
132
     */
133
    public function tryGetValueIntegerKey()
134
    {
135
        $value = 'filler';
136
        $this->assertTrue(A::tryGet([1.1, 2.2], 0, $value));
137
        $this->assertSame(1.1, $value);
138
    }
139
140
    /**
141
     * @test
142
     * @covers ::project
143
     */
144
    public function projectBasicUse()
145
    {
146
        $expected = [2, 'boo' => 4];
147
        $result = A::project([['key1' => 1, 'key2' => 2], 'boo' => ['key1' => 3, 'key2' => 4]], 'key2');
148
149
        $this->assertSame($expected, $result);
150
    }
151
152
    /**
153
     * @test
154
     * @covers ::project
155
     * @expectedException \InvalidArgumentException
156
     */
157
    public function projectStrictKeyFail()
158
    {
159
        A::project([['key1' => 1, 'key2' => 2], ['key1' => 3]], 'key2');
160
    }
161
162
    /**
163
     * @test
164
     * @covers ::project
165
     */
166
    public function projectStrictKeyFalse()
167
    {
168
        $expected = [1 => 4];
169
        $result = A::project([['key1' => 1], ['key1' => 3, 'key2' => 4]], 'key2', false);
170
171
        $this->assertSame($expected, $result);
172
    }
173
174
    /**
175
     * @test
176
     * @covers ::project
177
     * @expectedException \InvalidArgumentException
178
     * @expectedExceptionMessage $strictKeyCheck was not a bool
179
     */
180
    public function projectStrictKeyNotBool()
181
    {
182
        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...
183
    }
184
185
    /**
186
     * @test
187
     * @covers ::project
188
     * @expectedException \InvalidArgumentException
189
     * @expectedExceptionMessage a value in $input was not an array
190
     */
191
    public function projectInputValueNotArray()
192
    {
193
        A::project([1], 'not under test');
194
    }
195
196
    /**
197
     * Verifies basic usage for where() with exact matching
198
     *
199
     * @test
200
     * @covers ::where
201
     */
202
    public function whereBasicUsage()
203
    {
204
        $people = [
205
            ['name' => 'Tom', 'score' => '0'],
206
            ['name' => 'Dick', 'score' => 0],
207
            ['name' => 'Jane'],
208
        ];
209
210
        $expected = [['name' => 'Dick', 'score' => 0]];
211
        $result = A::where($people, ['score' => 0]);
212
        $this->assertSame($expected, $result);
213
    }
214
215
    /**
216
     * Verifies that where() returns empty array when nothing matches
217
     *
218
     * @test
219
     * @covers ::where
220
     */
221
    public function whereReturnsEmptyArray()
222
    {
223
        $people = [
224
            ['name' => 'Tom', 'score' => '0'],
225
            ['name' => 'Dick', 'score' => 0],
226
            ['name' => 'Harry', 'score' => 0.0],
227
        ];
228
229
        $result = A::where($people, ['score' => false]);
230
        $this->assertSame([], $result);
231
    }
232
233
    /**
234
     * Verifies use of multiple conditions
235
     *
236
     * @test
237
     * @covers ::where
238
     */
239
    public function whereWithMultipleConditions()
240
    {
241
        $people = [
242
            ['name' => 'Tom', 'score' => 1, 'extra' => 'abc'],
243
            ['name' => 'Dick', 'score' => 1, 'extra' => false],
244
            ['name' => 'Dick', 'score' => 0, 'extra' => 123],
245
        ];
246
247
        $expected = [['name' => 'Dick', 'score' => 1, 'extra' => false]];
248
        $result = A::where($people, ['name' => 'Dick', 'score' => 1]);
249
        $this->assertSame($expected, $result);
250
    }
251
252
    /**
253
     * Verifies use of multiple conditions
254
     *
255
     * @test
256
     * @covers ::where
257
     */
258
    public function whereReturnsMultipleResults()
259
    {
260
        $array = [
261
            ['key 1' => 'a', 'key 2' => 'b'],
262
            ['key 1' => 'c', 'key 2' => 'd'],
263
            ['key 1' => 'a', 'key 2' => 'c'],
264
        ];
265
266
        $expected = [
267
            ['key 1' => 'a', 'key 2' => 'b'],
268
            ['key 1' => 'a', 'key 2' => 'c'],
269
        ];
270
271
        $result = A::where($array, ['key 1' => 'a']);
272
        $this->assertSame($expected, $result);
273
    }
274
275
    /**
276
     * @test
277
     * @covers ::where
278
     * @expectedException \InvalidArgumentException
279
     * @expectedExceptionMessage a value in $array was not an array
280
     */
281
    public function whereInputValueNotArray()
282
    {
283
        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...
284
    }
285
286
    /**
287
     * Verifies that embedInto works well with adding new items into an existing array.
288
     *
289
     * @test
290
     * @covers ::embedInto
291
     */
292
    public function embedIntoBasicUse()
293
    {
294
        $this->assertSame(
295
            [
296
                ['request' => ['image' => 'foo'], 'result' => ['exception' => 'exception 1']],
297
                ['request' => ['image' => 'bar'], 'result' => ['exception' => 'exception 2']],
298
            ],
299
            A::embedInto(
300
                [['exception' => 'exception 1'], ['exception' => 'exception 2']],
301
                'result',
302
                [['request' => ['image' => 'foo']], ['request' => ['image' => 'bar']]]
303
            )
304
        );
305
    }
306
307
    /**
308
     * Verifies that embedInto works well with creating new records.
309
     *
310
     * @test
311
     * @covers ::embedInto
312
     */
313
    public function embedIntoEmptyDestination()
314
    {
315
        $this->assertSame(
316
            [['request' => ['image' => 'foo']], ['request' => ['image' => 'bar']]],
317
            A::embedInto([['image' => 'foo'], ['image' => 'bar']], 'request')
318
        );
319
    }
320
321
    /**
322
     * Verifies that embedInto requires string for fieldname
323
     *
324
     * @test
325
     * @covers ::embedInto
326
     * @expectedException InvalidArgumentException
327
     * @expectedExceptionMessage $fieldName was not a string
328
     */
329
    public function embedIntoNumericFieldName()
330
    {
331
        A::embedInto([], 5);
332
    }
333
334
    /**
335
     * Verifies that embedInto requires destination entries to be arrays.
336
     *
337
     * @test
338
     * @covers ::embedInto
339
     * @expectedException InvalidArgumentException
340
     * @expectedExceptionMessage a value in $destination was not an array
341
     */
342
    public function embedIntoNonArrayDestinationItems()
343
    {
344
        A::embedInto(['one' => 0], 'result', ['one' => 0]);
345
    }
346
347
    /**
348
     * Verifies that embedInto refuses to overwrite field names.
349
     *
350
     * @test
351
     * @covers ::embedInto
352
     * @expectedException Exception
353
     */
354
    public function embedIntoExistingFieldName()
355
    {
356
        A::embedInto(['new'], 'result', [['result' => 'old']]);
357
    }
358
359
    /**
360
     * Verifies that embedInto does nothing with 0 items to embed.
361
     *
362
     * @test
363
     * @covers ::embedInto
364
     */
365
    public function embedIntoNoItems()
366
    {
367
        $this->assertSame([['result' => 'foo']], A::embedInto([], 'result', [['result' => 'foo']]));
368
    }
369
370
    /**
371
     * @test
372
     * @covers ::embedInto
373
     */
374
    public function embedIntoOverwrite()
375
    {
376
        $this->assertSame([['key' => true]], A::embedInto([true], 'key', [['key' => false]], true));
377
    }
378
379
    /**
380
     * @test
381
     * @covers ::embedInto
382
     * @expectedException \InvalidArgumentException
383
     * @expectedExceptionMessage $overwrite was not a bool
384
     */
385
    public function embedIntoOverwriteNotBool()
386
    {
387
        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...
388
    }
389
390
    /**
391
     * Basic usage of fillIfKeysExist()
392
     *
393
     * @test
394
     * @covers ::fillIfKeysExist
395
     */
396
    public function fillIfKeysExist()
397
    {
398
        $template = ['a' => null, 'b' => null, 'c' => null, 'd' => null, 'e' => null];
399
400
        $actual = A::fillIfKeysExist($template, ['a' => 1, 'c' => 1, 'e' => 1]);
401
402
        $expected = ['a' => 1, 'b' => null, 'c' => 1, 'd' => null, 'e' => 1];
403
404
        $this->assertSame($expected, $actual);
405
    }
406
407
    /**
408
     * Basic usage of extract()
409
     *
410
     * @test
411
     * @covers ::extract
412
     * @uses \DominionEnterprises\Util\Arrays::get
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
     * @uses \DominionEnterprises\Util\Arrays::get
435
     */
436 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...
437
    {
438
        $input = [
439
            ['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'],
440
            ['extra' => 123, 'key' => 'baz', 'value' => 'fez'],
441
            ['value' => 'duplicate1', 'extra' => true, 'key' => 'boo'],
442
            ['extra' => true, 'key' => 'noValue'],
443
            ['value' => 'duplicate2', 'extra' => true, 'key' => 'boo'],
444
        ];
445
446
        $expected = ['foo' => 'bar', 'baz' => 'fez', 'boo' => 'duplicate1', 'noValue' => null];
447
448
        $this->assertSame($expected, A::extract($input, 'key', 'value', 'takeFirst'));
449
    }
450
451
    /**
452
     * Basic usage of extract() with 'throw' option
453
     *
454
     * @test
455
     * @covers ::extract
456
     * @uses \DominionEnterprises\Util\Arrays::get
457
     * @expectedException \Exception
458
     * @expectedExceptionMessage Duplicate entry for 'boo' found.
459
     */
460
    public function extractThrowOnDuplicate()
461
    {
462
        $input = [
463
            ['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'],
464
            ['extra' => 123, 'key' => 'baz', 'value' => 'fez'],
465
            ['value' => 'duplicate1', 'extra' => true, 'key' => 'boo'],
466
            ['extra' => true, 'key' => 'noValue'],
467
            ['value' => 'duplicate2', 'extra' => true, 'key' => 'boo'],
468
        ];
469
470
        A::extract($input, 'key', 'value', 'throw');
471
    }
472
473
    /**
474
     * Verify behavior when a single dimensional array is given to extract().
475
     *
476
     * @test
477
     * @covers ::extract
478
     * @expectedException \InvalidArgumentException
479
     * @expectedExceptionMessage $arrays was not a multi-dimensional array
480
     */
481
    public function extractWithSingleDimensionalArray()
482
    {
483
        A::extract(['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'], 'key', 'value');
484
    }
485
486
    /**
487
     * Verify behavior when $arrays contain a invalid key value in the supplied $keyIndex field.
488
     *
489
     * @test
490
     * @covers ::extract
491
     * @uses \DominionEnterprises\Util\Arrays::get
492
     * @expectedException \UnexpectedValueException
493
     * @expectedExceptionMessage Value for $arrays[1][key] was not a string or integer
494
     */
495
    public function extractWithInvalidKeyValue()
496
    {
497
        $input = [
498
            ['key' => 'foo', 'value' => 'bar', 'extra' => 'abc'],
499
            ['extra' => 123, 'key' => [], 'value' => 'fez'],
500
        ];
501
502
        A::extract($input, 'key', 'value', 'throw');
503
    }
504
505
    /**
506
     * Verify behavior when $keyIndex is not a string or integer
507
     *
508
     * @test
509
     * @covers ::extract
510
     * @expectedException \InvalidArgumentException
511
     * @expectedExceptionMessage $keyIndex was not a string or integer
512
     */
513
    public function extractWithInvalidKeyIndex()
514
    {
515
        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...
516
    }
517
518
    /**
519
     * Verify behavior when $valueIndex is not a string or integer
520
     *
521
     * @test
522
     * @covers ::extract
523
     * @expectedException \InvalidArgumentException
524
     * @expectedExceptionMessage $valueIndex was not a string or integer
525
     */
526
    public function extractWithInvalidValueIndex()
527
    {
528
        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...
529
    }
530
531
    /**
532
     * Verify behavior when $duplicateBehavior is not valid
533
     *
534
     * @test
535
     * @covers ::extract
536
     * @expectedException \InvalidArgumentException
537
     * @expectedExceptionMessage $duplicateBehavior was not 'takeFirst', 'takeLast', or 'throw'
538
     */
539
    public function extractWithInvalidDuplicateBehavior()
540
    {
541
        A::extract([], 'key', 'value', 'invalid');
542
    }
543
544
    /**
545
     * Verify basic behavior of getFirstSet()
546
     *
547
     * @test
548
     * @covers ::getFirstSet
549
     */
550
    public function getFirstSet()
551
    {
552
        $this->assertSame('bar', A::getFirstSet(['foo', null, 'bar'], [1, 2]));
553
    }
554
555
    /**
556
     * Verify getFirstSet() returns default value
557
     *
558
     * @test
559
     * @covers ::getFirstSet
560
     */
561
    public function getFirstSetWithDefault()
562
    {
563
        $this->assertSame('baz', A::getFirstSet(['foo', null, 'bar'], [1, 4], 'baz'));
564
    }
565
566
    /**
567
     * Verifiy basic behavior of partition()
568
     *
569
     * @test
570
     * @covers ::partition
571
     */
572
    public function partition()
573
    {
574
        $this->assertSame([['a'], ['b'], ['c']], A::partition(['a', 'b', 'c'], 3));
575
    }
576
577
    /**
578
     * Verify partition() behavior when $input array contains less items than than $partitionCount.
579
     *
580
     * @test
581
     * @covers ::partition
582
     */
583
    public function partitionInputLessThanPartitionCount()
584
    {
585
        $this->assertSame([['a'], ['b'], ['c']], A::partition(['a', 'b', 'c'], 4));
586
    }
587
588
    /**
589
     * Verify remainder of $input array is front-loaded in partition().
590
     *
591
     * @test
592
     * @covers ::partition
593
     */
594
    public function partitionWithRemainder()
595
    {
596
        $this->assertSame([['a', 'b'], ['c'], ['d']], A::partition(['a', 'b', 'c', 'd'], 3));
597
    }
598
599
    /**
600
     * Verify remainder of $input array is front-loaded in partition().
601
     *
602
     * @test
603
     * @covers ::partition
604
     */
605
    public function partitionWithMultipleRemainder()
606
    {
607
        $this->assertSame([['a', 'b'], ['c', 'd'], ['e']], A::partition(['a', 'b', 'c', 'd', 'e'], 3));
608
    }
609
610
    /**
611
     * Verify partition() handles empty $input array.
612
     *
613
     * @test
614
     * @covers ::partition
615
     */
616
    public function partitionEmptyInput()
617
    {
618
        $this->assertSame([], A::partition([], 2));
619
    }
620
621
    /**
622
     * Verifiy behavior of partition() with $partitionCount of 1.
623
     *
624
     * @test
625
     * @covers ::partition
626
     */
627
    public function partitionOnePartition()
628
    {
629
        $this->assertSame([['a', 'b', 'c']], A::partition(['a', 'b', 'c'], 1));
630
    }
631
632
    /**
633
     * Verifiy partition() throws with negative $partitionCount.
634
     *
635
     * @test
636
     * @covers ::partition
637
     * @expectedException \InvalidArgumentException
638
     * @expectedExceptionMessage $partitionCount must be a positive integer
639
     */
640
    public function partitionNegativePartitionCount()
641
    {
642
        A::partition(['a', 'b', 'c'], -1);
643
    }
644
645
    /**
646
     * Verifiy partition() throws with 0 $partitionCount.
647
     *
648
     * @test
649
     * @covers ::partition
650
     * @expectedException \InvalidArgumentException
651
     * @expectedExceptionMessage $partitionCount must be a positive integer
652
     */
653
    public function partitionZeroPartitionCount()
654
    {
655
        A::partition(['a', 'b', 'c'], 0);
656
    }
657
658
    /**
659
     * Verifiy partition() throws with non-integer $partitionCount.
660
     *
661
     * @test
662
     * @covers ::partition
663
     * @expectedException \InvalidArgumentException
664
     * @expectedExceptionMessage $partitionCount must be a positive integer
665
     */
666
    public function partitionNonIntegerPartitionCount()
667
    {
668
        A::partition(['a', 'b', 'c'], 'not an int');
669
    }
670
671
    /**
672
     * Verifiy partition() preserves numeric keys.
673
     *
674
     * @test
675
     * @covers ::partition
676
     */
677
    public function partitionPreserveNumericKeys()
678
    {
679
        $this->assertSame(
680
            [[0 => 'a', 1 => 'b'], [2 => 'c', 3 => 'd'], [4 => 'e']],
681
            A::partition(['a', 'b', 'c', 'd', 'e'], 3, true)
682
        );
683
    }
684
685
    /**
686
     * Verifiy partition() preserves associative keys.
687
     *
688
     * @test
689
     * @covers ::partition
690
     */
691
    public function partitionPreserveAssociativeKeys()
692
    {
693
        $this->assertSame(
694
            [['a' => 0, 'b' => 1], ['c' => 2, 'd' => 3], ['e' => 4]],
695
            A::partition(['a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4], 3)
696
        );
697
    }
698
699
    /**
700
     * Verifiy partition() throws with non-boolean $preserveKeys.
701
     *
702
     * @test
703
     * @covers ::partition
704
     * @expectedException \InvalidArgumentException
705
     * @expectedExceptionMessage $preserveKeys must be a boolean value
706
     */
707
    public function partitionNonBoolPreserveKeys()
708
    {
709
        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...
710
    }
711
712
    /**
713
     * Verify basic behavior of unsetAll().
714
     *
715
     * @test
716
     * @covers ::unsetAll
717
     */
718
    public function unsetAll()
719
    {
720
        $array = ['a', 'b', 'c'];
721
        A::unsetAll($array, [0, 2]);
722
        $this->assertSame([1 => 'b'], $array);
723
    }
724
725
    /**
726
     * Verify behavior of unsetAll() with empty array.
727
     *
728
     * @test
729
     * @covers ::unsetAll
730
     */
731
    public function unsetAllEmptyArray()
732
    {
733
        $array = [];
734
        A::unsetAll($array, [0, 2]);
735
        // array unchanged
736
        $this->assertSame([], $array);
737
    }
738
739
    /**
740
     * Verify behavior of unsetAll() with empty keys.
741
     *
742
     * @test
743
     * @covers ::unsetAll
744
     */
745 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...
746
    {
747
        $array = ['a', 'b', 'c'];
748
        A::unsetAll($array, []);
749
        // array unchanged
750
        $this->assertSame(['a', 'b', 'c'], $array);
751
    }
752
753
    /**
754
     * Verify behavior of unsetAll() with keys that don't exist
755
     *
756
     * @test
757
     * @covers ::unsetAll
758
     */
759 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...
760
    {
761
        $array = ['a', 'b', 'c'];
762
        A::unsetAll($array, [3, 4]);
763
        // array unchanged
764
        $this->assertSame(['a', 'b', 'c'], $array);
765
    }
766
767
    /**
768
     * Verify basic behavior of nullifyEmptyStrings().
769
     * @test
770
     * @covers ::nullifyEmptyStrings
771
     */
772
    public function nullifyEmptyStrings()
773
    {
774
        $array = ['a' => '', 'b' => true, 'c' => "\n\t", 'd' => "\tstring with whitespace\n"];
775
        A::nullifyEmptyStrings($array);
776
        $this->assertSame(['a' => null, 'b' => true, 'c' => null, 'd' => "\tstring with whitespace\n"], $array);
777
    }
778
779
    /**
780
     * Verify behavior of nullifyEmptyStrings() with empty input.
781
     * @test
782
     * @covers ::nullifyEmptyStrings
783
     */
784
    public function nullifyEmptyStringsEmptyArray()
785
    {
786
        $array = [];
787
        A::nullifyEmptyStrings($array);
788
        $this->assertSame([], $array);
789
    }
790
791
    /**
792
     * Verify basic functionality of getNested.
793
     *
794
     * @test
795
     * @covers ::getNested
796
     *
797
     * @return void
798
     */
799
    public function getNested()
800
    {
801
        $array = ['db' => ['host' => 'localhost', 'login' => [ 'username' => 'scott', 'password' => 'tiger']]];
802
        $this->assertSame('scott', A::getNested($array, 'db.login.username'));
803
    }
804
805
    /**
806
     * Verify behavior when the given delimitedKey does not exist in the given array.
807
     *
808
     * @test
809
     * @covers ::getNested
810
     *
811
     * @return void
812
     */
813
    public function getNestedPathNotFound()
814
    {
815
        $array = ['db' => ['host' => 'localhost', 'login' => [ 'username' => 'scott', 'password' => 'tiger']]];
816
        $this->assertNull(A::getNested($array, 'db.notfound.username'));
817
    }
818
819
    /**
820
     * Verify functionality of changeKeyCase().
821
     *
822
     * @test
823
     * @covers ::changeKeyCase
824
     * @dataProvider changeKeyCaseData
825
     *
826
     * @return void
827
     */
828
    public function changeKeyCase($input, $case, $expected)
829
    {
830
        $this->assertSame($expected, A::changeKeyCase($input, $case));
831
    }
832
833
    /**
834
     * Dataprovider for changeKeyCase test.
835
     *
836
     * @return array
837
     */
838
    public function changeKeyCaseData()
839
    {
840
        $lowerUnderscore = [
841
            'first_and_last_name' => 'John Doe',
842
            'email_address' => '[email protected]',
843
            'age' => 35,
844
        ];
845
846
        $upperUnderscore = [
847
            'FIRST_AND_LAST_NAME' => 'John Doe',
848
            'EMAIL_ADDRESS' => '[email protected]',
849
            'AGE' => 35,
850
        ];
851
852
        $camelCaps = [
853
            'firstAndLastName' => 'John Doe',
854
            'emailAddress' => '[email protected]',
855
            'age' => 35,
856
        ];
857
858
        $underscore = [
859
            'first_And_Last_Name' => 'John Doe',
860
            'email_Address' => '[email protected]',
861
            'age' => 35,
862
        ];
863
864
        $lower = [
865
            'firstandlastname' => 'John Doe',
866
            'emailaddress' => '[email protected]',
867
            'age' => 35,
868
        ];
869
870
        $upper = [
871
            'FIRSTANDLASTNAME' => 'John Doe',
872
            'EMAILADDRESS' => '[email protected]',
873
            'AGE' => 35,
874
        ];
875
876
        return [
877
            'upper to lower' => [$upper, A::CASE_LOWER, $lower],
878
            'lower to upper' => [$lower, A::CASE_UPPER, $upper],
879
            'underscore to camel' => [$lowerUnderscore, A::CASE_CAMEL_CAPS, $camelCaps],
880
            'camel to underscore' => [$camelCaps, A::CASE_UNDERSCORE, $underscore],
881
            'camel to upper underscore' => [$camelCaps, A::CASE_UNDERSCORE | A::CASE_UPPER, $upperUnderscore],
882
            'camel to lower underscore' => [$camelCaps, A::CASE_UNDERSCORE | A::CASE_LOWER, $lowerUnderscore],
883
            'lower underscore to upper camel' => [$lowerUnderscore, A::CASE_CAMEL_CAPS | A::CASE_UPPER, $upper],
884
        ];
885
    }
886
887
    /**
888
     * Verify basic behavior of flatten().
889
     *
890
     * @test
891
     * @covers ::flatten
892
     *
893
     * @return void
894
     */
895 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...
896
    {
897
        $array = [
898
            'db' => [
899
                'host' => 'localhost',
900
                'login' => [
901
                    'username' => 'scott',
902
                    'password' => 'tiger'
903
                ]
904
            ]
905
        ];
906
907
        $expected = [
908
            'db.host' => 'localhost',
909
            'db.login.username' => 'scott',
910
            'db.login.password' => 'tiger',
911
        ];
912
913
        $this->assertSame($expected, A::flatten($array));
914
    }
915
916
    /**
917
     * Verify behavior of flatten() with custom delimiter.
918
     *
919
     * @test
920
     * @covers ::flatten
921
     *
922
     * @return void
923
     */
924 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...
925
    {
926
        $array = [
927
            'db' => [
928
                'host' => 'localhost',
929
                'login' => [
930
                    'username' => 'scott',
931
                    'password' => 'tiger'
932
                ]
933
            ]
934
        ];
935
936
        $expected = [
937
            'db/host' => 'localhost',
938
            'db/login/username' => 'scott',
939
            'db/login/password' => 'tiger',
940
        ];
941
942
        $this->assertSame($expected, A::flatten($array, '/'));
943
    }
944
945
    /**
946
     * @test
947
     * @covers ::getAllWhereKeyExists
948
     *
949
     * @return void
950
     */
951
    public function getAllWhereKeyExists()
952
    {
953
        $input = [
954
            [
955
                'id' => 1,
956
                'code' => 'foo',
957
                'extra' => 'abc',
958
            ],
959
            [
960
                'id' => 2,
961
                'code' => 'bar',
962
            ],
963
            [
964
                'id' => 3,
965
                'code' => 'baz',
966
                'extra' => 'xyz',
967
            ],
968
        ];
969
970
        $this->assertSame(
971
            [
972
                0 => [
973
                    'id' => 1,
974
                    'code' => 'foo',
975
                    'extra' => 'abc',
976
                ],
977
                2 => [
978
                    'id' => 3,
979
                    'code' => 'baz',
980
                    'extra' => 'xyz',
981
                ],
982
            ],
983
            A::getAllWhereKeyExists($input, 'extra')
984
        );
985
    }
986
}
987