Skip to content

Commit ff0c7b1

Browse files
committed
Make task handler route customizable
1 parent cfcf0eb commit ff0c7b1

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

config/cloud-tasks.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
declare(strict_types=1);
44

55
return [
6+
// The URI of the endpoint that will handle the task
7+
'uri' => env('STACKKIT_CLOUD_TASKS_URI', 'handle-task'),
8+
69
// If the application only dispatches jobs
710
'disable_task_handler' => env('STACKKIT_CLOUD_TASKS_DISABLE_TASK_HANDLER', false),
811

src/CloudTasksQueue.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ public function getHandler(): string
245245

246246
$handler = rtrim($this->config['handler'], '/');
247247

248-
if (str_ends_with($handler, '/handle-task')) {
248+
if (str_ends_with($handler, '/'. config('cloud-tasks.uri'))) {
249249
return $handler;
250250
}
251251

252-
return $handler.'/handle-task';
252+
return $handler.'/'. config('cloud-tasks.uri');
253253
}
254254
}

src/CloudTasksServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function registerRoutes(): void
6464
*/
6565
$router = $this->app['router'];
6666

67-
$router->post('handle-task', [TaskHandler::class, 'handle'])->name('cloud-tasks.handle-task');
67+
$router->post(config('cloud-tasks.uri'), [TaskHandler::class, 'handle'])->name('cloud-tasks.handle-task');
6868
}
6969

7070
private function registerEvents(): void

tests/ConfigHandlerTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,35 @@ public function test_it_allows_a_handler_url_to_contain_path(string $handler, st
2626
});
2727
}
2828

29+
/** @test */
30+
public function the_handle_route_task_uri_can_be_configured(): void
31+
{
32+
CloudTasksApi::fake();
33+
34+
$this->app['config']->set('cloud-tasks.uri', 'my-custom-route');
35+
36+
$this->dispatch(new SimpleJob());
37+
38+
CloudTasksApi::assertTaskCreated(function (Task $task) {
39+
return $task->getHttpRequest()->getUrl() === 'https://docker.for.mac.localhost:8080/my-custom-route';
40+
});
41+
}
42+
43+
/** @test */
44+
public function the_handle_route_task_uri_in_combination_with_path_can_be_configured(): void
45+
{
46+
CloudTasksApi::fake();
47+
48+
$this->setConfigValue('handler', 'https://example.com/api');
49+
$this->app['config']->set('cloud-tasks.uri', 'my-custom-route');
50+
51+
$this->dispatch(new SimpleJob());
52+
53+
CloudTasksApi::assertTaskCreated(function (Task $task) {
54+
return $task->getHttpRequest()->getUrl() === 'https://example.com/api/my-custom-route';
55+
});
56+
}
57+
2958
public static function handlerDataProvider(): array
3059
{
3160
return [

tests/TestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ protected function getPackageProviders($app)
7070
*/
7171
protected function defineDatabaseMigrations()
7272
{
73-
// $this->loadMigrationsFrom(__DIR__.'/../migrations');
74-
// $this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations');
73+
$this->loadMigrationsFrom(__DIR__.'/../migrations');
74+
$this->loadMigrationsFrom(__DIR__.'/../vendor/orchestra/testbench-core/laravel/migrations');
7575
}
7676

7777
/**

0 commit comments

Comments
 (0)