Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

"Fake" Time to the Future/Past

Premium
4 min read

Let's talk about how to fake time in a test. What do I mean by that?

What if you want to test the situation that would happen in the future or should have occurred in the past depending on some, for example, database field like published_at or created_at?

You can set the "fake" time to run a specific test method.


Scenario Example

For example, you have published_at in the database table and want to show only published records on your home page.

Schema::table('products', function (Blueprint $table) {
$table->dateTime('published_at')->nullable()->after('price');
});

Then, in the Model, you have an optional scope of published.

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
 
class Product extends Model
{
use HasFactory;
 
protected $fillable = [
'name',
'price',
'published_at',
];
 
public function scopePublished(Builder $query): Builder
{
return $query->whereNotNull('published_at')
->where('published_at', '<=', now());
}
}

And, in the Controller, you get only the published records.

use App\Models\Product;
use Illuminate\View\View;
 
class ProductController extends Controller
{
public function index(): View
{
$products = Product::published()->get();
 
return view('products.index', compact('products'));
}
 
// ...
}

The Test

Then, in the test, you create...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…