4
4
5
5
use Mockery ;
6
6
use GuzzleHttp \Psr7 \Request ;
7
+ use GuzzleHttp \Psr7 \Response ;
7
8
use Minishlink \WebPush \WebPush ;
8
9
use Minishlink \WebPush \Subscription ;
9
10
use Minishlink \WebPush \MessageSentReport ;
10
- use NotificationChannels \WebPush \WebPushChannel ;
11
11
use NotificationChannels \WebPush \ReportHandler ;
12
- use GuzzleHttp \ Psr7 \ Response ;
12
+ use NotificationChannels \ WebPush \ WebPushChannel ;
13
13
14
14
class ChannelTest extends TestCase
15
15
{
16
- /** @var Mockery\Mock */
16
+ /** @var \ Mockery\MockInterface */
17
17
protected $ webPush ;
18
18
19
19
/** @var \NotificationChannels\WebPush\WebPushChannel */
@@ -24,28 +24,24 @@ public function setUp(): void
24
24
parent ::setUp ();
25
25
26
26
$ this ->webPush = Mockery::mock (WebPush::class);
27
-
28
27
$ this ->channel = new WebPushChannel ($ this ->webPush , new ReportHandler );
29
28
}
30
29
31
- public function tearDown (): void
32
- {
33
- Mockery::close ();
34
- parent ::tearDown ();
35
- }
36
-
37
30
/** @test */
38
- public function it_can_send_a_notification ()
31
+ public function notification_can_be_sent ()
39
32
{
33
+ $ message = ($ notification = new TestNotification )->toWebPush (null , null );
34
+
40
35
$ this ->webPush ->shouldReceive ('sendNotification ' )
41
36
->once ()
42
- ->withArgs (function (Subscription $ subscription , string $ payload , bool $ flush = false , array $ options = []) {
43
- $ this ->assertSame ($ this ->getPayload (), $ payload );
37
+ ->withArgs (function (Subscription $ subscription , string $ payload , bool $ flush , array $ options = []) use ($ message ) {
44
38
$ this ->assertInstanceOf (Subscription::class, $ subscription );
45
39
$ this ->assertEquals ('endpoint ' , $ subscription ->getEndpoint ());
46
40
$ this ->assertEquals ('key ' , $ subscription ->getPublicKey ());
47
41
$ this ->assertEquals ('token ' , $ subscription ->getAuthToken ());
48
42
$ this ->assertEquals ('aesgcm ' , $ subscription ->getContentEncoding ());
43
+ $ this ->assertSame ($ message ->getOptions (), $ options );
44
+ $ this ->assertSame (json_encode ($ message ->toArray ()), $ payload );
49
45
50
46
return true ;
51
47
})
@@ -59,78 +55,31 @@ public function it_can_send_a_notification()
59
55
60
56
$ this ->testUser ->updatePushSubscription ('endpoint ' , 'key ' , 'token ' , 'aesgcm ' );
61
57
62
- $ this ->channel ->send ($ this ->testUser , new TestNotification );
63
-
64
- $ this ->assertTrue (true );
58
+ $ this ->channel ->send ($ this ->testUser , $ notification );
65
59
}
66
60
67
61
/** @test */
68
- public function it_can_send_a_notification_with_options ()
62
+ public function subscriptions_with_invalid_endpoint_are_deleted ()
69
63
{
70
64
$ this ->webPush ->shouldReceive ('sendNotification ' )
71
- ->once ()
72
- ->withArgs (function ($ subscription , $ payload , $ flush , array $ options = []) {
73
- $ this ->assertSame (['ttl ' => 60 ], $ options );
74
-
75
- return true ;
76
- })
77
- ->andReturn (true );
78
-
79
- $ this ->webPush ->shouldReceive ('flush ' )
80
- ->once ()
81
- ->andReturn ((function () {
82
- yield new MessageSentReport (new Request ('POST ' , 'endpoint ' ), null , true );
83
- })());
84
-
85
- $ this ->testUser ->updatePushSubscription ('endpoint ' , 'key ' , 'token ' );
86
-
87
- $ this ->channel ->send ($ this ->testUser , new TestNotificationWithOptions );
88
-
89
- $ this ->assertTrue (true );
90
- }
91
-
92
- /** @test */
93
- public function it_will_delete_invalid_subscriptions ()
94
- {
95
- $ this ->webPush ->shouldReceive ('sendNotification ' )
96
- ->once ()
97
- // ->with('valid_endpoint', $this->getPayload(), null, null, [])
98
- ->andReturn (true );
99
-
100
- $ this ->webPush ->shouldReceive ('sendNotification ' )
101
- ->once ()
102
- // ->with('invalid_endpoint', $this->getPayload(), null, null, [])
103
- ->andReturn (true );
65
+ ->times (3 );
104
66
105
67
$ this ->webPush ->shouldReceive ('flush ' )
106
68
->once ()
107
69
->andReturn ((function () {
108
70
yield new MessageSentReport (new Request ('POST ' , 'valid_endpoint ' ), new Response (200 ), true );
109
- yield new MessageSentReport (new Request ('POST ' , 'invalid_endpoint ' ), new Response (404 ), false );
71
+ yield new MessageSentReport (new Request ('POST ' , 'invalid_endpoint2 ' ), new Response (404 ), false );
72
+ yield new MessageSentReport (new Request ('POST ' , 'invalid_endpoint1 ' ), new Response (410 ), false );
110
73
})());
111
74
112
75
$ this ->testUser ->updatePushSubscription ('valid_endpoint ' );
113
- $ this ->testUser ->updatePushSubscription ('invalid_endpoint ' );
76
+ $ this ->testUser ->updatePushSubscription ('invalid_endpoint1 ' );
77
+ $ this ->testUser ->updatePushSubscription ('invalid_endpoint2 ' );
114
78
115
79
$ this ->channel ->send ($ this ->testUser , new TestNotification );
116
80
117
- $ this ->assertFalse ($ this ->testUser ->pushSubscriptions ()->where ('endpoint ' , 'invalid_endpoint ' )->exists ());
118
81
$ this ->assertTrue ($ this ->testUser ->pushSubscriptions ()->where ('endpoint ' , 'valid_endpoint ' )->exists ());
119
- }
120
-
121
- /**
122
- * @return string
123
- */
124
- protected function getPayload ()
125
- {
126
- return json_encode ([
127
- 'title ' => 'Title ' ,
128
- 'actions ' => [
129
- ['title ' => 'Title ' , 'action ' => 'Action ' ],
130
- ],
131
- 'body ' => 'Body ' ,
132
- 'icon ' => 'Icon ' ,
133
- 'data ' => ['id ' => 1 ],
134
- ]);
82
+ $ this ->assertFalse ($ this ->testUser ->pushSubscriptions ()->where ('endpoint ' , 'invalid_endpoint1 ' )->exists ());
83
+ $ this ->assertFalse ($ this ->testUser ->pushSubscriptions ()->where ('endpoint ' , 'invalid_endpoint2 ' )->exists ());
135
84
}
136
85
}
0 commit comments