4

I want to test that the job has been released back onto the queue in certain circumstances.

This is my job class:

class ChargeOrder extends Job
{
    use InteractsWithQueue, SerializesModels;

    /**
     * The order model which is to be charged
     */
    protected $order;

    /**
     * The token or card_id which allows us to take payment
     */
    protected $source;

    /**
     * Create a new job instance.
     *
     * @param   App\Order       $order;
     * @param   string          $source;
     * @return  array
     */
    public function __construct($order, $source)
    {
        $this->order        = $order;
        $this->source       = $source;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Charge $charge)
    {
        $result             = $charge->execute($this->source, $this->order->totalInclVat());
        $exception_errors   = config('payment.errors.exception_errors');

        //  If we have an error that isn't caused by the user (anything but a card error)
        //  We're going to notify ourselves via slack so we can investigate.
        if (array_key_exists('error', $result) && in_array($result['error']['code'], array_keys(config('payment.errors.other_error'))))
        {
            $client         = new Client(config('services.slack.channels.payment_errors.url'), config('services.slack.channels.payment_errors.settings'));
            $client->send(app()->environment() . ": " . $result['error']['code']);
        }

        //  If the error is in the list of errors that throw an exception, then throw it.
        if (array_key_exists('error', $result) && (in_array($result['error']['type'], $exception_errors) || in_array($result['error']['code'], $exception_errors)))
        {
            $status_code    = config('payment.errors')[$result['error']['type']][$result['error']['code']]['status_code'];
            $message        = config('payment.errors')[$result['error']['type']][$result['error']['code']]['message'];

            throw new BillingErrorException($status_code, $message);
        }

        //  If we still have an error, then it something out of the user's control.
        //  This could be a network error, or an error with the payment system
        //  Therefore, we're going to throw this job back onto the queue so it can be processed later.
        if (array_key_exists('error', $result) && in_array($result['error']['code'], array_keys(config('payment.errors.other_error'))))
        {
            $this->release(60);
        }
    }
}

I need to test that "$this->release(60)" is called in certain circumstances.

I'm trying to mock the job contract as so, in my tests:

// Set Up
$this->job  = Mockery::mock('Illuminate\Contracts\Queue\Job');
$this->app->instance('Illuminate\Contracts\Queue\Job', $this->job);

And then

// During Test
$this->job->shouldReceive('release')->once();

But this isn't working.

Anybody have any ideas?

Ed Stephenson
  • 704
  • 1
  • 8
  • 31

2 Answers2

12

Try adding the following in you test before dispatching the job:

Queue::after(function (JobProcessed $event) {
    $this->assertTrue($event->job->isReleased());
});

The code above will be triggered after the job is done and checks that the job has been released.

Make sure to remove any calls to Queue::fake()and $this->expectsJob() since these will prevent the actual job from being executed.

reekris
  • 508
  • 7
  • 12
0

I solved this problem by creating an event that is only fired after the job is released back into the queue. Then in my tests I can use the Event mocks to watch for that event after I dispatch a job and know if we released it back into the queue or not.

// In your job
$this->release();
event(new MyJobReleasedEvent()); // Every time you have a release() call

// In your Unit Test
Event::fake([MyJobReleasedEvent::class]);
dispatch(new MyJob();
Event::assertDispatched(MyJobReleasedEvent::class);

If you wanted to get fancy I'm sure you could wire up your own Job class that did this automatically when release() was called, but I needed it infrequently enough to just do it inline as-needed.