Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add additional test cases for Arr helper to enhance coverage #54298

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}