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

[10.x] Improve tests for Arr::first and Arr::last #48511

Merged
merged 4 commits into from
Sep 25, 2023
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
40 changes: 34 additions & 6 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,13 @@ public function testFirst()
}, function () {
return 'baz';
});
$value5 = Arr::first($array, function ($value, $key) {
return $key < 2;
});
$this->assertNull($value2);
$this->assertSame('bar', $value3);
$this->assertSame('baz', $value4);
$this->assertEquals(100, $value5);
}

public function testJoin()
Expand All @@ -229,17 +233,41 @@ public function testLast()
{
$array = [100, 200, 300];

$last = Arr::last($array, function ($value) {
// Callback is null and array is empty
$this->assertNull(Arr::last([], null));
$this->assertSame('foo', Arr::last([], null, 'foo'));
$this->assertSame('bar', Arr::last([], null, function () {
return 'bar';
}));

// Callback is null and array is not empty
$this->assertEquals(300, Arr::last($array));

// Callback is not null and array is not empty
$value = Arr::last($array, function ($value) {
return $value < 250;
});
$this->assertEquals(200, $last);
$this->assertEquals(200, $value);

$last = Arr::last($array, function ($value, $key) {
// Callback is not null, array is not empty but no satisfied item
$value2 = Arr::last($array, function ($value) {
return $value > 300;
});
$value3 = Arr::last($array, function ($value) {
return $value > 300;
}, 'bar');
$value4 = Arr::last($array, function ($value) {
return $value > 300;
}, function () {
return 'baz';
});
$value5 = Arr::last($array, function ($value, $key) {
return $key < 2;
});
$this->assertEquals(200, $last);

$this->assertEquals(300, Arr::last($array));
$this->assertNull($value2);
$this->assertSame('bar', $value3);
$this->assertSame('baz', $value4);
$this->assertEquals(200, $value5);
}

public function testFlatten()
Expand Down