Skip to content

Commit d54ba2e

Browse files
Merge pull request #69 from stackkit/bugfix/dispatch-after-commit
Respect after_commit when set through config
2 parents 36cecdc + 1db3586 commit d54ba2e

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/CloudTasksConnector.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ public function connect(array $config)
2323
};
2424
}
2525

26-
return new CloudTasksQueue($config, app(CloudTasksClient::class));
26+
return new CloudTasksQueue($config, app(CloudTasksClient::class), $config['after_commit'] ?? null);
2727
}
2828
}

src/CloudTasksQueue.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ class CloudTasksQueue extends LaravelQueue implements QueueContract
2424

2525
public array $config;
2626

27-
public function __construct(array $config, CloudTasksClient $client)
27+
public function __construct(array $config, CloudTasksClient $client, $dispatchAfterCommit = false)
2828
{
2929
$this->client = $client;
3030
$this->config = $config;
31+
$this->dispatchAfterCommit = $dispatchAfterCommit;
3132
}
3233

3334
/**

tests/QueueTest.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function it_posts_the_task_the_correct_queue()
144144
$command = TaskHandler::getCommandProperties($decoded['data']['command']);
145145

146146
return $decoded['displayName'] === SimpleJob::class
147-
&& $command['queue'] === null
147+
&& ($command['queue'] ?? null) === null
148148
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue';
149149
});
150150

@@ -161,7 +161,7 @@ public function it_posts_the_task_the_correct_queue()
161161
/**
162162
* @test
163163
*/
164-
public function it_can_dispatch_after_commit()
164+
public function it_can_dispatch_after_commit_inline()
165165
{
166166
if (version_compare(app()->version(), '8.0.0', '<')) {
167167
$this->markTestSkipped('Not supported by Laravel 7.x and below.');
@@ -181,4 +181,29 @@ public function it_can_dispatch_after_commit()
181181
return $event->job instanceof SimpleJob;
182182
});
183183
}
184+
185+
/**
186+
* @test
187+
*/
188+
public function it_can_dispatch_after_commit_through_config()
189+
{
190+
if (version_compare(app()->version(), '8.0.0', '<')) {
191+
$this->markTestSkipped('Not supported by Laravel 7.x and below.');
192+
}
193+
194+
// Arrange
195+
CloudTasksApi::fake();
196+
Event::fake();
197+
$this->setConfigValue('after_commit', true);
198+
199+
// Act & Assert
200+
Event::assertNotDispatched(JobQueued::class);
201+
DB::beginTransaction();
202+
SimpleJob::dispatch();
203+
Event::assertNotDispatched(JobQueued::class);
204+
DB::commit();
205+
Event::assertDispatched(JobQueued::class, function (JobQueued $event) {
206+
return $event->job instanceof SimpleJob;
207+
});
208+
}
184209
}

0 commit comments

Comments
 (0)