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] Introduce firstOrFail for Database Query Builder #51381

Closed
wants to merge 9 commits into from
23 changes: 23 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Database\Query\Processors\MySqlProcessor;
use Illuminate\Database\Query\Processors\PostgresProcessor;
use Illuminate\Database\Query\Processors\Processor;
use Illuminate\Database\RecordNotFoundException;
use Illuminate\Pagination\AbstractPaginator as Paginator;
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
Expand Down Expand Up @@ -2758,6 +2759,28 @@ public function testFirstMethodReturnsFirstResult()
$this->assertEquals(['foo' => 'bar'], $results);
}

public function testFirstOrFailMethodReturnsFirstResult()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([['foo' => 'bar']]);
$builder->getProcessor()->shouldReceive('processSelect')->once()->with($builder, [['foo' => 'bar']])->andReturnUsing(function ($query, $results) {
return $results;
});
$results = $builder->from('users')->where('id', '=', 1)->firstOrFail();
$this->assertEquals(['foo' => 'bar'], $results);
}

public function testFirstOrFailMethodThrowsRecordNotFoundException()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('select')->once()->with('select * from "users" where "id" = ? limit 1', [1], true)->andReturn([]);

$this->expectException(RecordNotFoundException::class);
$this->expectExceptionMessage("No record found for the given query.");
Copy link
Contributor

Choose a reason for hiding this comment

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

improve code style: (using single quotation)

$this->expectExceptionMessage('No record found for the given query.');


$builder->from('users')->where('id', '=', 1)->firstOrFail();
}

public function testPluckMethodGetsCollectionOfColumnValues()
{
$builder = $this->getBuilder();
Expand Down