Skip to content

Commit 0716a70

Browse files
author
Jiří Novák
committed
feat: AJDA-143 add webhook recipient, fix code style
1 parent 5db5d30 commit 0716a70

6 files changed

Lines changed: 118 additions & 50 deletions

File tree

src/Requests/PostSubscription/EmailRecipient.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Keboola\NotificationClient\Requests\PostSubscription;
66

7-
use JsonSerializable;
8-
9-
class EmailRecipient implements JsonSerializable
7+
class EmailRecipient implements RecipientInterface
108
{
119
private string $address;
1210

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostSubscription;
6+
7+
use JsonSerializable;
8+
9+
interface RecipientInterface extends JsonSerializable
10+
{
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostSubscription;
6+
7+
class WebhookRecipient implements RecipientInterface
8+
{
9+
private string $url;
10+
11+
public function __construct(string $url)
12+
{
13+
$this->url = $url;
14+
}
15+
16+
public function jsonSerialize(): array
17+
{
18+
return [
19+
'channel' => 'webhook',
20+
'url' => $this->url,
21+
];
22+
}
23+
}

src/Requests/Subscription.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
use Keboola\NotificationClient\Requests\PostEvent\PhaseJobSucceededWithWarningEventData;
1919
use Keboola\NotificationClient\Requests\PostSubscription\EmailRecipient;
2020
use Keboola\NotificationClient\Requests\PostSubscription\Filter;
21+
use Keboola\NotificationClient\Requests\PostSubscription\RecipientInterface;
2122

2223
class Subscription implements JsonSerializable
2324
{
2425
private string $eventType;
25-
private EmailRecipient $recipient;
26+
private RecipientInterface $recipient;
2627
/** @var array<Filter> */
2728
private array $filters;
2829

2930
/**
3031
* PostSubscriptionRequest constructor.
3132
* @param string $eventType
32-
* @param EmailRecipient $recipient
33+
* @param RecipientInterface $recipient
3334
* @param array<Filter> $filters
3435
*/
35-
public function __construct(string $eventType, EmailRecipient $recipient, array $filters)
36+
public function __construct(string $eventType, RecipientInterface $recipient, array $filters)
3637
{
3738
$this->checkEventType($eventType);
3839
$this->eventType = $eventType;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Tests\Requests\PostSubscription;
6+
7+
use Keboola\NotificationClient\Requests\PostSubscription\WebhookRecipient;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class WebhookRecipientTest extends TestCase
11+
{
12+
public function testJsonSerialize(): void
13+
{
14+
$webhookRecipient = new WebhookRecipient('https://example.com/webhook');
15+
self::assertSame(
16+
[
17+
'channel' => 'webhook',
18+
'url' => 'https://example.com/webhook',
19+
],
20+
$webhookRecipient->jsonSerialize(),
21+
);
22+
}
23+
}

tests/Requests/SubscriptionTest.php

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,39 @@
66

77
use Keboola\NotificationClient\Requests\PostSubscription\EmailRecipient;
88
use Keboola\NotificationClient\Requests\PostSubscription\Filter;
9+
use Keboola\NotificationClient\Requests\PostSubscription\RecipientInterface;
10+
use Keboola\NotificationClient\Requests\PostSubscription\WebhookRecipient;
911
use Keboola\NotificationClient\Requests\Subscription;
1012
use PHPUnit\Framework\TestCase;
1113

1214
class SubscriptionTest extends TestCase
1315
{
14-
public function testJsonSerialize(): void
16+
public function recipientProvider(): iterable
17+
{
18+
yield 'email' => [
19+
new EmailRecipient('john.doe@example.com'),
20+
[
21+
'channel' => 'email',
22+
'address' => 'john.doe@example.com',
23+
],
24+
];
25+
26+
yield 'webhook' => [
27+
new WebhookRecipient('https://example.com/webhook'),
28+
[
29+
'channel' => 'webhook',
30+
'url' => 'https://example.com/webhook',
31+
],
32+
];
33+
}
34+
35+
36+
/** @dataProvider recipientProvider */
37+
public function testJsonSerialize(RecipientInterface $recipient, array $expectedRecipient): void
1538
{
1639
$subscriptionRequest = new Subscription(
1740
'job-failed',
18-
new EmailRecipient('john.doe@example.com'),
41+
$recipient,
1942
[new Filter('foo', 'bar')],
2043
);
2144
self::assertSame(
@@ -27,20 +50,18 @@ public function testJsonSerialize(): void
2750
'value' => 'bar',
2851
],
2952
],
30-
'recipient' => [
31-
'channel' => 'email',
32-
'address' => 'john.doe@example.com',
33-
],
53+
'recipient' => $expectedRecipient,
3454
],
3555
$subscriptionRequest->jsonSerialize(),
3656
);
3757
}
3858

39-
public function testJsonSerializeStrangeFilters(): void
59+
/** @dataProvider recipientProvider */
60+
public function testJsonSerializeStrangeFilters(RecipientInterface $recipient, array $expectedRecipient): void
4061
{
4162
$subscriptionRequest = new Subscription(
4263
'job-failed',
43-
new EmailRecipient('john.doe@example.com'),
64+
$recipient,
4465
['a' => new Filter('foo', 'bar'), '4filter' => new Filter('bar', 'Kochba')],
4566
);
4667
self::assertSame(
@@ -56,20 +77,18 @@ public function testJsonSerializeStrangeFilters(): void
5677
'value' => 'Kochba',
5778
],
5879
],
59-
'recipient' => [
60-
'channel' => 'email',
61-
'address' => 'john.doe@example.com',
62-
],
80+
'recipient' => $expectedRecipient,
6381
],
6482
$subscriptionRequest->jsonSerialize(),
6583
);
6684
}
6785

68-
public function testPhaseJobFailedSubscription(): void
86+
/** @dataProvider recipientProvider */
87+
public function testPhaseJobFailedSubscription(RecipientInterface $recipient, array $expectedRecipient): void
6988
{
7089
$subscriptionRequest = new Subscription(
7190
'phase-job-failed',
72-
new EmailRecipient('john.doe@example.com'),
91+
$recipient,
7392
[
7493
new Filter('job.component.id', 'my.component'),
7594
new Filter('job.configuration.id', '12345'),
@@ -93,20 +112,20 @@ public function testPhaseJobFailedSubscription(): void
93112
'value' => '123',
94113
],
95114
],
96-
'recipient' => [
97-
'channel' => 'email',
98-
'address' => 'john.doe@example.com',
99-
],
115+
'recipient' => $expectedRecipient,
100116
],
101117
$subscriptionRequest->jsonSerialize(),
102118
);
103119
}
104120

105-
public function testPhaseJobSucceededWithWarningSubscription(): void
106-
{
121+
/** @dataProvider recipientProvider */
122+
public function testPhaseJobSucceededWithWarningSubscription(
123+
RecipientInterface $recipient,
124+
array $expectedRecipient
125+
): void {
107126
$subscriptionRequest = new Subscription(
108127
'phase-job-succeeded-with-warning',
109-
new EmailRecipient('john.doe@example.com'),
128+
$recipient,
110129
[
111130
new Filter('job.component.id', 'my.component'),
112131
new Filter('job.configuration.id', '12345'),
@@ -130,20 +149,18 @@ public function testPhaseJobSucceededWithWarningSubscription(): void
130149
'value' => '123',
131150
],
132151
],
133-
'recipient' => [
134-
'channel' => 'email',
135-
'address' => 'john.doe@example.com',
136-
],
152+
'recipient' => $expectedRecipient,
137153
],
138154
$subscriptionRequest->jsonSerialize(),
139155
);
140156
}
141157

142-
public function testJobSucceededSubscription(): void
158+
/** @dataProvider recipientProvider */
159+
public function testJobSucceededSubscription(RecipientInterface $recipient, array $expectedRecipient): void
143160
{
144161
$subscriptionRequest = new Subscription(
145162
'job-succeeded',
146-
new EmailRecipient('john.doe@example.com'),
163+
$recipient,
147164
[
148165
new Filter('job.component.id', 'my.component'),
149166
new Filter('job.configuration.id', '12345'),
@@ -163,20 +180,18 @@ public function testJobSucceededSubscription(): void
163180
'value' => '12345',
164181
],
165182
],
166-
'recipient' => [
167-
'channel' => 'email',
168-
'address' => 'john.doe@example.com',
169-
],
183+
'recipient' => $expectedRecipient,
170184
],
171185
$subscriptionRequest->jsonSerialize(),
172186
);
173187
}
174188

175-
public function testPhaseJobSucceededSubscription(): void
189+
/** @dataProvider recipientProvider */
190+
public function testPhaseJobSucceededSubscription(RecipientInterface $recipient, array $expectedRecipient): void
176191
{
177192
$subscriptionRequest = new Subscription(
178193
'phase-job-succeeded',
179-
new EmailRecipient('john.doe@example.com'),
194+
$recipient,
180195
[
181196
new Filter('job.component.id', 'my.component'),
182197
new Filter('job.configuration.id', '12345'),
@@ -201,20 +216,20 @@ public function testPhaseJobSucceededSubscription(): void
201216
'value' => '123',
202217
],
203218
],
204-
'recipient' => [
205-
'channel' => 'email',
206-
'address' => 'john.doe@example.com',
207-
],
219+
'recipient' => $expectedRecipient,
208220
],
209221
$subscriptionRequest->jsonSerialize(),
210222
);
211223
}
212224

213-
public function testPhaseJobProcessingLongSubscription(): void
214-
{
225+
/** @dataProvider recipientProvider */
226+
public function testPhaseJobProcessingLongSubscription(
227+
RecipientInterface $recipient,
228+
array $expectedRecipient
229+
): void {
215230
$subscriptionRequest = new Subscription(
216231
'phase-job-processing-long',
217-
new EmailRecipient('john.doe@example.com'),
232+
$recipient,
218233
[
219234
new Filter('job.component.id', 'my.component'),
220235
new Filter('job.configuration.id', '12345'),
@@ -238,10 +253,7 @@ public function testPhaseJobProcessingLongSubscription(): void
238253
'value' => '123',
239254
],
240255
],
241-
'recipient' => [
242-
'channel' => 'email',
243-
'address' => 'john.doe@example.com',
244-
],
256+
'recipient' => $expectedRecipient,
245257
],
246258
$subscriptionRequest->jsonSerialize(),
247259
);

0 commit comments

Comments
 (0)