10
10
use Illuminate \Database \Eloquent \Collection as EloquentCollection ;
11
11
use Illuminate \Database \Eloquent \ModelNotFoundException ;
12
12
use Illuminate \Support \Facades \Date ;
13
+ use Illuminate \Support \Facades \DB ;
13
14
use Illuminate \Support \Str ;
14
- use InvalidArgumentException ;
15
15
use MongoDB \BSON \Binary ;
16
16
use MongoDB \BSON \ObjectID ;
17
17
use MongoDB \BSON \UTCDateTime ;
@@ -48,7 +48,7 @@ class ModelTest extends TestCase
48
48
public function tearDown (): void
49
49
{
50
50
Carbon::setTestNow ();
51
- User:: truncate ();
51
+ DB :: connection ( ' mongodb ' )-> getCollection ( ' users ' )-> drop ();
52
52
Soft::truncate ();
53
53
Book::truncate ();
54
54
Item::truncate ();
@@ -1050,17 +1050,25 @@ public function testNumericFieldName(): void
1050
1050
1051
1051
public function testCreateOrFirst ()
1052
1052
{
1053
+ DB ::connection ('mongodb ' )
1054
+ ->getCollection ('users ' )
1055
+ ->createIndex (['email ' => 1 ], ['unique ' => true ]);
1056
+
1053
1057
Carbon::setTestNow ('2010-06-22 ' );
1054
1058
$ createdAt = Carbon::now ()->getTimestamp ();
1059
+ $ events = [];
1060
+ self ::registerModelEvents (User::class, $ events );
1055
1061
$ user1 = User::createOrFirst (['email ' => 'john.doe@example.com ' ]);
1056
1062
1057
1063
$ this ->assertSame ('john.doe@example.com ' , $ user1 ->email );
1058
1064
$ this ->assertNull ($ user1 ->name );
1059
1065
$ this ->assertTrue ($ user1 ->wasRecentlyCreated );
1060
1066
$ this ->assertEquals ($ createdAt , $ user1 ->created_at ->getTimestamp ());
1061
1067
$ this ->assertEquals ($ createdAt , $ user1 ->updated_at ->getTimestamp ());
1068
+ $ this ->assertEquals (['saving ' , 'creating ' , 'created ' , 'saved ' ], $ events );
1062
1069
1063
1070
Carbon::setTestNow ('2020-12-28 ' );
1071
+ $ events = [];
1064
1072
$ user2 = User::createOrFirst (
1065
1073
['email ' => 'john.doe@example.com ' ],
1066
1074
['name ' => 'John Doe ' , 'birthday ' => new DateTime ('1987-05-28 ' )],
@@ -1073,7 +1081,9 @@ public function testCreateOrFirst()
1073
1081
$ this ->assertFalse ($ user2 ->wasRecentlyCreated );
1074
1082
$ this ->assertEquals ($ createdAt , $ user1 ->created_at ->getTimestamp ());
1075
1083
$ this ->assertEquals ($ createdAt , $ user1 ->updated_at ->getTimestamp ());
1084
+ $ this ->assertEquals (['saving ' , 'creating ' ], $ events );
1076
1085
1086
+ $ events = [];
1077
1087
$ user3 = User::createOrFirst (
1078
1088
['email ' => 'jane.doe@example.com ' ],
1079
1089
['name ' => 'Jane Doe ' , 'birthday ' => new DateTime ('1987-05-28 ' )],
@@ -1086,21 +1096,17 @@ public function testCreateOrFirst()
1086
1096
$ this ->assertTrue ($ user3 ->wasRecentlyCreated );
1087
1097
$ this ->assertEquals ($ createdAt , $ user1 ->created_at ->getTimestamp ());
1088
1098
$ this ->assertEquals ($ createdAt , $ user1 ->updated_at ->getTimestamp ());
1099
+ $ this ->assertEquals (['saving ' , 'creating ' , 'created ' , 'saved ' ], $ events );
1089
1100
1101
+ $ events = [];
1090
1102
$ user4 = User::createOrFirst (
1091
1103
['name ' => 'Robert Doe ' ],
1092
1104
['name ' => 'Maria Doe ' , 'email ' => 'maria.doe@example.com ' ],
1093
1105
);
1094
1106
1095
1107
$ this ->assertSame ('Maria Doe ' , $ user4 ->name );
1096
1108
$ this ->assertTrue ($ user4 ->wasRecentlyCreated );
1097
- }
1098
-
1099
- public function testCreateOrFirstRequiresFilter ()
1100
- {
1101
- $ this ->expectException (InvalidArgumentException::class);
1102
- $ this ->expectExceptionMessage ('You must provide attributes to check for duplicates ' );
1103
- User::createOrFirst ([]);
1109
+ $ this ->assertEquals (['saving ' , 'creating ' , 'created ' , 'saved ' ], $ events );
1104
1110
}
1105
1111
1106
1112
#[TestWith([['_id ' => new ObjectID ()]])]
@@ -1116,6 +1122,8 @@ public function testUpdateOrCreate(array $criteria)
1116
1122
1117
1123
Carbon::setTestNow ('2010-01-01 ' );
1118
1124
$ createdAt = Carbon::now ()->getTimestamp ();
1125
+ $ events = [];
1126
+ self ::registerModelEvents (User::class, $ events );
1119
1127
1120
1128
// Create
1121
1129
$ user = User::updateOrCreate (
@@ -1127,11 +1135,13 @@ public function testUpdateOrCreate(array $criteria)
1127
1135
$ this ->assertEquals (new DateTime ('1987-05-28 ' ), $ user ->birthday );
1128
1136
$ this ->assertEquals ($ createdAt , $ user ->created_at ->getTimestamp ());
1129
1137
$ this ->assertEquals ($ createdAt , $ user ->updated_at ->getTimestamp ());
1130
-
1138
+ $ this ->assertEquals (['saving ' , 'creating ' , 'created ' , 'saved ' ], $ events );
1139
+ $ this ->assertEquals (['saving ' , 'creating ' , 'created ' , 'saved ' ], $ events );
1131
1140
Carbon::setTestNow ('2010-02-01 ' );
1132
1141
$ updatedAt = Carbon::now ()->getTimestamp ();
1133
1142
1134
1143
// Update
1144
+ $ events = [];
1135
1145
$ user = User::updateOrCreate (
1136
1146
$ criteria ,
1137
1147
['birthday ' => new DateTime ('1990-01-12 ' ), 'foo ' => 'bar ' ],
@@ -1159,13 +1169,20 @@ public function testCreateWithNullId()
1159
1169
$ this ->assertSame (1 , User::count ());
1160
1170
}
1161
1171
1162
- public function testUpdateOrCreateWithNullId ()
1172
+ /** @param class-string<Model> $modelClass */
1173
+ private static function registerModelEvents (string $ modelClass , array &$ events ): void
1163
1174
{
1164
- $ this ->expectException (InvalidArgumentException::class);
1165
- $ this ->expectExceptionMessage ('You must provide attributes to check for duplicates ' );
1166
- User::updateOrCreate (
1167
- ['_id ' => null ],
1168
- ['email ' => 'jane.doe@example.com ' ],
1169
- );
1175
+ $ modelClass ::creating (function () use (&$ events ) {
1176
+ $ events [] = 'creating ' ;
1177
+ });
1178
+ $ modelClass ::created (function () use (&$ events ) {
1179
+ $ events [] = 'created ' ;
1180
+ });
1181
+ $ modelClass ::saving (function () use (&$ events ) {
1182
+ $ events [] = 'saving ' ;
1183
+ });
1184
+ $ modelClass ::saved (function () use (&$ events ) {
1185
+ $ events [] = 'saved ' ;
1186
+ });
1170
1187
}
1171
1188
}
0 commit comments