Environment
Laravel Version : 5.1.45 (LTS)
PHP Version : 5.6.1
Description
I'm trying to run a command every 1 minute using Laravel Task Scheduling.
Attempt
I've added this line to my cron tab file
* * * * * php artisan schedule:run >> /dev/null 2>&1
Here is my /app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
\App\Console\Commands\Inspire::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
$schedule->command('echo "Happy New Year!" ')->everyMinute(); //<---- ADD HERE }
}
I've added this line $schedule->command('echo "Happy New Year!" ')->everyMinute();
Question
How do I test this ?
How do I trigger my echo to display ?
How do I know if what I did is not wrong ?
I'm opening to any suggestions at this moment.
Any hints / suggestions / helps on this be will be much appreciated !
If you want to unit test the scheduling of events you can use this example. It is based on the default inspire command:
Building on Michiel's answer, I've used the methods contained in
Illuminate\Console\Scheduling\Event
to test if the event is due to run for a given date.I've mocked the current date using
Carbon::setTestNow()
so that any date based logic in thewhen()
andskip()
filters will behave as expected.command()
runs an artisan command. What you're trying to achieve - issuing a command to the OS - is done byexec('echo "Happy New Year!"')
Testing depends on what you want to test:
In this case, you don't have to. It is tested in the original framework code.
Well, you can manually run
php artisan schedule:run
and see the output.The scheduler does not produce any output on default (
>> /dev/null 2>&1
). You can, however, redirect the output of the runned scripts to any file by chainingwriteOutputTo()
orappendOutputTo()
(https://laravel.com/docs/5.1/scheduling#task-output).For more complex logic, write a console command instead (https://laravel.com/docs/5.1/artisan#writing-commands) and use
command()
- this way you can write nice, testable code.