在使用PHPUnit测试模式时,我在队列中外部处理作业时遇到了问题。我有一项任务,将一条消息写入日志文件,当我访问一个路由时,该日志文件被分派给日志文件,我认为它可以像在开发中那样工作,在开发中有一个终端窗口侦听php artisan queue:work和其他正在运行的服务器的工作。
Test.php
public function testBasicTest()
{
$message = "Sample message job " . date("l jS \of F Y h:i:s A");
$filename = "laravel.log";
$this->json('GET', route('test.test-try-log-job'), ['message' => $message]);
$this->assertDatabaseHas('jobs', [
'id' => 1,
]);
exec('php artisan queue:work'); // Artisan::call("queue:work");
}控制器
class TestController extends Controller
{
public function tryLogJob(Request $request){
dispatch(new TestJob($request->message))->onQueue('default');
return response()->json(['success'=>true], Response::HTTP_OK);
}
}工作
class TestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function handle()
{
Log::info($this->message);
sleep(5);
}
}当我使用Artisan::call("queue:work");时,作业将被处理,但当我使用exec('php artisan queue:work');时,它将不会被处理。
有什么办法能让这件事起作用吗?我真的需要使用exec()函数。
发布于 2022-01-04 19:25:34
phpunit.xml配置为SQLite,而.env文件配置为MySQL。
在测试期间,作业被添加到SQLite,而不是运行exec('php artisan queue:work');的MySQL中。
我将phpunit.xml中的数据库变量设置为与这些.env (MySQL)匹配,并且作业正在正确处理。
https://stackoverflow.com/questions/70578762
复制相似问题