Skip to content

Commit 27d10b3

Browse files
committed
Add tests on timestamps and non-fillable field
1 parent bd1d2ae commit 27d10b3

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

tests/ModelTest.php

+18-11
Original file line numberDiff line numberDiff line change
@@ -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' => '[email protected]'],
1113+
['email' => '[email protected]'],
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' => '[email protected]', 'birthday' => new DateTime('1987-05-28')],
11161123
);
11171124
$this->assertInstanceOf(User::class, $user);
11181125
$this->assertEquals('[email protected]', $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('[email protected]', $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('[email protected]', $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

Comments
 (0)