Skip to content

Commit 367c8fc

Browse files
Merge pull request #148 from stackkit/feature/batched-job-queue-tests
Add tests for custom queue on batched jobs
2 parents d6471a3 + c7b8817 commit 367c8fc

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

tests/QueueTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Queue\Events\JobQueued;
1313
use Illuminate\Queue\Events\JobReleasedAfterException;
1414
use Illuminate\Support\Carbon;
15+
use Illuminate\Support\Facades\Bus;
1516
use Illuminate\Support\Facades\DB;
1617
use Illuminate\Support\Facades\Event;
1718
use Illuminate\Support\Facades\Queue;
@@ -27,6 +28,7 @@
2728
use Tests\Support\JobOutput;
2829
use Tests\Support\JobThatWillBeReleased;
2930
use Tests\Support\SimpleJob;
31+
use Tests\Support\SimpleJobWithTimeout;
3032
use Tests\Support\User;
3133
use Tests\Support\UserJob;
3234

@@ -518,4 +520,34 @@ public function headers_can_be_added_to_the_task_with_job_context()
518520
return $task->getHttpRequest()->getHeaders()['X-MyHeader'] === SimpleJob::class;
519521
});
520522
}
523+
524+
#[Test]
525+
public function batched_jobs_with_custom_queue_are_dispatched_on_the_custom_queue()
526+
{
527+
// Arrange
528+
CloudTasksApi::fake();
529+
530+
// Act
531+
$this->dispatch(Bus::batch([
532+
tap(new SimpleJob(), function (SimpleJob $job) {
533+
$job->queue = 'my-queue1';
534+
}),
535+
tap(new SimpleJobWithTimeout(), function (SimpleJob $job) {
536+
$job->queue = 'my-queue2';
537+
}),
538+
])->onQueue('my-batch-queue'));
539+
540+
// Assert
541+
CloudTasksApi::assertCreatedTaskCount(2);
542+
543+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
544+
return str_contains($task->getName(), 'SimpleJob')
545+
&& str_contains($task->getName(), 'my-batch-queue');
546+
});
547+
548+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
549+
return str_contains($task->getName(), 'SimpleJobWithTimeout')
550+
&& str_contains($task->getName(), 'my-batch-queue');
551+
});
552+
}
521553
}

tests/Support/BaseJob.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Support;
66

7+
use Illuminate\Bus\Batchable;
78
use Illuminate\Bus\Queueable;
89
use Illuminate\Contracts\Queue\ShouldQueue;
910
use Illuminate\Foundation\Bus\Dispatchable;
@@ -12,5 +13,5 @@
1213

1314
class BaseJob implements ShouldQueue
1415
{
15-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16+
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
1617
}

tests/Support/SimpleJob.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@
44

55
namespace Tests\Support;
66

7-
use Illuminate\Bus\Queueable;
8-
use Illuminate\Contracts\Queue\ShouldQueue;
9-
use Illuminate\Foundation\Bus\Dispatchable;
10-
use Illuminate\Queue\InteractsWithQueue;
11-
use Illuminate\Queue\SerializesModels;
12-
13-
class SimpleJob implements ShouldQueue
7+
class SimpleJob extends BaseJob
148
{
15-
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16-
179
public $tries = 3;
1810

1911
public $id = 0;

tests/TestCase.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
namespace Tests;
66

77
use Google\Cloud\Tasks\V2\Client\CloudTasksClient;
8-
use Illuminate\Foundation\Bus\PendingClosureDispatch;
9-
use Illuminate\Foundation\Bus\PendingDispatch;
8+
use Illuminate\Bus\PendingBatch;
109
use Illuminate\Foundation\Testing\DatabaseTransactions;
1110
use Illuminate\Support\Facades\Event;
1211
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksServiceProvider;
@@ -42,11 +41,13 @@ protected function defineDatabaseMigrations()
4241
// Necessary to test the [failed_jobs] table.
4342

4443
$this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations');
44+
$this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations/queue');
4545
}
4646

4747
protected function getEnvironmentSetUp($app)
4848
{
4949
$app['config']->set('database.default', 'testbench');
50+
$app['config']->set('queue.batching.database', 'testbench');
5051
$port = env('DB_DRIVER') === 'mysql' ? 3307 : 5432;
5152
$app['config']->set('database.connections.testbench', [
5253
'driver' => env('DB_DRIVER', 'mysql'),
@@ -95,11 +96,21 @@ public function dispatch($job, ?string $onQueue = null): DispatchedJob
9596
$task = $event->task;
9697
});
9798

98-
tap(dispatch($job), function (PendingClosureDispatch|PendingDispatch $pendingDispatch) use ($onQueue) {
99-
if ($onQueue !== null) {
99+
if ($job instanceof PendingBatch) {
100+
if ($onQueue) {
101+
$job->onQueue($onQueue);
102+
}
103+
104+
$job->dispatch();
105+
} else {
106+
$pendingDispatch = dispatch($job);
107+
108+
if ($onQueue) {
100109
$pendingDispatch->onQueue($onQueue);
101110
}
102-
});
111+
112+
unset($pendingDispatch);
113+
}
103114

104115
return new DispatchedJob($payload, $task, $this);
105116
}

0 commit comments

Comments
 (0)