Skip to content

Commit 38e6fb4

Browse files
committed
test: add tests for Declarative Web Push messages
1 parent cd7147f commit 38e6fb4

File tree

3 files changed

+279
-0
lines changed

3 files changed

+279
-0
lines changed

tests/ChannelTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,44 @@ public function notification_can_be_sent(): void
5555
Event::assertDispatched(NotificationSent::class);
5656
}
5757

58+
#[Test]
59+
public function declarative_notification_can_be_sent(): void
60+
{
61+
Event::fake();
62+
63+
/** @var mixed $webpush */
64+
$webpush = Mockery::mock(WebPush::class);
65+
$channel = new WebPushChannel($webpush, $this->app->make(ReportHandler::class));
66+
$message = ($notification = new TestDeclarativeNotification)->toWebPush(null, null);
67+
68+
$webpush->shouldReceive('queueNotification')
69+
->once()
70+
->withArgs(function (Subscription $subscription, string $payload, array $options, array $auth = []) use ($message): true {
71+
$this->assertInstanceOf(Subscription::class, $subscription);
72+
$this->assertEquals('endpoint', $subscription->getEndpoint());
73+
$this->assertEquals('key', $subscription->getPublicKey());
74+
$this->assertEquals('token', $subscription->getAuthToken());
75+
$this->assertEquals('aesgcm', $subscription->getContentEncoding());
76+
$this->assertSame($message->getOptions(), $options);
77+
$this->assertSame(json_encode($message->toArray()), $payload);
78+
79+
return true;
80+
})
81+
->andReturn(true);
82+
83+
$webpush->shouldReceive('flush')
84+
->once()
85+
->andReturn((function () {
86+
yield new MessageSentReport(new Request('POST', 'endpoint'), null, true);
87+
})());
88+
89+
$this->testUser->updatePushSubscription('endpoint', 'key', 'token', 'aesgcm');
90+
91+
$channel->send($this->testUser, $notification);
92+
93+
Event::assertDispatched(NotificationSent::class);
94+
}
95+
5896
#[Test]
5997
public function subscriptions_with_invalid_endpoint_are_deleted(): void
6098
{

tests/DeclarativeMessageTest.php

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
3+
namespace NotificationChannels\WebPush\Test;
4+
5+
use NotificationChannels\WebPush\DeclarativeWebPushMessage;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\TestCase;
8+
use NotificationChannels\WebPush\Exceptions\MessageValidationFailed;
9+
10+
class DeclarativeMessageTest extends TestCase
11+
{
12+
protected DeclarativeWebPushMessage $message;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->message = new DeclarativeWebPushMessage;
19+
$this->message->title('Message title');
20+
$this->message->navigate('https://example.com');
21+
}
22+
23+
#[Test]
24+
public function title_can_be_set(): void
25+
{
26+
$this->message->title('New message title');
27+
28+
$this->assertEquals('New message title', $this->message->toArray()['notification']['title']);
29+
}
30+
31+
#[Test]
32+
public function title_must_be_set(): void
33+
{
34+
$this->message = new DeclarativeWebPushMessage;
35+
36+
$this->expectException(MessageValidationFailed::class);
37+
38+
$this->expectExceptionMessage('title');
39+
40+
$this->message->toArray();
41+
}
42+
43+
#[Test]
44+
public function navigate_can_be_set(): void
45+
{
46+
$this->message->navigate('https://new-example.com');
47+
48+
$this->assertEquals('https://new-example.com', $this->message->toArray()['notification']['navigate']);
49+
}
50+
51+
#[Test]
52+
public function navigate_must_be_set(): void
53+
{
54+
$this->message = new DeclarativeWebPushMessage;
55+
56+
$this->message->title('Message title');
57+
58+
$this->expectException(MessageValidationFailed::class);
59+
60+
$this->expectExceptionMessage('navigate');
61+
62+
$this->message->toArray();
63+
}
64+
65+
#[Test]
66+
public function action_can_be_set(): void
67+
{
68+
$this->message->action('Some Action', 'some_action', 'https://example.com/action');
69+
70+
$this->assertEquals(
71+
[['title' => 'Some Action', 'action' => 'some_action', 'navigate' => 'https://example.com/action']], $this->message->toArray()['notification']['actions']
72+
);
73+
}
74+
75+
#[Test]
76+
public function action_can_be_set_with_icon(): void
77+
{
78+
$this->message->action('Some Action', 'some_action', 'https://example.com/action', '/icon.png');
79+
80+
$this->assertEquals(
81+
[['title' => 'Some Action', 'action' => 'some_action', 'navigate' => 'https://example.com/action', 'icon' => '/icon.png']], $this->message->toArray()['notification']['actions']
82+
);
83+
}
84+
85+
#[Test]
86+
public function badge_can_be_set(): void
87+
{
88+
$this->message->badge('/badge.jpg');
89+
90+
$this->assertEquals('/badge.jpg', $this->message->toArray()['notification']['badge']);
91+
}
92+
93+
#[Test]
94+
public function body_can_be_set(): void
95+
{
96+
$this->message->body('Message body');
97+
98+
$this->assertEquals('Message body', $this->message->toArray()['notification']['body']);
99+
}
100+
101+
#[Test]
102+
public function direction_can_be_set(): void
103+
{
104+
$this->message->dir('rtl');
105+
106+
$this->assertEquals('rtl', $this->message->toArray()['notification']['dir']);
107+
}
108+
109+
#[Test]
110+
public function icon_can_be_set(): void
111+
{
112+
$this->message->icon('/icon.jpg');
113+
114+
$this->assertEquals('/icon.jpg', $this->message->toArray()['notification']['icon']);
115+
}
116+
117+
#[Test]
118+
public function image_can_be_set(): void
119+
{
120+
$this->message->image('/image.jpg');
121+
122+
$this->assertEquals('/image.jpg', $this->message->toArray()['notification']['image']);
123+
}
124+
125+
#[Test]
126+
public function lang_can_be_set(): void
127+
{
128+
$this->message->lang('en');
129+
130+
$this->assertEquals('en', $this->message->toArray()['notification']['lang']);
131+
}
132+
133+
#[Test]
134+
public function mutable_can_be_set(): void
135+
{
136+
$this->message->mutable();
137+
138+
$this->assertTrue($this->message->toArray()['mutable']);
139+
}
140+
141+
#[Test]
142+
public function renotify_can_be_set(): void
143+
{
144+
$this->message->renotify();
145+
146+
$this->assertTrue($this->message->toArray()['notification']['renotify']);
147+
}
148+
149+
#[Test]
150+
public function require_interaction_can_be_set(): void
151+
{
152+
$this->message->requireInteraction();
153+
154+
$this->assertTrue($this->message->toArray()['notification']['requireInteraction']);
155+
}
156+
157+
#[Test]
158+
public function silent_can_be_set(): void
159+
{
160+
$this->message->silent();
161+
162+
$this->assertTrue($this->message->toArray()['notification']['silent']);
163+
}
164+
165+
#[Test]
166+
public function tag_can_be_set(): void
167+
{
168+
$this->message->tag('tag1');
169+
170+
$this->assertEquals('tag1', $this->message->toArray()['notification']['tag']);
171+
}
172+
173+
#[Test]
174+
public function timestamp_can_be_set(): void
175+
{
176+
$this->message->timestamp(1763059844);
177+
178+
$this->assertEquals(1763059844, $this->message->toArray()['notification']['timestamp']);
179+
}
180+
181+
#[Test]
182+
public function vibration_pattern_can_be_set(): void
183+
{
184+
$this->message->vibrate([1, 2, 3]);
185+
186+
$this->assertEquals([1, 2, 3], $this->message->toArray()['notification']['vibrate']);
187+
}
188+
189+
#[Test]
190+
public function arbitrary_data_can_be_set(): void
191+
{
192+
$this->message->data(['id' => 1]);
193+
194+
$this->assertEquals(['id' => 1], $this->message->toArray()['notification']['data']);
195+
}
196+
197+
#[Test]
198+
public function payload_is_declarative_message(): void
199+
{
200+
$this->assertEquals(8030, $this->message->toArray()['web_push']);
201+
}
202+
203+
#[Test]
204+
public function options_can_be_set(): void
205+
{
206+
$this->message->options(['ttl' => 60]);
207+
208+
$this->assertEquals(['ttl' => 60], $this->message->getOptions());
209+
$this->assertArrayNotHasKey('options', $this->message->toArray());
210+
}
211+
212+
#[Test]
213+
public function options_contain_json_content_type(): void
214+
{
215+
$this->assertEquals(['contentType' => 'application/json'], $this->message->getOptions());
216+
}
217+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace NotificationChannels\WebPush\Test;
4+
5+
use Illuminate\Notifications\Notification;
6+
use NotificationChannels\WebPush\DeclarativeWebPushMessage;
7+
8+
class TestDeclarativeNotification extends Notification
9+
{
10+
/**
11+
* Get the declarative web push representation of the notification.
12+
*/
13+
public function toWebPush(mixed $notifiable, mixed $notification): DeclarativeWebPushMessage
14+
{
15+
return (new DeclarativeWebPushMessage)
16+
->data(['id' => 1])
17+
->title('Title')
18+
->navigate('https://example.com')
19+
->icon('Icon')
20+
->body('Body')
21+
->action('Title', 'Action', 'https://example.com/action')
22+
->options(['ttl' => 60]);
23+
}
24+
}

0 commit comments

Comments
 (0)