Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eskiesirius's avatar

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

https://prnt.sc/kNGKUl1sbQ6A

0 likes
12 replies
eskiesirius's avatar
/** @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',
];
MohamedTammam's avatar

@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'),
    ]);
kokoshneta's avatar

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's avatar

@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.

Please or to participate in this conversation.