How to test cron in laravel?

1

I've created a cron for sending emails every time. But how to test the operation?

Code

<?php

namespace App\Console\Commands;
namespace App\Console\Commands\EnvioEmailBlCron;

use Illuminate\Console\Command;

class envioEmailBIcron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'envioEmailBI:cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command Email';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       //Pegar os dados no banco
        $sql = ' select * from payments as P, receipts as R ';
        $sql .= ' where P.created_at < CURRENT_DATE AND P.created_at < CURRENT_DATE -1';
        $sql .= ' OR R.created_at < CURRENT_DATE AND R.created_at < CURRENT_DATE -1';

        //pega os dados no banco
        $dados = \DB::select($sql);

        //envio email
        Mail::send('emails.BI', $dados, function ($message) {
            $message->to(Input::get('email'));
        }); 


       // executando as funções de envio de e-mail
       $this->info('Example Cron comando rodando com êxito');
    }
}

ConsoleKernel.php

<?php

namespace App\Units;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel;

class ConsoleKernel extends Kernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //Commands\Inspire::class,
        Commands\EnvioEmailBICron::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     */
    protected function schedule(Schedule $schedule)
    {

        //configuração do cron
        $schedule->command('inspire')->dailyAt('01:00'); 
        $schedule->command('EnvioEmailBICron:cron')->daily(); // email diários
    }

    /**
     * Register the Closure based commands for the application.
     */
    protected function commands()
    {
    }
}
    
asked by anonymous 25.10.2017 / 21:06

1 answer

3

In your command class you have the command that should be triggered as follows:

class ExampleCron extends Command
{

    protected $signature = 'envioEmailBI:cron';

    protected $description = 'Command E-mail';

    public function __construct()
    {
        parent::__construct();
    }
    // aqui você coloca a lógica do seu processo
    // pode utilizar todos os recursos do Laravel
    public function handle()
    {
        //Pegar os dados no banco
         $sql = ' select * from payments as P, receipts as R ';
         $sql .= ' where P.created_at < CURRENT_DATE AND P.created_at < CURRENT_DATE -1';
         $sql .= ' OR R.created_at < CURRENT_DATE AND R.created_at < CURRENT_DATE -1';

         //pega os dados no banco
         $dados = \DB::select($sql);

         //envio email
         Mail::send('emails.BI', $dados, function ($message) {
            $message->to(Input::get('email'));
         }); 


        // executando as funções de envio de e-mail
        $this->info('Example Cron comando rodando com êxito');
    }
}

Commando:

php artisan envioEmailBI:cron

This command serves as the primary test, without having the inclusion in Cron of the server, it is even for testing, it is very useful, working is ok the process. Now just add to Cron as explained in previous answer .

  

* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

    
25.10.2017 / 21:09