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

[8.x] Adds firstOrFail to Illuminate\Support\Collections and Illuminate\Support\LazyCollections #38420

Merged
merged 14 commits into from
Aug 19, 2021
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ public function firstOrFail($key = null, $operator = null, $value = null)
? $this->operatorForWhere(...func_get_args())
: $key;

$items = $this->unless($filter == null)->filter($filter);
$items = $this->when($filter)->filter($filter);
powellblyth marked this conversation as resolved.
Show resolved Hide resolved

if ($items->isEmpty()) {
throw new ItemNotFoundException;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ public function firstOrFail($key = null, $operator = null, $value = null)
: $key;

return $this
->unless($filter == null)
->when($filter)
powellblyth marked this conversation as resolved.
Show resolved Hide resolved
->filter($filter)
->take(1)
->collect()
Expand Down
28 changes: 28 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Support;

use Illuminate\Collections\ItemNotFoundException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #38449

use Illuminate\Collections\MultipleItemsFoundException;
use Illuminate\Support\LazyCollection;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -986,6 +987,33 @@ public function testSliceIsLazy()
});
}

public function testFindFirstOrFailIsLazy()
{
$this->assertEnumerates(1, function ($collection) {
try {
$collection->firstOrFail();
} catch (ItemNotFoundException $e) {
//
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you expect this to throw? Why?

});

$this->assertEnumerates(1, function ($collection) {
$collection->firstOrFail(function ($item) {
return $item === 1;
});
});

$this->assertEnumerates(2, function ($collection) {
try {
$collection->firstOrFail(function ($item) {
return $item % 2 === 0;
});
} catch (ItemNotFoundException $e) {
//
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Why would this fail?

And really, you're not testing an actual failure, and ensuring it enumerates the whole collection once.

});
}

public function testSomeIsLazy()
{
$this->assertEnumerates(5, function ($collection) {
Expand Down