-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trigger events in Model::createOrFirst
- Loading branch information
Showing
4 changed files
with
129 additions
and
37 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
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 |
---|---|---|
|
@@ -1052,15 +1052,20 @@ public function testCreateOrFirst() | |
{ | ||
Carbon::setTestNow('2010-06-22'); | ||
$createdAt = Carbon::now()->getTimestamp(); | ||
$events = []; | ||
self::registerModelEvents(User::class, $events); | ||
|
||
$user1 = User::createOrFirst(['email' => '[email protected]']); | ||
|
||
$this->assertSame('[email protected]', $user1->email); | ||
$this->assertNull($user1->name); | ||
$this->assertTrue($user1->wasRecentlyCreated); | ||
$this->assertEquals($createdAt, $user1->created_at->getTimestamp()); | ||
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp()); | ||
$this->assertEquals(['saving', 'creating', 'created', 'saved'], $events); | ||
|
||
Carbon::setTestNow('2020-12-28'); | ||
$events = []; | ||
$user2 = User::createOrFirst( | ||
['email' => '[email protected]'], | ||
['name' => 'John Doe', 'birthday' => new DateTime('1987-05-28')], | ||
|
@@ -1073,7 +1078,9 @@ public function testCreateOrFirst() | |
$this->assertFalse($user2->wasRecentlyCreated); | ||
$this->assertEquals($createdAt, $user1->created_at->getTimestamp()); | ||
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp()); | ||
$this->assertEquals(['saving', 'creating'], $events); | ||
|
||
$events = []; | ||
$user3 = User::createOrFirst( | ||
['email' => '[email protected]'], | ||
['name' => 'Jane Doe', 'birthday' => new DateTime('1987-05-28')], | ||
|
@@ -1086,14 +1093,17 @@ public function testCreateOrFirst() | |
$this->assertTrue($user3->wasRecentlyCreated); | ||
$this->assertEquals($createdAt, $user1->created_at->getTimestamp()); | ||
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp()); | ||
$this->assertEquals(['saving', 'creating', 'created', 'saved'], $events); | ||
|
||
$events = []; | ||
$user4 = User::createOrFirst( | ||
['name' => 'Robert Doe'], | ||
['name' => 'Maria Doe', 'email' => '[email protected]'], | ||
); | ||
|
||
$this->assertSame('Maria Doe', $user4->name); | ||
$this->assertTrue($user4->wasRecentlyCreated); | ||
$this->assertEquals(['saving', 'creating', 'created', 'saved'], $events); | ||
} | ||
|
||
public function testCreateOrFirstRequiresFilter() | ||
|
@@ -1114,6 +1124,9 @@ public function testUpdateOrCreate(array $criteria) | |
['email' => '[email protected]'], | ||
]); | ||
|
||
$events = []; | ||
self::registerModelEvents(User::class, $events); | ||
|
||
Carbon::setTestNow('2010-01-01'); | ||
$createdAt = Carbon::now()->getTimestamp(); | ||
|
||
|
@@ -1127,6 +1140,8 @@ public function testUpdateOrCreate(array $criteria) | |
$this->assertEquals(new DateTime('1987-05-28'), $user->birthday); | ||
$this->assertEquals($createdAt, $user->created_at->getTimestamp()); | ||
$this->assertEquals($createdAt, $user->updated_at->getTimestamp()); | ||
$this->assertEquals(['saving', 'creating', 'created', 'saved'], $events); | ||
$events = []; | ||
|
||
Carbon::setTestNow('2010-02-01'); | ||
$updatedAt = Carbon::now()->getTimestamp(); | ||
|
@@ -1142,6 +1157,7 @@ public function testUpdateOrCreate(array $criteria) | |
$this->assertEquals(new DateTime('1990-01-12'), $user->birthday); | ||
$this->assertEquals($createdAt, $user->created_at->getTimestamp()); | ||
$this->assertEquals($updatedAt, $user->updated_at->getTimestamp()); | ||
$this->assertEquals(['saving', 'saved'], $events); | ||
|
||
// Stored data | ||
$checkUser = User::where($criteria)->first(); | ||
|
@@ -1151,4 +1167,21 @@ public function testUpdateOrCreate(array $criteria) | |
$this->assertEquals($createdAt, $checkUser->created_at->getTimestamp()); | ||
$this->assertEquals($updatedAt, $checkUser->updated_at->getTimestamp()); | ||
} | ||
|
||
/** @param class-string<Model> $modelClass */ | ||
private static function registerModelEvents(string $modelClass, array &$events): void | ||
{ | ||
$modelClass::creating(function () use (&$events) { | ||
$events[] = 'creating'; | ||
}); | ||
$modelClass::created(function () use (&$events) { | ||
$events[] = 'created'; | ||
}); | ||
$modelClass::saving(function () use (&$events) { | ||
$events[] = 'saving'; | ||
}); | ||
$modelClass::saved(function () use (&$events) { | ||
$events[] = 'saved'; | ||
}); | ||
} | ||
} |