Skip to content

Commit 971239e

Browse files
authored
Merge pull request #272 from laravel-workflow/fix-webhook-route-prefixes
Fix webhookUrl() to account for route prefixes
2 parents a80a1ec + 0f9a5f5 commit 971239e

5 files changed

Lines changed: 19 additions & 18 deletions

File tree

src/Activity.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@ public function workflowId()
7676

7777
public function webhookUrl(string $signalMethod = ''): string
7878
{
79-
$basePath = config('workflows.webhooks_route', '/webhooks');
8079
$workflow = Str::kebab(class_basename($this->storedWorkflow->class));
8180

8281
if ($signalMethod === '') {
83-
return url("{$basePath}/{$workflow}");
82+
return route("workflows.start.{$workflow}");
8483
}
8584

8685
$signal = Str::kebab($signalMethod);
87-
return url("{$basePath}/signal/{$workflow}/{$this->storedWorkflow->id}/{$signal}");
86+
return route("workflows.signal.{$workflow}.{$signal}", [
87+
'workflowId' => $this->storedWorkflow->id,
88+
]);
8889
}
8990

9091
public function handle()

src/Webhooks.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static function registerWorkflowWebhooks($workflow, $basePath)
8585
return response()->json([
8686
'message' => 'Workflow started',
8787
]);
88-
});
88+
})->name("workflows.start.{$slug}");
8989
}
9090
}
9191
}
@@ -111,7 +111,7 @@ static function (Request $request, $workflowId) use ($workflow, $method) {
111111
'message' => 'Signal sent',
112112
]);
113113
}
114-
);
114+
)->name("workflows.signal.{$slug}.{$signal}");
115115
}
116116
}
117117
}

tests/Fixtures/TestWorkflow.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use Workflow\ActivityStub;
99
use Workflow\QueryMethod;
1010
use Workflow\SignalMethod;
11+
use Workflow\Webhook;
1112
use Workflow\Workflow;
1213
use Workflow\WorkflowStub;
1314

15+
#[Webhook]
1416
class TestWorkflow extends Workflow
1517
{
1618
public $connection = 'redis';
@@ -20,6 +22,7 @@ class TestWorkflow extends Workflow
2022
private bool $canceled = false;
2123

2224
#[SignalMethod]
25+
#[Webhook]
2326
public function cancel(): void
2427
{
2528
$this->canceled = true;

tests/Unit/ActivityTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Workflow\Serializers\Serializer;
1818
use Workflow\States\WorkflowCreatedStatus;
1919
use Workflow\States\WorkflowFailedStatus;
20+
use Workflow\Webhooks;
2021
use Workflow\WorkflowStub;
2122

2223
final class ActivityTest extends TestCase
@@ -125,12 +126,14 @@ public function testActivityAlreadyComplete(): void
125126

126127
public function testWebhookUrl(): void
127128
{
129+
Webhooks::routes('Tests\\Fixtures', __DIR__ . '/../Fixtures');
130+
128131
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
129132
$activity = new TestOtherActivity(0, now()->toDateTimeString(), StoredWorkflow::findOrFail($workflow->id()), [
130133
'other',
131134
]);
132135

133-
$this->assertSame('http://localhost/webhooks/test-workflow', $activity->webhookUrl());
134-
$this->assertSame('http://localhost/webhooks/signal/test-workflow/1/other', $activity->webhookUrl('other'));
136+
$this->assertSame('http://localhost/webhooks/start/test-workflow', $activity->webhookUrl());
137+
$this->assertSame('http://localhost/webhooks/signal/test-workflow/1/cancel', $activity->webhookUrl('cancel'));
135138
}
136139
}

tests/Unit/WebhooksTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,13 @@ public function testResolveNamedParametersUsesDefaults()
313313

314314
public function testWebhookRegistration()
315315
{
316-
$response = $this->postJson('/webhooks/signal/test-webhook-workflow/1/cancel');
317-
318-
Route::shouldReceive('post')
319-
->once()
320-
->withArgs(static function ($uri, $callback) {
321-
return str_contains($uri, 'webhooks/start/test-webhook-workflow');
322-
});
316+
$routeMock = Mockery::mock();
317+
$routeMock->shouldReceive('name')
318+
->andReturnSelf();
323319

324320
Route::shouldReceive('post')
325-
->once()
326-
->withArgs(static function ($uri, $callback) {
327-
return str_contains($uri, 'webhooks/signal/test-webhook-workflow/{workflowId}/cancel');
328-
});
321+
->times(4)
322+
->andReturn($routeMock);
329323

330324
Webhooks::routes('Tests\\Fixtures', __DIR__ . '/../Fixtures');
331325
}

0 commit comments

Comments
 (0)