Skip to content

Commit

Permalink
Add a test for the firstOrNew method when record exists
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Sep 26, 2023
1 parent 8492996 commit 202cc98
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/Integration/Database/EloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ public function testFirstOrNewOnMissingRecord()
$this->assertSame('Tony', $user1->name);
}

public function testFirstOrNewWhenRecordExists()
{
$taylor = User::create(['name' => 'Taylor', 'slug' => 'taylor']);
$team = Team::create(['owner_id' => $taylor->id]);
$existingTony = $team->members()->create(['name' => 'Tony Messias', 'slug' => 'tony']);

$newTony = $taylor->teamMates()->firstOrNew(
['slug' => 'tony'],
['name' => 'Tony', 'team_id' => $team->id],
);

$this->assertTrue($newTony->exists);
$this->assertEquals($team->id, $newTony->team_id);
$this->assertSame('tony', $newTony->slug);
$this->assertSame('Tony Messias', $newTony->name);

$this->assertTrue($existingTony->is($newTony));
$this->assertSame('tony', $existingTony->refresh()->slug);
$this->assertSame('Tony Messias', $existingTony->name);
}

public function testFirstOrCreateWhenModelDoesntExist()
{
$owner = User::create(['name' => 'Taylor']);
Expand Down

0 comments on commit 202cc98

Please sign in to comment.