vendredi 17 janvier 2020

Laravel how testing queue job listener failed function?

I would like to test the failed function of the Listener class. I don't know where to start, should I use mocks, or go through Queue::fake() ?

in the documentation of laravel, that does not help me much. It is not complete, just the minimum to start with.

Someone to help me ! Thanks

This is my Listener Class


namespace App\Listeners;

use App\Events\Order;
use App\Models\Product;
use App\Services\XMLService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class CheckXml implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * The number of seconds the job can run before timing out.
     *
     * @var int
     */
    public $timeout = 300;

    /** @var \App\Services\XMLService */
    private $xmlServer;

    /** @var \App\Models\Order */
    private $order;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        $this->xmlServer = new XMLService();
    }

    /**
     * Handle the event.
     *
     * @param  Order  $event
     * @return void
     */
    public function handle(Order $event)
    {
        $order = $event->order;
        collect($event->products)->each(function (Product $p) use ($order) {
            try {
                $xml = $this->xmlServer::createXml($order, $p);
            } catch (AdheseException $exception) {
                throw new \Exception('Error', 500);
            }

            $this->adServer->post("post.do", $xml);
        });
    }

    /**
     * Handle a job failure.
     *
     * @param  \App\Events\Order  $event
     * @param  \Exception  $exception
     * @return void
     */
    public function failed(CRMOrder $event, $exception)
    {
        if ($exception->getCode() === 502) {
            $this->release();
        }

        if ($this->attempts() === 3) {
            Log::stack(['slack'])->critical('502 error', [
                'event' => $event, 'exception' => $exception
            ]);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire