-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
PHPORM-180 Keep createOrFirst in 2 commands to simplify implementation #2984
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -10,8 +10,8 @@ | |
use Illuminate\Database\Eloquent\Collection as EloquentCollection; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Support\Facades\Date; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Str; | ||
use InvalidArgumentException; | ||
use MongoDB\BSON\Binary; | ||
use MongoDB\BSON\ObjectID; | ||
use MongoDB\BSON\UTCDateTime; | ||
|
@@ -48,7 +48,7 @@ class ModelTest extends TestCase | |
public function tearDown(): void | ||
{ | ||
Carbon::setTestNow(); | ||
User::truncate(); | ||
DB::connection('mongodb')->getCollection('users')->drop(); | ||
Soft::truncate(); | ||
Book::truncate(); | ||
Item::truncate(); | ||
|
@@ -1048,19 +1048,34 @@ public function testNumericFieldName(): void | |
$this->assertEquals([3 => 'two.three'], $found[2]); | ||
} | ||
|
||
public function testCreateOrFirst() | ||
#[TestWith([true])] | ||
#[TestWith([false])] | ||
public function testCreateOrFirst(bool $transaction) | ||
{ | ||
$connection = DB::connection('mongodb'); | ||
$connection | ||
->getCollection('users') | ||
->createIndex(['email' => 1], ['unique' => true]); | ||
|
||
if ($transaction) { | ||
$connection->beginTransaction(); | ||
} | ||
|
||
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 = []; | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$user2 = User::createOrFirst( | ||
['email' => '[email protected]'], | ||
['name' => 'John Doe', 'birthday' => new DateTime('1987-05-28')], | ||
|
@@ -1073,7 +1088,17 @@ public function testCreateOrFirst() | |
$this->assertFalse($user2->wasRecentlyCreated); | ||
$this->assertEquals($createdAt, $user1->created_at->getTimestamp()); | ||
$this->assertEquals($createdAt, $user1->updated_at->getTimestamp()); | ||
if ($transaction) { | ||
// In a transaction, firstOrCreate is used instead. | ||
// Since a document is found, "save" is not called. | ||
$this->assertEquals([], $events); | ||
} else { | ||
// The "duplicate key error" exception interrupts the save process | ||
// before triggering "created" and "saved". Consistent with Laravel | ||
$this->assertEquals(['saving', 'creating'], $events); | ||
} | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$events = []; | ||
$user3 = User::createOrFirst( | ||
['email' => '[email protected]'], | ||
['name' => 'Jane Doe', 'birthday' => new DateTime('1987-05-28')], | ||
|
@@ -1086,21 +1111,21 @@ 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); | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function testCreateOrFirstRequiresFilter() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('You must provide attributes to check for duplicates'); | ||
User::createOrFirst([]); | ||
if ($transaction) { | ||
$connection->commit(); | ||
} | ||
} | ||
|
||
#[TestWith([['_id' => new ObjectID()]])] | ||
|
@@ -1116,6 +1141,8 @@ public function testUpdateOrCreate(array $criteria) | |
|
||
Carbon::setTestNow('2010-01-01'); | ||
$createdAt = Carbon::now()->getTimestamp(); | ||
$events = []; | ||
self::registerModelEvents(User::class, $events); | ||
|
||
// Create | ||
$user = User::updateOrCreate( | ||
|
@@ -1127,11 +1154,12 @@ 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); | ||
Carbon::setTestNow('2010-02-01'); | ||
$updatedAt = Carbon::now()->getTimestamp(); | ||
|
||
// Update | ||
$events = []; | ||
$user = User::updateOrCreate( | ||
$criteria, | ||
['birthday' => new DateTime('1990-01-12'), 'foo' => 'bar'], | ||
|
@@ -1142,6 +1170,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', 'updating', 'updated', 'saved'], $events); | ||
|
||
// Stored data | ||
$checkUser = User::where($criteria)->first(); | ||
|
@@ -1159,13 +1188,26 @@ public function testCreateWithNullId() | |
$this->assertSame(1, User::count()); | ||
} | ||
|
||
public function testUpdateOrCreateWithNullId() | ||
/** @param class-string<Model> $modelClass */ | ||
private static function registerModelEvents(string $modelClass, array &$events): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('You must provide attributes to check for duplicates'); | ||
User::updateOrCreate( | ||
['_id' => null], | ||
['email' => '[email protected]'], | ||
); | ||
$modelClass::creating(function () use (&$events) { | ||
$events[] = 'creating'; | ||
}); | ||
$modelClass::created(function () use (&$events) { | ||
$events[] = 'created'; | ||
}); | ||
$modelClass::updating(function () use (&$events) { | ||
$events[] = 'updating'; | ||
}); | ||
$modelClass::updated(function () use (&$events) { | ||
$events[] = 'updated'; | ||
}); | ||
$modelClass::saving(function () use (&$events) { | ||
$events[] = 'saving'; | ||
}); | ||
$modelClass::saved(function () use (&$events) { | ||
$events[] = 'saved'; | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not actually suggesting this, but it's amusing to me that this entire method body could be reduced to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but it won't help me to use xdebug's step-by-step debugger.