Skip to content

Commit

Permalink
[11.x] Add additional test cases for Arr helper to enhance coverage (#…
Browse files Browse the repository at this point in the history
…54298)

* Add additional test cases for Arr helper

* styleci

* styleci

---------

Co-authored-by: cuong.tt <[email protected]>
  • Loading branch information
mrvipchien and cuong.tt authored Jan 22, 2025
1 parent b89006c commit d065f94
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public function testAdd()
$this->assertEquals(['developer' => ['name' => 'Ferid']], Arr::add([], 'developer.name', 'Ferid'));
$this->assertEquals([1 => 'hAz'], Arr::add([], 1, 'hAz'));
$this->assertEquals([1 => [1 => 'hAz']], Arr::add([], 1.1, 'hAz'));

// Case where the key already exists
$this->assertEquals(['type' => 'Table'], Arr::add(['type' => 'Table'], 'type', 'Chair'));
$this->assertEquals(['category' => ['type' => 'Table']], Arr::add(['category' => ['type' => 'Table']], 'category.type', 'Chair'));
}

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

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

// Case with empty two-dimensional arrays
$emptyArray = [[], [], []];
Expand Down Expand Up @@ -504,6 +508,10 @@ public function testGet()
$this->assertSame('dayle', Arr::get($array, 'names.otherDeveloper', function () {
return 'dayle';
}));

// Test array has a null key
$this->assertSame('bar', Arr::get(['' => 'bar'], ''));
$this->assertSame('bar', Arr::get(['' => ['' => 'bar']], '.'));
}

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

$this->assertFalse(Arr::isList([-1 => 1]));
$this->assertFalse(Arr::isList([-1 => 1, 0 => 2]));
$this->assertFalse(Arr::isList([1 => 'foo', 'bar']));
$this->assertFalse(Arr::isList([1 => 'foo', 0 => 'bar']));
$this->assertFalse(Arr::isList([0 => 'foo', 'bar' => 'baz']));
Expand All @@ -632,6 +642,17 @@ public function testOnly()
$array = Arr::only($array, ['name', 'price']);
$this->assertEquals(['name' => 'Desk', 'price' => 100], $array);
$this->assertEmpty(Arr::only($array, ['nonExistingKey']));

$this->assertEmpty(Arr::only($array, null));

// Test with array having numeric keys
$this->assertEquals(['foo'], Arr::only(['foo', 'bar', 'baz'], 0));
$this->assertEquals([1 => 'bar', 2 => 'baz'], Arr::only(['foo', 'bar', 'baz'], [1, 2]));
$this->assertEmpty(Arr::only(['foo', 'bar', 'baz'], [3]));

// Test with array having numeric key and string key
$this->assertEquals(['foo'], Arr::only(['foo', 'bar' => 'baz'], 0));
$this->assertEquals(['bar' => 'baz'], Arr::only(['foo', 'bar' => 'baz'], 'bar'));
}

public function testPluck()
Expand Down Expand Up @@ -1501,5 +1522,15 @@ public function testSelect()
'name' => 'Abigail',
],
], Arr::select($array, 'name'));

$this->assertEquals([
[],
[],
], Arr::select($array, 'nonExistingKey'));

$this->assertEquals([
[],
[],
], Arr::select($array, null));
}
}

0 comments on commit d065f94

Please sign in to comment.