-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x]
firstOrCreate
and firstOrNew
should merge attributes correc…
…tly (#38346) * firstOrCreate and firstOrNew should merge attributes correctly * Fix tests * Update HasOneOrMany
- Loading branch information
Showing
3 changed files
with
51 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -401,6 +401,16 @@ public function testCursorPaginatedModelCollectionRetrievalWhenNoElementsAndDefa | |
$this->assertInstanceOf(CursorPaginator::class, $models); | ||
} | ||
|
||
public function testFirstOrNew() | ||
{ | ||
$user1 = EloquentTestUser::firstOrNew( | ||
['name' => 'Dries Vints'], | ||
['name' => 'Nuno Maduro'] | ||
); | ||
|
||
$this->assertSame('Nuno Maduro', $user1->name); | ||
} | ||
|
||
public function testFirstOrCreate() | ||
{ | ||
$user1 = EloquentTestUser::firstOrCreate(['email' => '[email protected]']); | ||
|
@@ -425,6 +435,13 @@ public function testFirstOrCreate() | |
$this->assertNotEquals($user3->id, $user1->id); | ||
$this->assertSame('[email protected]', $user3->email); | ||
$this->assertSame('Abigail Otwell', $user3->name); | ||
|
||
$user4 = EloquentTestUser::firstOrCreate( | ||
['name' => 'Dries Vints'], | ||
['name' => 'Nuno Maduro', 'email' => '[email protected]'] | ||
); | ||
|
||
$this->assertSame('Nuno Maduro', $user4->name); | ||
} | ||
|
||
public function testUpdateOrCreate() | ||
|
@@ -678,6 +695,36 @@ public function testBasicModelHydration() | |
$this->assertCount(1, $models); | ||
} | ||
|
||
public function testFirstOrNewOnHasOneRelationShip() | ||
{ | ||
$user1 = EloquentTestUser::create(['email' => '[email protected]']); | ||
$post1 = $user1->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']); | ||
|
||
$this->assertSame('New Post', $post1->name); | ||
|
||
$user2 = EloquentTestUser::create(['email' => '[email protected]']); | ||
$post = $user2->post()->create(['name' => 'First Post']); | ||
$post2 = $user2->post()->firstOrNew(['name' => 'First Post'], ['name' => 'New Post']); | ||
|
||
$this->assertSame('First Post', $post2->name); | ||
$this->assertSame($post->id, $post2->id); | ||
} | ||
|
||
public function testFirstOrCreateOnHasOneRelationShip() | ||
{ | ||
$user1 = EloquentTestUser::create(['email' => '[email protected]']); | ||
$post1 = $user1->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']); | ||
|
||
$this->assertSame('New Post', $post1->name); | ||
|
||
$user2 = EloquentTestUser::create(['email' => '[email protected]']); | ||
$post = $user2->post()->create(['name' => 'First Post']); | ||
$post2 = $user2->post()->firstOrCreate(['name' => 'First Post'], ['name' => 'New Post']); | ||
|
||
$this->assertSame('First Post', $post2->name); | ||
$this->assertSame($post->id, $post2->id); | ||
} | ||
|
||
public function testHasOnSelfReferencingBelongsToManyRelationship() | ||
{ | ||
$user = EloquentTestUser::create(['email' => '[email protected]']); | ||
|