@@ -401,6 +401,16 @@ public function testCursorPaginatedModelCollectionRetrievalWhenNoElementsAndDefa
401
401
$ this ->assertInstanceOf (CursorPaginator::class, $ models );
402
402
}
403
403
404
+ public function testFirstOrNew ()
405
+ {
406
+ $ user1 = EloquentTestUser::firstOrNew (
407
+ ['name ' => 'Dries Vints ' ],
408
+ ['name ' => 'Nuno Maduro ' ]
409
+ );
410
+
411
+ $ this ->assertSame ('Nuno Maduro ' , $ user1 ->name );
412
+ }
413
+
404
414
public function testFirstOrCreate ()
405
415
{
406
416
$ user1 = EloquentTestUser::firstOrCreate (['email ' => 'taylorotwell@gmail.com ' ]);
@@ -425,6 +435,13 @@ public function testFirstOrCreate()
425
435
$ this ->assertNotEquals ($ user3 ->id , $ user1 ->id );
426
436
$ this ->assertSame ('abigailotwell@gmail.com ' , $ user3 ->email );
427
437
$ this ->assertSame ('Abigail Otwell ' , $ user3 ->name );
438
+
439
+ $ user4 = EloquentTestUser::firstOrCreate (
440
+ ['name ' => 'Dries Vints ' ],
441
+ ['name ' => 'Nuno Maduro ' , 'email ' => 'nuno@laravel.com ' ]
442
+ );
443
+
444
+ $ this ->assertSame ('Nuno Maduro ' , $ user4 ->name );
428
445
}
429
446
430
447
public function testUpdateOrCreate ()
@@ -678,6 +695,36 @@ public function testBasicModelHydration()
678
695
$ this ->assertCount (1 , $ models );
679
696
}
680
697
698
+ public function testFirstOrNewOnHasOneRelationShip ()
699
+ {
700
+ $ user1 = EloquentTestUser::create (['email ' => 'taylorotwell@gmail.com ' ]);
701
+ $ post1 = $ user1 ->post ()->firstOrNew (['name ' => 'First Post ' ], ['name ' => 'New Post ' ]);
702
+
703
+ $ this ->assertSame ('New Post ' , $ post1 ->name );
704
+
705
+ $ user2 = EloquentTestUser::create (['email ' => 'abigailotwell@gmail.com ' ]);
706
+ $ post = $ user2 ->post ()->create (['name ' => 'First Post ' ]);
707
+ $ post2 = $ user2 ->post ()->firstOrNew (['name ' => 'First Post ' ], ['name ' => 'New Post ' ]);
708
+
709
+ $ this ->assertSame ('First Post ' , $ post2 ->name );
710
+ $ this ->assertSame ($ post ->id , $ post2 ->id );
711
+ }
712
+
713
+ public function testFirstOrCreateOnHasOneRelationShip ()
714
+ {
715
+ $ user1 = EloquentTestUser::create (['email ' => 'taylorotwell@gmail.com ' ]);
716
+ $ post1 = $ user1 ->post ()->firstOrCreate (['name ' => 'First Post ' ], ['name ' => 'New Post ' ]);
717
+
718
+ $ this ->assertSame ('New Post ' , $ post1 ->name );
719
+
720
+ $ user2 = EloquentTestUser::create (['email ' => 'abigailotwell@gmail.com ' ]);
721
+ $ post = $ user2 ->post ()->create (['name ' => 'First Post ' ]);
722
+ $ post2 = $ user2 ->post ()->firstOrCreate (['name ' => 'First Post ' ], ['name ' => 'New Post ' ]);
723
+
724
+ $ this ->assertSame ('First Post ' , $ post2 ->name );
725
+ $ this ->assertSame ($ post ->id , $ post2 ->id );
726
+ }
727
+
681
728
public function testHasOnSelfReferencingBelongsToManyRelationship ()
682
729
{
683
730
$ user = EloquentTestUser::create (['email ' => 'taylorotwell@gmail.com ' ]);
0 commit comments