Skip to content

Commit 6a09858

Browse files
test firstOrCreate method for the model
1 parent f4c448f commit 6a09858

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tests/ModelTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,23 @@ public function testGuardedModel()
750750
$model->fill(['level1' => $dataValues]);
751751
$this->assertEquals($dataValues, $model->getAttribute('level1'));
752752
}
753+
754+
public function testFirstOrCreate(): void
755+
{
756+
$name = 'Jane Poe';
757+
758+
/** @var User $user */
759+
$user = User::where('name', $name)->first();
760+
$this->assertNull($user);
761+
762+
/** @var User $user */
763+
$user = User::firstOrCreate(compact('name'));
764+
$this->assertInstanceOf(Model::class, $user);
765+
$this->assertTrue($user->exists);
766+
$this->assertEquals($name, $user->name);
767+
768+
/** @var User $check */
769+
$check = User::where('name', $name)->first();
770+
$this->assertEquals($user->_id, $check->_id);
771+
}
753772
}

0 commit comments

Comments
 (0)