Skip to content

Respect after_commit when set through config #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CloudTasksConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
29 changes: 27 additions & 2 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});

Expand All @@ -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.');
Expand All @@ -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;
});
}
}