Can you please share the test code.
Weird Assertion on Date
When doing tests, sometimes my test fails because the date changes its format when doing database assertion but when i try to rerun the test.. everything works.. the links shows the test results
/** @test */
public function test_update_voucher_status_after_adding_domain()
{
Carbon::setTestNow();
$this->loginAsAdmin();
$voucher = Voucher::factory()->create();
$this->mock(Client::class, function (MockInterface $mock) {
$mock->shouldReceive('registerDomain')
->once()
->andReturn('192.168.1.1');
});
$this->mock(CertVerification::class, function (MockInterface $mock) {
$mock->shouldReceive('passes')->andReturn(true);
});
$this->post(route('domains.store'), [
'domain_url' => 'google.com',
'domain_ip' => '192.168.1.2',
'port' => 443,
'public_key' => new UploadedFile(base_path('tests/Certificates/fullchain.pem'), 'fullchain.pem', null, null, true),
'private_key' => new UploadedFile(base_path('tests/Certificates/privkey.pem'), 'privkey.pem', null, null, true),
'voucher' => $voucher->code,
]);
$this->assertDatabaseHas('vouchers', [
'id' => $voucher->id,
'domain_name' => 'google.com',
'status' => Voucher::USED,
'used_at' => now(),
'expired_at' => now()->addMonths($voucher->duration),
]);
}
I casted expired_at and used_at as dates
/**
* @var array
*/
protected $dates = [
'expired_at', 'used_at',
];
@eskiesirius are you using RefreshDatabase trait?
And maybe, if you you omit the milliseconds part it will work.
$this->assertDatabaseHas('vouchers', [
'id' => $voucher->id,
'domain_name' => 'google.com',
'status' => Voucher::USED,
'used_at' => now()->format('Y-m-d H:i:s'),
'expired_at' => now()->addMonths($voucher->duration)->format('Y-m-d H:i:s'),
]);
@MohamedTammam yes i am using RefreshDatabase and there is no milliseconds.. only seconds
@eskiesirius I see milliseconds in the screenshot.
@MohamedTammam sorry i got confused but the question is, why is it when i run the test again.. it works
@eskiesirius Maybe because the time in milliseconds between the record creation and the checking.
@MohamedTammam as you can see @kokoshneta 's comment.. they have 8-hours gap
There’s an eight-hour difference between the timestamps in the test and the ones in the database. Does your database use the same time zone as the server?
@kokoshneta im testing it on my local using laravel sail
@eskiesirius I don’t really use Docker, so I don’t know how time zones work there, but if the system time that PHP gets (i.e., what now() returns) is different from the database time (i.e., what SELECT CURRENT_TIME() returns), then that might explain it. Note that MySQL uses UTC time by default, so if your localhost/container is set to your local time zone somewhere in East Asia, they’ll probably be different.
You should be able to test it this way:
$systime = now();
$dbtime = DB::raw('SELECT CURRENT_TIMESTAMP() as time')->pluck('time');
dd($systime, $dbtime);
If those are eight hours apart, that’s your issue.
@kokoshneta the question is why when I run the test again, it works? as you can see in the screenshot
Please or to participate in this conversation.