4
4
5
5
use GuzzleHttp \Psr7 \Request ;
6
6
use GuzzleHttp \Psr7 \Response ;
7
+ use Illuminate \Support \Facades \Event ;
7
8
use Minishlink \WebPush \MessageSentReport ;
8
9
use Minishlink \WebPush \Subscription ;
9
10
use Minishlink \WebPush \WebPush ;
10
11
use Mockery ;
12
+ use NotificationChannels \WebPush \Events \NotificationFailed ;
13
+ use NotificationChannels \WebPush \Events \NotificationSent ;
11
14
use NotificationChannels \WebPush \ReportHandler ;
12
15
use NotificationChannels \WebPush \WebPushChannel ;
13
16
14
17
class ChannelTest extends TestCase
15
18
{
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
-
30
19
/** @test */
31
20
public function notification_can_be_sent ()
32
21
{
22
+ Event::fake ();
23
+
24
+ /** @var mixed $webpush */
25
+ $ webpush = Mockery::mock (WebPush::class);
26
+ $ channel = new WebPushChannel ($ webpush , $ this ->app ->make (ReportHandler::class));
33
27
$ message = ($ notification = new TestNotification )->toWebPush (null , null );
34
28
35
- $ this -> webPush ->shouldReceive ('queueNotification ' )
29
+ $ webpush ->shouldReceive ('queueNotification ' )
36
30
->once ()
37
31
->withArgs (function (Subscription $ subscription , string $ payload , array $ options = [], array $ auth = []) use ($ message ) {
38
32
$ this ->assertInstanceOf (Subscription::class, $ subscription );
@@ -47,24 +41,32 @@ public function notification_can_be_sent()
47
41
})
48
42
->andReturn (true );
49
43
50
- $ this -> webPush ->shouldReceive ('flush ' )
44
+ $ webpush ->shouldReceive ('flush ' )
51
45
->once ()
52
46
->andReturn ((function () {
53
47
yield new MessageSentReport (new Request ('POST ' , 'endpoint ' ), null , true );
54
48
})());
55
49
56
50
$ this ->testUser ->updatePushSubscription ('endpoint ' , 'key ' , 'token ' , 'aesgcm ' );
57
51
58
- $ this ->channel ->send ($ this ->testUser , $ notification );
52
+ $ channel ->send ($ this ->testUser , $ notification );
53
+
54
+ Event::assertDispatched (NotificationSent::class);
59
55
}
60
56
61
57
/** @test */
62
58
public function subscriptions_with_invalid_endpoint_are_deleted ()
63
59
{
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 ' )
65
67
->times (3 );
66
68
67
- $ this -> webPush ->shouldReceive ('flush ' )
69
+ $ webpush ->shouldReceive ('flush ' )
68
70
->once ()
69
71
->andReturn ((function () {
70
72
yield new MessageSentReport (new Request ('POST ' , 'valid_endpoint ' ), new Response (200 ), true );
@@ -76,10 +78,14 @@ public function subscriptions_with_invalid_endpoint_are_deleted()
76
78
$ this ->testUser ->updatePushSubscription ('invalid_endpoint1 ' );
77
79
$ this ->testUser ->updatePushSubscription ('invalid_endpoint2 ' );
78
80
79
- $ this -> channel ->send ($ this ->testUser , new TestNotification );
81
+ $ channel ->send ($ this ->testUser , new TestNotification );
80
82
81
83
$ this ->assertTrue ($ this ->testUser ->pushSubscriptions ()->where ('endpoint ' , 'valid_endpoint ' )->exists ());
82
84
$ this ->assertFalse ($ this ->testUser ->pushSubscriptions ()->where ('endpoint ' , 'invalid_endpoint1 ' )->exists ());
83
85
$ 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);
84
90
}
85
91
}
0 commit comments