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
25 changes: 25 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,31 @@ public function sole($key = null, $operator = null, $value = null)
return $items->first();
}

/**
* Get the first item in the collection, but if no items exist throw an exception.
*
* @param mixed $key
* @param mixed $operator
* @param mixed $value
* @return mixed
*
* @throws \Illuminate\Collections\ItemNotFoundException
*/
public function firstOrFail($key = null, $operator = null, $value = null)
{
$filter = func_num_args() > 1
? $this->operatorForWhere(...func_get_args())
: $key;

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

if ($items->isEmpty()) {
throw new ItemNotFoundException;
}

return $items->first();
}

/**
* Chunk the collection into chunks of the given size.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,30 @@ public function sole($key = null, $operator = null, $value = null)
->sole();
}

/**
* Get the first item in the collection, but if no items exist throw an exception.
*
* @param mixed $key
* @param mixed $operator
* @param mixed $value
* @return mixed
*
* @throws \Illuminate\Collections\ItemNotFoundException
*/
public function firstOrFail($key = null, $operator = null, $value = null)
{
$filter = func_num_args() > 1
? $this->operatorForWhere(...func_get_args())
: $key;

return $this
->when($filter)
powellblyth marked this conversation as resolved.
Show resolved Hide resolved
->filter($filter)
->take(1)
->collect()
->firstOrFail();
}

/**
* Chunk the collection into chunks of the given size.
*
Expand Down
89 changes: 87 additions & 2 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testSoleReturnsFirstItemInCollectionIfOnlyOneExists($collection)
/**
* @dataProvider collectionClassProvider
*/
public function testSoleThrowsExceptionIfNoItemsExists($collection)
public function testSoleThrowsExceptionIfNoItemsExist($collection)
{
$this->expectException(ItemNotFoundException::class);

Expand Down Expand Up @@ -129,7 +129,7 @@ public function testSoleReturnsFirstItemInCollectionIfOnlyOneExistsWithCallback(
/**
* @dataProvider collectionClassProvider
*/
public function testSoleThrowsExceptionIfNoItemsExistsWithCallback($collection)
public function testSoleThrowsExceptionIfNoItemsExistWithCallback($collection)
{
$this->expectException(ItemNotFoundException::class);

Expand All @@ -154,6 +154,91 @@ public function testSoleThrowsExceptionIfMoreThanOneItemExistsWithCallback($coll
});
}

/**
* @dataProvider collectionClassProvider
*/
public function testFirstOrFailReturnsFirstItemInCollection($collection)
{
$collection = new $collection([
['name' => 'foo'],
['name' => 'bar'],
]);

$this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->firstOrFail());
$this->assertSame(['name' => 'foo'], $collection->firstOrFail('name', '=', 'foo'));
$this->assertSame(['name' => 'foo'], $collection->firstOrFail('name', 'foo'));
}

/**
* @dataProvider collectionClassProvider
*/
public function testFirstOrFailThrowsExceptionIfNoItemsExist($collection)
{
$this->expectException(ItemNotFoundException::class);

$collection = new $collection([
['name' => 'foo'],
['name' => 'bar'],
]);

$collection->where('name', 'INVALID')->firstOrFail();
}

/**
* @dataProvider collectionClassProvider
*/
public function testFirstOrFailDoesntThrowExceptionIfMoreThanOneItemExists($collection)
{
$collection = new $collection([
['name' => 'foo'],
['name' => 'foo'],
['name' => 'bar'],
]);

$this->assertSame(['name' => 'foo'], $collection->where('name', 'foo')->firstOrFail());
}

/**
* @dataProvider collectionClassProvider
*/
public function testFirstOrFailReturnsFirstItemInCollectionIfOnlyOneExistsWithCallback($collection)
{
$data = new $collection(['foo', 'bar', 'baz']);
$result = $data->firstOrFail(function ($value) {
return $value === 'bar';
});
$this->assertSame('bar', $result);
}

/**
* @dataProvider collectionClassProvider
*/
public function testFirstOrFailThrowsExceptionIfNoItemsExistWithCallback($collection)
{
$this->expectException(ItemNotFoundException::class);

$data = new $collection(['foo', 'bar', 'baz']);

$data->firstOrFail(function ($value) {
return $value === 'invalid';
});
}

/**
* @dataProvider collectionClassProvider
*/
public function testFirstOrFailDoesntThrowExceptionIfMoreThanOneItemExistsWithCallback($collection)
{
$data = new $collection(['foo', 'bar', 'bar']);

$this->assertSame(
'bar',
$data->firstOrFail(function ($value) {
return $value === 'bar';
})
);
}

/**
* @dataProvider collectionClassProvider
*/
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
Member

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
Member

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
Member

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