File tree Expand file tree Collapse file tree 10 files changed +12
-137
lines changed
Expand file tree Collapse file tree 10 files changed +12
-137
lines changed Original file line number Diff line number Diff line change 44
55use Illuminate \Database \Eloquent \Factories \Factory ;
66use OnrampLab \Webhooks \Models \Webhook ;
7- use OnrampLab \Webhooks \ValueObjects \ExclusionCriterion ;
7+ use OnrampLab \Webhooks \ValueObjects \ExclusionCriteria ;
88
99class WebhookFactory extends Factory
1010{
@@ -27,13 +27,13 @@ public function definition()
2727 'http_verb ' => 'POST ' ,
2828 'enabled ' => true ,
2929 'exclusion_criteria ' => [
30- new ExclusionCriterion ([
31- 'name ' => 'account_ids ' ,
30+ new ExclusionCriteria ([
31+ 'name ' => 'campaign_ids ' ,
3232 'values ' => [1 , 2 ]
3333 ]),
34- new ExclusionCriterion ([
34+ new ExclusionCriteria ([
3535 'name ' => 'events ' ,
36- 'values ' => ['test_event ' ]
36+ 'values ' => ['sms_sent ' , ' sms_delivered ' ]
3737 ])
3838 ],
3939 'contextable_id ' => $ this ->faker ->randomNumber (),
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -26,9 +26,4 @@ public function getWebhookPayload(): array
2626 {
2727 return [];
2828 }
29-
30- public function getExclusionCriteria (): array
31- {
32- return [];
33- }
3429}
Original file line number Diff line number Diff line change @@ -12,6 +12,4 @@ public function getWebhookPayload(): array;
1212 public function getWebhookContext (): ?Model ;
1313
1414 public function getEventOccurredAt (): ?Carbon ;
15-
16- public function getExclusionCriteria (): array ;
1715}
Original file line number Diff line number Diff line change 66use Illuminate \Database \Eloquent \Model ;
77use Illuminate \Database \Eloquent \Factories \Factory ;
88use Illuminate \Database \Eloquent \Relations \HasMany ;
9- use OnrampLab \Webhooks \AttributeCastors \ExclusionCriteriaCastor ;
109use OnrampLab \Webhooks \Database \Factories \WebhookFactory ;
1110
1211
@@ -37,7 +36,7 @@ class Webhook extends Model
3736 */
3837 protected $ casts = [
3938 'enabled ' => 'boolean ' ,
40- 'exclusion_criteria ' => ExclusionCriteriaCastor::class ,
39+ 'exclusion_criteria ' => ' array ' ,
4140 'headers ' => 'array '
4241 ];
4342
Original file line number Diff line number Diff line change 44
55use JsonSerializable ;
66
7- class ExclusionCriterion implements JsonSerializable
7+ class ExclusionCriteria implements JsonSerializable
88{
99 public string $ name ;
1010
@@ -33,14 +33,4 @@ public function jsonSerialize(): array
3333 {
3434 return $ this ->toArray ();
3535 }
36-
37- public function getName (): string
38- {
39- return $ this ->name ;
40- }
41-
42- public function getValues (): array
43- {
44- return $ this ->values ;
45- }
4636}
Original file line number Diff line number Diff line change @@ -14,13 +14,8 @@ class WebhookDispatcher
1414
1515 public function handle (Webhookable $ event )
1616 {
17- $ webhooks = $ this ->getWebhooks ($ event );
18-
19- if ($ webhooks ->isEmpty ()) {
20- return ;
21- }
22-
2317 $ payload = $ event ->getWebhookPayload ();
18+ $ webhooks = $ this ->getWebhooks ($ event );
2419 $ this ->eventOccurredAt = $ event ->getEventOccurredAt ();
2520
2621 foreach ($ webhooks as $ webhook ) {
@@ -50,26 +45,7 @@ protected function getWebhooks(Webhookable $event): Collection
5045
5146 protected function areExclusionCriteriaMatched (Webhookable $ event , Webhook $ webhook ): bool
5247 {
53- $ webhookExclusionCriteria = $ webhook ->exclusion_criteria ;
54- $ eventExclusionCriteria = $ event ->getExclusionCriteria ();
55-
56- if (is_null ($ webhookExclusionCriteria ) || empty ($ eventExclusionCriteria )) {
57- return false ;
58- }
59-
60- foreach ($ eventExclusionCriteria as $ eventExclusionCriterion ) {
61- $ eventExclusionCriteriaName = $ eventExclusionCriterion ->getName ();
62- $ eventExclusionCriteriaValues = $ eventExclusionCriterion ->getValues ();
63-
64- foreach ($ webhookExclusionCriteria as $ webhookExclusionCriterion ) {
65- $ webhookExclusionCriteriaName = $ webhookExclusionCriterion ->getName ();
66- $ webhookExclusionCriteriaValues = $ webhookExclusionCriterion ->getValues ();
67- if ($ eventExclusionCriteriaName === $ webhookExclusionCriteriaName && count (array_intersect ($ webhookExclusionCriteriaValues , $ eventExclusionCriteriaValues )) > 0 ) {
68- return true ;
69- }
70- }
71- }
72-
48+ //to be implemented
7349 return false ;
7450 }
7551
Original file line number Diff line number Diff line change 22
33namespace OnrampLab \Webhooks \Tests \Classes ;
44
5+ use Illuminate \Database \Eloquent \Model ;
56use OnrampLab \Webhooks \Contracts \Webhookable as WebhookableContract ;
67use \OnrampLab \Webhooks \Concerns \Webhookable as WebhookableTrait ;
7- use OnrampLab \Webhooks \ValueObjects \ExclusionCriterion ;
88
99class TestEvent implements WebhookableContract
1010{
@@ -20,14 +20,4 @@ public function getWebhookContext(): ?Account
2020 {
2121 return $ this ->account ;
2222 }
23-
24- public function getExclusionCriteria (): array
25- {
26- return [
27- new ExclusionCriterion ([
28- 'name ' => 'events ' ,
29- 'values ' => ['test_event ' ]
30- ]),
31- ];
32- }
3323}
Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ protected function setUp(): void
3131 */
3232 public function should_dispatch_webhook_according_to_context (): void
3333 {
34+
3435 Queue::fake ();
3536
3637 $ dispatcher = new WebhookDispatcher ();
Original file line number Diff line number Diff line change 22
33namespace OnrampLab \Webhooks \Tests \Unit ;
44
5- use OnrampLab \Webhooks \CallWebhookJob ;
6- use OnrampLab \Webhooks \Models \Webhook ;
7- use OnrampLab \Webhooks \Tests \Classes \Account ;
8- use OnrampLab \Webhooks \Tests \Classes \TestEvent ;
9- use OnrampLab \Webhooks \Tests \TestCase ;
10- use OnrampLab \Webhooks \WebhookDispatcher ;
11-
12- class WebhookDispatcherTest extends TestCase
5+ class WebhookDispatcherTest
136{
14- protected function setUp (): void
15- {
16- parent ::setUp ();
17- $ this ->account = Account::factory ()->create ();
18- $ attributes = [
19- 'contextable_id ' => $ this ->account ->id ,
20- 'contextable_type ' => get_class ($ this ->account )
21- ];
22- $ this ->webhook = Webhook::factory ()->create ($ attributes );
23- $ this ->event = new TestEvent ($ this ->account );
24- $ this ->callWebhookJobMock = $ this ->mock (CallWebhookJob::class);
25- }
267
27- /**
28- * @test
29- */
30- public function should_not_dispatch_webhook_when_exclusion_criteria_are_matched (): void
31- {
32- $ dispatcher = new WebhookDispatcher ();
33- $ dispatcher ->handle ($ this ->event );
34- }
358}
36-
You can’t perform that action at this time.
0 commit comments