Skip to content

Commit f537295

Browse files
committed
Add events for notification sent/failed
1 parent 874b966 commit f537295

File tree

4 files changed

+127
-23
lines changed

4 files changed

+127
-23
lines changed

src/Events/NotificationFailed.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace NotificationChannels\WebPush\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
class NotificationFailed
8+
{
9+
use SerializesModels;
10+
11+
/**
12+
* @var \Minishlink\WebPush\MessageSentReport
13+
*/
14+
public $report;
15+
16+
/**
17+
* @var \NotificationChannels\WebPush\PushSubscription
18+
*/
19+
public $subscription;
20+
21+
/**
22+
* @var \NotificationChannels\WebPush\WebPushMessage
23+
*/
24+
public $message;
25+
26+
/**
27+
* Create a new event instance.
28+
*
29+
* @param \Minishlink\WebPush\MessageSentReport $report
30+
* @param \NotificationChannels\WebPush\PushSubscription $subscription
31+
* @param \NotificationChannels\WebPush\WebPushMessage $message
32+
* @return void
33+
*/
34+
public function __construct($report, $subscription, $message)
35+
{
36+
$this->report = $report;
37+
$this->subscription = $subscription;
38+
$this->message = $message;
39+
}
40+
}

src/Events/NotificationSent.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace NotificationChannels\WebPush\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
class NotificationSent
8+
{
9+
use SerializesModels;
10+
11+
/**
12+
* @var \Minishlink\WebPush\MessageSentReport
13+
*/
14+
public $report;
15+
16+
/**
17+
* @var \NotificationChannels\WebPush\PushSubscription
18+
*/
19+
public $subscription;
20+
21+
/**
22+
* @var \NotificationChannels\WebPush\WebPushMessage
23+
*/
24+
public $message;
25+
26+
/**
27+
* Create a new event instance.
28+
*
29+
* @param \Minishlink\WebPush\MessageSentReport $report
30+
* @param \NotificationChannels\WebPush\PushSubscription $subscription
31+
* @param \NotificationChannels\WebPush\WebPushMessage $message
32+
* @return void
33+
*/
34+
public function __construct($report, $subscription, $message)
35+
{
36+
$this->report = $report;
37+
$this->subscription = $subscription;
38+
$this->message = $message;
39+
}
40+
}

src/ReportHandler.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,28 @@
22

33
namespace NotificationChannels\WebPush;
44

5-
use Illuminate\Support\Facades\Log;
5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use NotificationChannels\WebPush\Events\NotificationFailed;
7+
use NotificationChannels\WebPush\Events\NotificationSent;
68

79
class ReportHandler implements ReportHandlerInterface
810
{
11+
/**
12+
* @var \Illuminate\Contracts\Events\Dispatcher
13+
*/
14+
protected $events;
15+
16+
/**
17+
* Create a new report handler.
18+
*
19+
* @param \Illuminate\Contracts\Events\Dispatcher $events
20+
* @return void
21+
*/
22+
public function __construct(Dispatcher $events)
23+
{
24+
$this->events = $events;
25+
}
26+
927
/**
1028
* Handle a message sent report.
1129
*
@@ -17,14 +35,14 @@ class ReportHandler implements ReportHandlerInterface
1735
public function handleReport($report, $subscription, $message)
1836
{
1937
if ($report->isSuccess()) {
38+
$this->events->dispatch(new NotificationSent($report, $subscription, $message));
2039
return;
2140
}
2241

2342
if ($report->isSubscriptionExpired()) {
2443
$subscription->delete();
25-
return;
2644
}
2745

28-
Log::warning("Notification failed to sent for subscription {$subscription->endpoint}: {$report->getReason()}");
46+
$this->events->dispatch(new NotificationFailed($report, $subscription, $message));
2947
}
3048
}

tests/ChannelTest.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,29 @@
44

55
use GuzzleHttp\Psr7\Request;
66
use GuzzleHttp\Psr7\Response;
7+
use Illuminate\Support\Facades\Event;
78
use Minishlink\WebPush\MessageSentReport;
89
use Minishlink\WebPush\Subscription;
910
use Minishlink\WebPush\WebPush;
1011
use Mockery;
12+
use NotificationChannels\WebPush\Events\NotificationFailed;
13+
use NotificationChannels\WebPush\Events\NotificationSent;
1114
use NotificationChannels\WebPush\ReportHandler;
1215
use NotificationChannels\WebPush\WebPushChannel;
1316

1417
class ChannelTest extends TestCase
1518
{
16-
/** @var \Mockery\MockInterface */
17-
protected $webPush;
18-
19-
/** @var \NotificationChannels\WebPush\WebPushChannel */
20-
protected $channel;
21-
22-
public function setUp(): void
23-
{
24-
parent::setUp();
25-
26-
$this->webPush = Mockery::mock(WebPush::class);
27-
$this->channel = new WebPushChannel($this->webPush, new ReportHandler);
28-
}
29-
3019
/** @test */
3120
public function notification_can_be_sent()
3221
{
22+
Event::fake();
23+
24+
/** @var mixed $webpush */
25+
$webpush = Mockery::mock(WebPush::class);
26+
$channel = new WebPushChannel($webpush, $this->app->make(ReportHandler::class));
3327
$message = ($notification = new TestNotification)->toWebPush(null, null);
3428

35-
$this->webPush->shouldReceive('queueNotification')
29+
$webpush->shouldReceive('queueNotification')
3630
->once()
3731
->withArgs(function (Subscription $subscription, string $payload, array $options = [], array $auth = []) use ($message) {
3832
$this->assertInstanceOf(Subscription::class, $subscription);
@@ -47,24 +41,32 @@ public function notification_can_be_sent()
4741
})
4842
->andReturn(true);
4943

50-
$this->webPush->shouldReceive('flush')
44+
$webpush->shouldReceive('flush')
5145
->once()
5246
->andReturn((function () {
5347
yield new MessageSentReport(new Request('POST', 'endpoint'), null, true);
5448
})());
5549

5650
$this->testUser->updatePushSubscription('endpoint', 'key', 'token', 'aesgcm');
5751

58-
$this->channel->send($this->testUser, $notification);
52+
$channel->send($this->testUser, $notification);
53+
54+
Event::assertDispatched(NotificationSent::class);
5955
}
6056

6157
/** @test */
6258
public function subscriptions_with_invalid_endpoint_are_deleted()
6359
{
64-
$this->webPush->shouldReceive('queueNotification')
60+
Event::fake();
61+
62+
/** @var mixed $webpush */
63+
$webpush = Mockery::mock(WebPush::class);
64+
$channel = new WebPushChannel($webpush, $this->app->make(ReportHandler::class));
65+
66+
$webpush->shouldReceive('queueNotification')
6567
->times(3);
6668

67-
$this->webPush->shouldReceive('flush')
69+
$webpush->shouldReceive('flush')
6870
->once()
6971
->andReturn((function () {
7072
yield new MessageSentReport(new Request('POST', 'valid_endpoint'), new Response(200), true);
@@ -76,10 +78,14 @@ public function subscriptions_with_invalid_endpoint_are_deleted()
7678
$this->testUser->updatePushSubscription('invalid_endpoint1');
7779
$this->testUser->updatePushSubscription('invalid_endpoint2');
7880

79-
$this->channel->send($this->testUser, new TestNotification);
81+
$channel->send($this->testUser, new TestNotification);
8082

8183
$this->assertTrue($this->testUser->pushSubscriptions()->where('endpoint', 'valid_endpoint')->exists());
8284
$this->assertFalse($this->testUser->pushSubscriptions()->where('endpoint', 'invalid_endpoint1')->exists());
8385
$this->assertFalse($this->testUser->pushSubscriptions()->where('endpoint', 'invalid_endpoint2')->exists());
86+
87+
Event::assertDispatched(NotificationSent::class);
88+
Event::assertDispatched(NotificationFailed::class);
89+
Event::assertDispatched(NotificationFailed::class);
8490
}
8591
}

0 commit comments

Comments
 (0)