diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 0683fd3..2386e76 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -12,6 +12,7 @@ use Illuminate\Queue\Events\JobQueued; use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Queue; @@ -27,6 +28,7 @@ use Tests\Support\JobOutput; use Tests\Support\JobThatWillBeReleased; use Tests\Support\SimpleJob; +use Tests\Support\SimpleJobWithTimeout; use Tests\Support\User; use Tests\Support\UserJob; @@ -518,4 +520,34 @@ public function headers_can_be_added_to_the_task_with_job_context() return $task->getHttpRequest()->getHeaders()['X-MyHeader'] === SimpleJob::class; }); } + + #[Test] + public function batched_jobs_with_custom_queue_are_dispatched_on_the_custom_queue() + { + // Arrange + CloudTasksApi::fake(); + + // Act + $this->dispatch(Bus::batch([ + tap(new SimpleJob(), function (SimpleJob $job) { + $job->queue = 'my-queue1'; + }), + tap(new SimpleJobWithTimeout(), function (SimpleJob $job) { + $job->queue = 'my-queue2'; + }), + ])->onQueue('my-batch-queue')); + + // Assert + CloudTasksApi::assertCreatedTaskCount(2); + + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return str_contains($task->getName(), 'SimpleJob') + && str_contains($task->getName(), 'my-batch-queue'); + }); + + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return str_contains($task->getName(), 'SimpleJobWithTimeout') + && str_contains($task->getName(), 'my-batch-queue'); + }); + } } diff --git a/tests/Support/BaseJob.php b/tests/Support/BaseJob.php index 4f9cc94..98b9688 100644 --- a/tests/Support/BaseJob.php +++ b/tests/Support/BaseJob.php @@ -4,6 +4,7 @@ namespace Tests\Support; +use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -12,5 +13,5 @@ class BaseJob implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; } diff --git a/tests/Support/SimpleJob.php b/tests/Support/SimpleJob.php index 4825355..007a308 100644 --- a/tests/Support/SimpleJob.php +++ b/tests/Support/SimpleJob.php @@ -4,16 +4,8 @@ namespace Tests\Support; -use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Foundation\Bus\Dispatchable; -use Illuminate\Queue\InteractsWithQueue; -use Illuminate\Queue\SerializesModels; - -class SimpleJob implements ShouldQueue +class SimpleJob extends BaseJob { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - public $tries = 3; public $id = 0; diff --git a/tests/TestCase.php b/tests/TestCase.php index cdaff0e..7bf5b86 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,8 +5,7 @@ namespace Tests; use Google\Cloud\Tasks\V2\Client\CloudTasksClient; -use Illuminate\Foundation\Bus\PendingClosureDispatch; -use Illuminate\Foundation\Bus\PendingDispatch; +use Illuminate\Bus\PendingBatch; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Event; use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksServiceProvider; @@ -42,11 +41,13 @@ protected function defineDatabaseMigrations() // Necessary to test the [failed_jobs] table. $this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations'); + $this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations/queue'); } protected function getEnvironmentSetUp($app) { $app['config']->set('database.default', 'testbench'); + $app['config']->set('queue.batching.database', 'testbench'); $port = env('DB_DRIVER') === 'mysql' ? 3307 : 5432; $app['config']->set('database.connections.testbench', [ 'driver' => env('DB_DRIVER', 'mysql'), @@ -95,11 +96,21 @@ public function dispatch($job, ?string $onQueue = null): DispatchedJob $task = $event->task; }); - tap(dispatch($job), function (PendingClosureDispatch|PendingDispatch $pendingDispatch) use ($onQueue) { - if ($onQueue !== null) { + if ($job instanceof PendingBatch) { + if ($onQueue) { + $job->onQueue($onQueue); + } + + $job->dispatch(); + } else { + $pendingDispatch = dispatch($job); + + if ($onQueue) { $pendingDispatch->onQueue($onQueue); } - }); + + unset($pendingDispatch); + } return new DispatchedJob($payload, $task, $this); }