Skip to content

Commit d065f94

Browse files
mrvipchiencuong.tt
and
cuong.tt
authored
[11.x] Add additional test cases for Arr helper to enhance coverage (#54298)
* Add additional test cases for Arr helper * styleci * styleci --------- Co-authored-by: cuong.tt <[email protected]>
1 parent b89006c commit d065f94

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

tests/Support/SupportArrTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function testAdd()
3939
$this->assertEquals(['developer' => ['name' => 'Ferid']], Arr::add([], 'developer.name', 'Ferid'));
4040
$this->assertEquals([1 => 'hAz'], Arr::add([], 1, 'hAz'));
4141
$this->assertEquals([1 => [1 => 'hAz']], Arr::add([], 1.1, 'hAz'));
42+
43+
// Case where the key already exists
44+
$this->assertEquals(['type' => 'Table'], Arr::add(['type' => 'Table'], 'type', 'Chair'));
45+
$this->assertEquals(['category' => ['type' => 'Table']], Arr::add(['category' => ['type' => 'Table']], 'category.type', 'Chair'));
4246
}
4347

4448
public function testCollapse()
@@ -48,8 +52,8 @@ public function testCollapse()
4852
$this->assertEquals(['foo', 'bar', 'baz'], Arr::collapse($data));
4953

5054
// Case including numeric and string elements
51-
$array = [[1], [2], [3], ['foo', 'bar'], collect(['baz', 'boom'])];
52-
$this->assertEquals([1, 2, 3, 'foo', 'bar', 'baz', 'boom'], Arr::collapse($array));
55+
$array = [[1], [2], [3], ['foo', 'bar']];
56+
$this->assertEquals([1, 2, 3, 'foo', 'bar'], Arr::collapse($array));
5357

5458
// Case with empty two-dimensional arrays
5559
$emptyArray = [[], [], []];
@@ -504,6 +508,10 @@ public function testGet()
504508
$this->assertSame('dayle', Arr::get($array, 'names.otherDeveloper', function () {
505509
return 'dayle';
506510
}));
511+
512+
// Test array has a null key
513+
$this->assertSame('bar', Arr::get(['' => 'bar'], ''));
514+
$this->assertSame('bar', Arr::get(['' => ['' => 'bar']], '.'));
507515
}
508516

509517
public function testHas()
@@ -619,6 +627,8 @@ public function testIsList()
619627
$this->assertTrue(Arr::isList([0 => 'foo', 'bar']));
620628
$this->assertTrue(Arr::isList([0 => 'foo', 1 => 'bar']));
621629

630+
$this->assertFalse(Arr::isList([-1 => 1]));
631+
$this->assertFalse(Arr::isList([-1 => 1, 0 => 2]));
622632
$this->assertFalse(Arr::isList([1 => 'foo', 'bar']));
623633
$this->assertFalse(Arr::isList([1 => 'foo', 0 => 'bar']));
624634
$this->assertFalse(Arr::isList([0 => 'foo', 'bar' => 'baz']));
@@ -632,6 +642,17 @@ public function testOnly()
632642
$array = Arr::only($array, ['name', 'price']);
633643
$this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
634644
$this->assertEmpty(Arr::only($array, ['nonExistingKey']));
645+
646+
$this->assertEmpty(Arr::only($array, null));
647+
648+
// Test with array having numeric keys
649+
$this->assertEquals(['foo'], Arr::only(['foo', 'bar', 'baz'], 0));
650+
$this->assertEquals([1 => 'bar', 2 => 'baz'], Arr::only(['foo', 'bar', 'baz'], [1, 2]));
651+
$this->assertEmpty(Arr::only(['foo', 'bar', 'baz'], [3]));
652+
653+
// Test with array having numeric key and string key
654+
$this->assertEquals(['foo'], Arr::only(['foo', 'bar' => 'baz'], 0));
655+
$this->assertEquals(['bar' => 'baz'], Arr::only(['foo', 'bar' => 'baz'], 'bar'));
635656
}
636657

637658
public function testPluck()
@@ -1501,5 +1522,15 @@ public function testSelect()
15011522
'name' => 'Abigail',
15021523
],
15031524
], Arr::select($array, 'name'));
1525+
1526+
$this->assertEquals([
1527+
[],
1528+
[],
1529+
], Arr::select($array, 'nonExistingKey'));
1530+
1531+
$this->assertEquals([
1532+
[],
1533+
[],
1534+
], Arr::select($array, null));
15041535
}
15051536
}

0 commit comments

Comments
 (0)