|
| 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 | +} |
0 commit comments