Skip to content

Instantly share code, notes, and snippets.

@micc83
Created May 13, 2021 08:19
Show Gist options
  • Save micc83/555c37ed4af8ae7afc21692e2b80bb70 to your computer and use it in GitHub Desktop.
Save micc83/555c37ed4af8ae7afc21692e2b80bb70 to your computer and use it in GitHub Desktop.
Assert a given command is scheduled once on Laravel Task Scheduling
<?php
namespace Tests;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Support\Str;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function assertScheduledOnce(string $command, string $cronExpression): void
{
$scheduler = $this->app->make(Schedule::class);
$events = collect($scheduler->events())->filter(
fn (Event $event) => Str::contains($event->command, $command)
);
$this->assertCount(1, $events);
/** @var Event $event */
$event = $events->first();
$this->assertEquals($cronExpression, $event->expression);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment