← All writing

Freeze time when testing scheduled publication

Laravel 13 / PHP 8.3+Sources checked 2026-09-14

A publication test passes most days but fails near midnight. The test depends on the actual wall clock instead of stating the exact instant it wants to examine. Freeze time and test the boundary explicitly.

Make the publication instant part of the fixture

$this->travelTo(\Carbon\Carbon::parse('2026-10-01 09:00:00', 'UTC'));
// Create one article published now and one at 09:00:01 UTC.
// Assert the first is visible and the second is not.
$this->travelBack();

This is a Laravel test fragment. Use your application's model creation helpers and public route assertions. Ensure time is restored even when you organize several cases within the same test; framework time helpers and isolated tests make that easier.

Test every public surface

A scheduled post should remain absent from its detail route, listing, search results, sitemap, and previous/next links before release. Hiding it only from the homepage does not keep it unpublished.

At the exact publication timestamp, decide whether the comparison is inclusive. Most timestamp-based publication uses published_at <= now(). Test that instant, one second before it, and a null date representing a draft.

Keep stored timestamps and display timezones distinct. A reader can see a local date label while the query compares UTC instants. If a cron job only plans dates and the public query handles visibility, publication does not need a worker to flip a boolean every minute. Document that design so an operator knows which parts require cron and which do not.

Reference

Official documentation.