@@ -1102,45 +1102,52 @@ public function testCreateOrFirstRequiresFilter()
11021102 User::createOrFirst ([]);
11031103 }
11041104
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 )
11081108 {
1109+ // Insert data to ensure we filter on the correct criteria, and not getting
1110+ // the first document randomly.
1111+ User::insert ([
1112+ ['email ' => 'fixture@example.com ' ],
1113+ ['email ' => 'john.doe@example.com ' ],
1114+ ]);
1115+ 11091116 Carbon::setTestNow ('2010年01月01日 ' );
1110- // $createdAt = Carbon::now()->getTimestamp();
1117+ $ createdAt = Carbon::now ()->getTimestamp ();
11111118
11121119 // Create
11131120 $ user = User::updateOrCreate (
1114- [ ' _id ' => $ id ] ,
1121+ $ criteria ,
11151122 ['email ' => 'john.doe@example.com ' , 'birthday ' => new DateTime ('1987年05月28日 ' )],
11161123 );
11171124 $ this ->assertInstanceOf (User::class, $ user );
11181125 $ this ->assertEquals ('john.doe@example.com ' , $ user ->email );
11191126 $ 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 ());
11221129
11231130 Carbon::setTestNow ('2010年02月01日 ' );
11241131 $ updatedAt = Carbon::now ()->getTimestamp ();
11251132
11261133 // Update
11271134 $ user = User::updateOrCreate (
1128- [ ' _id ' => $ id ] ,
1135+ $ criteria ,
11291136 ['birthday ' => new DateTime ('1990年01月12日 ' ), 'foo ' => 'bar ' ],
11301137 );
11311138
11321139 $ this ->assertInstanceOf (User::class, $ user );
11331140 $ this ->assertEquals ('john.doe@example.com ' , $ user ->email );
11341141 $ 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 ());
11361143 $ this ->assertEquals ($ updatedAt , $ user ->updated_at ->getTimestamp ());
11371144
11381145 // Stored data
1139- $ checkUser = User::find ( $ id )->first ();
1146+ $ checkUser = User::where ( $ criteria )->first ();
11401147 $ this ->assertInstanceOf (User::class, $ checkUser );
11411148 $ this ->assertEquals ('john.doe@example.com ' , $ checkUser ->email );
11421149 $ 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 ());
11441151 $ this ->assertEquals ($ updatedAt , $ checkUser ->updated_at ->getTimestamp ());
11451152 }
11461153}
0 commit comments