How to test the cron in the Laravel?

Asked

Viewed 1,490 times

1

I created a cron for sending email every period of 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()
    {
    }
}

1 answer

3


In its class of commands has the command that must 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 a primary test, without having the inclusion in the server’s Cron, it is even for testing, it is very useful, working is ok the process. Now just add to Cron as explained in the previous reply.

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

  • I am receiving the message: Class App Units Commands Inspire does not exist

  • after removing a Commands Inspire::class line from C

  • consoleKernel received this message

  • [Reflectionexception] Class App Units Commands enviedEmailBIcron does not exist

  • You have to put the namespace correctly and check the paths, take a look this is your local error. @alexjosesilva

  • the class name is this: mailEmailBIcron

  • The class name is already outside the Laravel standard (Laravel follows some nomenclatures is pretty cool because it puts the code on an equality and ease of maintenance) the correct name should be EnvioEmailBlCron she has a namespace you added this with the command use ? @alexjosesilva

  • Just remembering that the above answer works and has been tested... with Laravel 5.3 of the official package. @alexjosesilva

  • use was not used!

  • I have this statement Envioemailbicron.php : namespace App Console Commands; use Illuminate Console Command;

  • Okay, but where are you playing this class needs to have her namespace, ie, App\Console\Commands\EnvioEmailBlCron I think that’s what’s missing. @alexjosesilva

  • in the file Envioemailbicron.php: namespace App Console Commands Envioemailblcron;

  • but the error persists!

  • Edit your question and put all the classes and how you’re calling! I can’t catch the bug. @alexjosesilva

  • added namespace and in Envioemailbicron.php

  • I added the.php kernel script.

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.