Skip to content

Commit d3b7f24

Browse files
committed
test: add tests & docs
1 parent d8bc963 commit d3b7f24

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ WebhookCall::create()
118118
->dispatchSync();
119119
```
120120

121+
### Conditionally sending webhooks
122+
123+
If you would like to conditionally dispatch a webhook, you may use the `dispatchIf`, `dispatchUnless`, `dispatchSyncIf` and `dispatchSyncUnless` methods:
124+
125+
```php
126+
WebhookCall::create()
127+
...
128+
->dispatchIf($condition);
129+
130+
WebhookCall::create()
131+
...
132+
->dispatchUnless($condition);
133+
134+
WebhookCall::create()
135+
...
136+
->dispatchSyncIf($condition);
137+
138+
WebhookCall::create()
139+
...
140+
->dispatchSyncUnless($condition);
141+
```
142+
121143
### How signing requests works
122144

123145
When setting up, it's common to generate, store, and share a secret between your app and the app that wants to receive webhooks. Generating the secret could be done with `Illuminate\Support\Str::random()`, but it's entirely up to you. The package will use the secret to sign a webhook call.

tests/WebhookTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,44 @@ public function it_can_get_the_uuid_property()
151151
$this->assertIsString($webhookCall->getUuid());
152152
$this->assertSame('my-unique-identifier', $webhookCall->getUuid());
153153
}
154+
155+
/** @test */
156+
public function it_can_dispatch_a_job_that_calls_a_webhook_if_condition_true()
157+
{
158+
$url = 'https://localhost';
159+
160+
WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(true);
161+
162+
Queue::assertPushed(CallWebhookJob::class);
163+
}
164+
165+
/** @test */
166+
public function it_can_not_dispatch_a_job_that_calls_a_webhook_if_condition_false()
167+
{
168+
$url = 'https://localhost';
169+
170+
WebhookCall::create()->url($url)->useSecret('123')->dispatchIf(false);
171+
172+
Queue::assertNotPushed(CallWebhookJob::class);
173+
}
174+
175+
/** @test */
176+
public function it_can_not_dispatch_a_job_that_calls_a_webhook_unless_condition_true()
177+
{
178+
$url = 'https://localhost';
179+
180+
WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(true);
181+
182+
Queue::assertNotPushed(CallWebhookJob::class);
183+
}
184+
185+
/** @test */
186+
public function it_can_dispatch_a_job_that_calls_a_webhook_unless_condition_false()
187+
{
188+
$url = 'https://localhost';
189+
190+
WebhookCall::create()->url($url)->useSecret('123')->dispatchUnless(false);
191+
192+
Queue::assertPushed(CallWebhookJob::class);
193+
}
154194
}

0 commit comments

Comments
 (0)