Test if a command is scheduled and executed at the expected time #41816
Replies: 4 comments
-
https://laracasts.com/discuss/channels/testing/unit-testing-task-scheduling?page=1&replyId=712537 offers running the https://gist.github.com/MattApril/e6f4b8af32eb6438f482d307280d6a79 - another solution, walking through the events - absolutely not an elegant way. |
Beta Was this translation helpful? Give feedback.
-
It would be great to have something built-it to test that the command is scheduled for a particular time. I understand that the actual scheduler is already tested by the framework but I would like to test that someone has not inadvertently removed or messed-up the actual schedule in the application I'm working on. |
Beta Was this translation helpful? Give feedback.
-
It would be very nice to have something like this build-in. |
Beta Was this translation helpful? Give feedback.
-
This is not perfect but it works for now. I put the macros inline for readability, you should throw these in a provider or in your test case setup. The time provided should be exactly like in the Schedule::command()->dailyAt(), of course this doesn't cover all the cases but you can't cover all of them only if you pass the cron yourself. Otherwise just create more macros or modify this to pass a cron directly. it('schedules commands', function () {
Schedule::macro('assertScheduled', function (string $command, array $parameters = []) {
if (class_exists($command)) {
$command = \Illuminate\Console\Application::formatCommandString(app($command)->getName());
}else {
$command = \Illuminate\Console\Application::formatCommandString($command);
}
if (count($parameters)) {
$command .= ' '.$this->compileParameters($parameters);
}
$events = array_filter($this->events, function ($event) use ($command) {
return $event->command === $command;
});
\PHPUnit\Framework\Assert::assertTrue(count($events) > 0);
});
Schedule::assertScheduled(Test::class, [1]);
});
it('schedules commands at specific time', function () {
Schedule::macro('assertScheduledAt', function (string $time, string $command, array $parameters = []) {
if (class_exists($command)) {
$command = \Illuminate\Console\Application::formatCommandString(app($command)->getName());
}else {
$command = \Illuminate\Console\Application::formatCommandString($command);
}
if (count($parameters)) {
$command .= ' '.$this->compileParameters($parameters);
}
$eventAt = new \Illuminate\Console\Scheduling\Event($this->eventMutex, $command, $this->timezone);
$eventAt->at($time);
$events = array_filter($this->events, function ($event) use ($command, $eventAt) {
return $event->command === $command && $event->expression === $eventAt->expression;
});
\PHPUnit\Framework\Assert::assertTrue(count($events) > 0);
});
Schedule::assertScheduledAt('16:00', Test::class, [1]);
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
There is no way to test if a scheduled command will be executed in time.
What about
Your ideas on how to make it better are welcome.
Beta Was this translation helpful? Give feedback.
All reactions