-
Notifications
You must be signed in to change notification settings - Fork 11.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Feat: Add
firstOrInsert
method to Query Builder with tests
This commit introduces a new `firstOrInsert` method to the Laravel Query Builder. This method efficiently retrieves the first record matching the given attributes, or inserts a new record with the provided attributes and values if no record is found. It includes comprehensive unit tests to ensure its functionality and reliability. This enhancement provides a convenient way to perform "first or insert" operations directly within the query builder, improving developer workflow and code conciseness.
- Loading branch information
1 parent
122e5c2
commit 7e44ebb
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4141,6 +4141,72 @@ public function testUpdateOrInsertMethodWorksWithEmptyUpdateValues() | |
$builder->shouldNotHaveReceived('update'); | ||
} | ||
|
||
public function testFirstOrInsertMethodRecordExists() | ||
{ | ||
$builder = m::mock(Builder::class.'[where,first,insert]', [ | ||
m::mock(ConnectionInterface::class), | ||
new Grammar, | ||
m::mock(Processor::class), | ||
]); | ||
|
||
$existingRecord = new stdClass(); | ||
$existingRecord->name = 'Existing User'; | ||
$existingRecord->email = '[email protected]'; | ||
|
||
$builder->shouldReceive('where') | ||
->once() | ||
->with(['email' => '[email protected]']) | ||
->andReturn(m::self()); | ||
|
||
$builder->shouldReceive('first') | ||
->once() | ||
->andReturn($existingRecord); | ||
|
||
$builder->shouldNotReceive('insert'); | ||
|
||
$result = $builder->firstOrInsert(['email' => '[email protected]'], ['name' => 'New Name']); | ||
|
||
$this->assertInstanceOf(stdClass::class, $result); | ||
$this->assertEquals('Existing User', $result->name); | ||
$this->assertEquals('[email protected]', $result->email); | ||
} | ||
|
||
public function testFirstOrInsertMethodRecordDoesNotExist() | ||
{ | ||
$builder = m::mock(Builder::class.'[where,first,insert]', [ | ||
m::mock(ConnectionInterface::class), | ||
new Grammar, | ||
m::mock(Processor::class), | ||
]); | ||
|
||
$insertedRecord = new stdClass(); | ||
$insertedRecord->name = 'New User'; | ||
$insertedRecord->email = '[email protected]'; | ||
|
||
$builder->shouldReceive('where') | ||
->twice() | ||
->with(['email' => '[email protected]']) | ||
->andReturn(m::self()); | ||
|
||
$builder->shouldReceive('first') | ||
->once() | ||
->andReturn(null); | ||
|
||
$builder->shouldReceive('insert') | ||
->once() | ||
->with(['email' => '[email protected]', 'name' => 'New User']) | ||
->andReturn(true); | ||
|
||
$builder->shouldReceive('first') | ||
->once()->andReturn($insertedRecord); | ||
|
||
$result = $builder->firstOrInsert(['email' => '[email protected]'], ['name' => 'New User']); | ||
|
||
$this->assertInstanceOf(stdClass::class, $result); | ||
$this->assertEquals('New User', $result->name); | ||
$this->assertEquals('[email protected]', $result->email); | ||
} | ||
|
||
public function testDeleteMethod() | ||
{ | ||
$builder = $this->getBuilder(); | ||
|