Laravel 测试 Job 是否发布

标签 laravel testing jobs task-queue

我想测试在某些情况下作业是否已被释放回队列。

这是我的工作类:

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);
        }
    }
}

我需要测试在某些情况下调用“$this->release(60)”。

我正试图在我的测试中模拟工作契约(Contract):

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

然后

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

但这行不通。

有人有什么想法吗?

最佳答案

尝试在调度作业之前在您的测试中添加以下内容:

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

上面的代码将在作业完成后触发,并检查作业是否已发布。

确保删除对 Queue::fake()$this->expectsJob() 的任何调用,因为它们会阻止实际作业的执行。

关于Laravel 测试 Job 是否发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244475/

相关文章:

java - 使用 Mockito 为扩展泛型类的类创建模拟

android - 如何检查 Junit 中的字母数字条件

kotlin - 另一个作业完成后重新创建作业

java - Java 中使用拆分和聚合的并行作业执行

java - 缓存/作业跟踪框架

php - Laravel:此集合实例上不存在属性 [名称]

javascript - 提交时切换表单 - jQuery (Ajax)

javascript - Laravel 和 Dropzonejs,文件上传不正确

php - 如何在单个查询中根据 WHERE 子句有条件地选择要 SELECT 的列?

java - 如何正确测试java中类的内部方法