diff --git a/src/CloudTasksConnector.php b/src/CloudTasksConnector.php index 79465fe..db81cd6 100644 --- a/src/CloudTasksConnector.php +++ b/src/CloudTasksConnector.php @@ -23,6 +23,6 @@ public function connect(array $config) }; } - return new CloudTasksQueue($config, app(CloudTasksClient::class)); + return new CloudTasksQueue($config, app(CloudTasksClient::class), $config['after_commit'] ?? null); } } diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index 1e85c7f..7fde5e9 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -24,10 +24,11 @@ class CloudTasksQueue extends LaravelQueue implements QueueContract public array $config; - public function __construct(array $config, CloudTasksClient $client) + public function __construct(array $config, CloudTasksClient $client, $dispatchAfterCommit = false) { $this->client = $client; $this->config = $config; + $this->dispatchAfterCommit = $dispatchAfterCommit; } /** diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 4e45e83..7f44496 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -144,7 +144,7 @@ public function it_posts_the_task_the_correct_queue() $command = TaskHandler::getCommandProperties($decoded['data']['command']); return $decoded['displayName'] === SimpleJob::class - && $command['queue'] === null + && ($command['queue'] ?? null) === null && $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue'; }); @@ -161,7 +161,7 @@ public function it_posts_the_task_the_correct_queue() /** * @test */ - public function it_can_dispatch_after_commit() + public function it_can_dispatch_after_commit_inline() { if (version_compare(app()->version(), '8.0.0', '<')) { $this->markTestSkipped('Not supported by Laravel 7.x and below.'); @@ -181,4 +181,29 @@ public function it_can_dispatch_after_commit() return $event->job instanceof SimpleJob; }); } + + /** + * @test + */ + public function it_can_dispatch_after_commit_through_config() + { + if (version_compare(app()->version(), '8.0.0', '<')) { + $this->markTestSkipped('Not supported by Laravel 7.x and below.'); + } + + // Arrange + CloudTasksApi::fake(); + Event::fake(); + $this->setConfigValue('after_commit', true); + + // Act & Assert + Event::assertNotDispatched(JobQueued::class); + DB::beginTransaction(); + SimpleJob::dispatch(); + Event::assertNotDispatched(JobQueued::class); + DB::commit(); + Event::assertDispatched(JobQueued::class, function (JobQueued $event) { + return $event->job instanceof SimpleJob; + }); + } }