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

jva91's avatar

\Illuminate\Support\Facades\Schedule::events() returns empty array in tests when using ->withSchedule()

Im testing scheduled commands if they are registered correctly on a given time in different test cases in different files.

File A:

#[Test]
public function commandIsScheduled(): void
{
    /** @var CallbackEvent|null $event */
    $event = (new Collection(\Illuminate\Support\Facades\Schedule::events()))
        ->filter(function (Event $event) {
            if (!is_string($event->command)) {
                return false;
            }
            return str_contains($event->command, 'company:anonymize');
        })
        ->first();

    if ($event === null) {
        $this->fail('No event found');
    }

    $this->assertEquals('0 20 * * *', $event->expression);
}

File B

#[Test]
public function jobIsScheduled(): void
{
    /** @var CallbackEvent|null $event */
    $event = (new Collection(Schedule::events()))
        ->filter(fn(Event $event) => $event->description === AnonymizableCompanyReminderJob::class)
        ->first();

    if ($event === null) {
        $this->fail('No event found');
    }

    $this->assertEquals('40 0 * * *', $event->expression);
}

When in another testcase in a different file calling \Illuminate\Support\Facades\Schedule::events() again this leads to a empty array.

The schedule commands are registered in bootstrap/app.php

->withSchedule(function (\Illuminate\Console\Scheduling\Schedule $schedule): void {
    $schedule->command(DeleteUnusedMembers::class)
        ->daily()
        ->sentryMonitor();
    $schedule->job(SevenDayApplicationReminderJob::class)
        ->dailyAt('08:00')
        ->sentryMonitor('seven day application reminder');
    ...
})

When im using the routes/console.php file this seems not to happen

Anyone knows how this can be fixed?

0 likes
0 replies

Please or to participate in this conversation.