Configure and Test Laravel Task Scheduling

后端 未结 3 1533
栀梦
栀梦 2020-12-17 16:10

Environment

  • Laravel Version : 5.1.45 (LTS)

  • PHP Version : 5.6.1


Description

3条回答
  •  醉酒成梦
    2020-12-17 16:27

    If you want to unit test the scheduling of events you can use this example. It is based on the default inspire command:

    public function testIsAvailableInTheScheduler()
    {
        /** @var \Illuminate\Console\Scheduling\Schedule $schedule */
        $schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class);
    
        $events = collect($schedule->events())->filter(function (\Illuminate\Console\Scheduling\Event $event) {
            return stripos($event->command, 'YourCommandHere');
        });
    
        if ($events->count() == 0) {
            $this->fail('No events found');
        }
    
        $events->each(function (\Illuminate\Console\Scheduling\Event $event) {
            // This example is for hourly commands.
            $this->assertEquals('0 * * * * *', $event->expression);
        });
    }
    

提交回复
热议问题