Skip to content

Commit

Permalink
Merge pull request #69 from stackkit/bugfix/dispatch-after-commit
Browse files Browse the repository at this point in the history
Respect after_commit when set through config
  • Loading branch information
marickvantuil authored Sep 2, 2022
2 parents 36cecdc + 1db3586 commit d54ba2e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
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;
});
}
}

0 comments on commit d54ba2e

Please sign in to comment.