@@ -45,7 +45,7 @@ class ModelTest extends TestCase
45
45
{
46
46
public function tearDown (): void
47
47
{
48
- User::truncate ( );
48
+ User::raw ( fn ( Collection $ collection ) => $ collection -> drop () );
49
49
Soft::truncate ();
50
50
Book::truncate ();
51
51
Item::truncate ();
@@ -1044,4 +1044,43 @@ public function testNumericFieldName(): void
1044
1044
$ this ->assertInstanceOf (User::class, $ found );
1045
1045
$ this ->assertEquals ([3 => 'two.three ' ], $ found [2 ]);
1046
1046
}
1047
+
1048
+ public function testCreateOrFirst ()
1049
+ {
1050
+ // Create index unique on "email" and "name"
1051
+ User::raw (fn (Collection $ collection ) => $ collection ->createIndex (['email ' => 1 ], ['unique ' => true ]));
1052
+ User::raw (fn (Collection $ collection ) => $ collection ->createIndex (['name ' => 1 ], ['unique ' => true ]));
1053
+
1054
+ $ user1 = User::
createOrFirst ([
'email ' =>
'[email protected] ' ]);
1055
+
1056
+ $ this ->
assertSame (
'[email protected] ' ,
$ user1->
email );
1057
+ $ this ->assertNull ($ user1 ->name );
1058
+
1059
+ $ user2 = User::createOrFirst (
1060
+
1061
+ ['name ' => 'Taylor Otwell ' , 'birthday ' => new DateTime ('1987-05-28 ' )],
1062
+ );
1063
+
1064
+ $ this ->assertEquals ($ user1 ->id , $ user2 ->id );
1065
+ $ this ->
assertSame (
'[email protected] ' ,
$ user2->
email );
1066
+ $ this ->assertNull ($ user2 ->name );
1067
+ $ this ->assertNull ($ user2 ->birthday );
1068
+
1069
+ $ user3 = User::createOrFirst (
1070
+
1071
+ ['name ' => 'Abigail Otwell ' , 'birthday ' => new DateTime ('1987-05-28 ' )],
1072
+ );
1073
+
1074
+ $ this ->assertNotEquals ($ user3 ->id , $ user1 ->id );
1075
+ $ this ->
assertSame (
'[email protected] ' ,
$ user3->
email );
1076
+ $ this ->assertSame ('Abigail Otwell ' , $ user3 ->name );
1077
+ $ this ->assertEquals (new DateTime ('1987-05-28 ' ), $ user3 ->birthday );
1078
+
1079
+ $ user4 = User::createOrFirst (
1080
+ ['name ' => 'Dries Vints ' ],
1081
+ [
'name ' =>
'Nuno Maduro ' ,
'email ' =>
'[email protected] ' ],
1082
+ );
1083
+
1084
+ $ this ->assertSame ('Nuno Maduro ' , $ user4 ->name );
1085
+ }
1047
1086
}
0 commit comments