作者:itbases | 来源:互联网 | 2023-01-12 12:50
当工作完成时,我试图抓住一个事件
测试代码:
class MyTest extends TestCase {
public function testJobsEvents ()
{
Queue::after(function (JobProcessed $event) {
// if ( $job is 'MyJob1' ) then do test
dump($event->job->payload());
$event->job->payload()
});
$respOnse= $this->post('/api/user', [ 'test' => 'data' ], $this->headers);
$response->assertSuccessful($response->isOk());
}
}
UserController中的方法:
public function userAction (Request $request) {
MyJob1::dispatch($request->toArray());
MyJob2::dispatch($request->toArray());
return response(null, 200);
}
我的工作:
class Job1 implements ShouldQueue {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $data = [];
public function __construct($data)
{
$this->data= $data;
}
public function handle()
{
// Process uploaded
}
}
我需要检查一些数据的作业完成,但我从串行数据后,
$event->job->payload()
在Queue::after
我不知道如何检查的工作吗?
1> Bondan Sebas..:
那么,要测试handle
方法内部的逻辑,您只需要实例化作业类并调用该handle
方法.
public function testJobsEvents()
{
$job = new \App\Jobs\YourJob;
$job->handle();
// Assert the side effect of your job...
}
记住,毕竟工作只是一个阶级.