@@ -1102,45 +1102,52 @@ public function testCreateOrFirstRequiresFilter()
1102
1102
User::createOrFirst ([]);
1103
1103
}
1104
1104
1105
- #[TestWith([new ObjectID ()])]
1106
- #[TestWith(['foo ' ])]
1107
- public function testUpdateOrCreate (mixed $ id )
1105
+ #[TestWith([[ ' _id ' => new ObjectID ()] ])]
1106
+ #[TestWith([[ 'foo ' => ' bar ' ] ])]
1107
+ public function testUpdateOrCreate (array $ criteria )
1108
1108
{
1109
+ // Insert data to ensure we filter on the correct criteria, and not getting
1110
+ // the first document randomly.
1111
+ User::insert ([
1112
+
1113
+
1114
+ ]);
1115
+
1109
1116
Carbon::setTestNow ('2010-01-01 ' );
1110
- // $createdAt = Carbon::now()->getTimestamp();
1117
+ $ createdAt = Carbon::now ()->getTimestamp ();
1111
1118
1112
1119
// Create
1113
1120
$ user = User::updateOrCreate (
1114
- [ ' _id ' => $ id ] ,
1121
+ $ criteria ,
1115
1122
[
'email ' =>
'[email protected] ' ,
'birthday ' =>
new DateTime (
'1987-05-28 ' )],
1116
1123
);
1117
1124
$ this ->assertInstanceOf (User::class, $ user );
1118
1125
$ this ->
assertEquals (
'[email protected] ' ,
$ user->
email );
1119
1126
$ this ->assertEquals (new DateTime ('1987-05-28 ' ), $ user ->birthday );
1120
- // $this->assertEquals($createdAt, $user->created_at->getTimestamp());
1121
- // $this->assertEquals($createdAt, $user->updated_at->getTimestamp());
1127
+ $ this ->assertEquals ($ createdAt , $ user ->created_at ->getTimestamp ());
1128
+ $ this ->assertEquals ($ createdAt , $ user ->updated_at ->getTimestamp ());
1122
1129
1123
1130
Carbon::setTestNow ('2010-02-01 ' );
1124
1131
$ updatedAt = Carbon::now ()->getTimestamp ();
1125
1132
1126
1133
// Update
1127
1134
$ user = User::updateOrCreate (
1128
- [ ' _id ' => $ id ] ,
1135
+ $ criteria ,
1129
1136
['birthday ' => new DateTime ('1990-01-12 ' ), 'foo ' => 'bar ' ],
1130
1137
);
1131
1138
1132
1139
$ this ->assertInstanceOf (User::class, $ user );
1133
1140
$ this ->
assertEquals (
'[email protected] ' ,
$ user->
email );
1134
1141
$ this ->assertEquals (new DateTime ('1990-01-12 ' ), $ user ->birthday );
1135
- // $this->assertEquals($createdAt, $user->created_at->getTimestamp());
1142
+ $ this ->assertEquals ($ createdAt , $ user ->created_at ->getTimestamp ());
1136
1143
$ this ->assertEquals ($ updatedAt , $ user ->updated_at ->getTimestamp ());
1137
1144
1138
1145
// Stored data
1139
- $ checkUser = User::find ( $ id )->first ();
1146
+ $ checkUser = User::where ( $ criteria )->first ();
1140
1147
$ this ->assertInstanceOf (User::class, $ checkUser );
1141
1148
$ this ->
assertEquals (
'[email protected] ' ,
$ checkUser->
email );
1142
1149
$ this ->assertEquals (new DateTime ('1990-01-12 ' ), $ checkUser ->birthday );
1143
- // $this->assertEquals($createdAt, $user ->created_at->getTimestamp());
1150
+ $ this ->assertEquals ($ createdAt , $ checkUser ->created_at ->getTimestamp ());
1144
1151
$ this ->assertEquals ($ updatedAt , $ checkUser ->updated_at ->getTimestamp ());
1145
1152
}
1146
1153
}
0 commit comments