-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathNotifierTest.php
More file actions
302 lines (245 loc) · 10.1 KB
/
Copy pathNotifierTest.php
File metadata and controls
302 lines (245 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
namespace Bow\Tests\Notifier;
use Bow\View\View;
use Bow\Mail\Envelop;
use Bow\Notifier\Notifier;
use Bow\Database\Database;
use Bow\Database\Barry\Model;
use Bow\Database\Migration\Table;
use Bow\Mail\Mail;
use PHPUnit\Framework\TestCase;
use Bow\Tests\Config\TestingConfiguration;
use Bow\Tests\Database\Stubs\MigrationExtendedStub;
use Bow\Tests\Notifier\Stubs\MockChannelAdapter;
use Bow\Tests\Notifier\Stubs\TestNotifier;
use PHPUnit\Framework\MockObject\MockObject;
use Bow\Tests\Notifier\Stubs\TestNotifiableModel;
class NotifierTest extends TestCase
{
private TestNotifiableModel $context;
private MockObject|TestNotifier $notifier;
public static function setUpBeforeClass(): void
{
$config = TestingConfiguration::getConfig();
Database::configure($config["database"]);
Mail::configure($config["mail"]);
View::configure($config["view"]);
(new MigrationExtendedStub())->dropIfExists("notifications", false);
(new MigrationExtendedStub())->createIfNotExists("notifications", function (Table $table) {
$table->addIncrement('id', ["primary" => true]);
$table->addString('type');
$table->addString('concern_id');
$table->addString('concern_type');
$table->addText('data');
$table->addDatetime('read_at', ['nullable' => true]);
$table->addTimestamps();
}, false);
// Mock external notification channels to avoid requiring real credentials
Notifier::pushChannels([
'telegram' => MockChannelAdapter::class,
'slack' => MockChannelAdapter::class,
'sms' => MockChannelAdapter::class,
]);
}
protected function setUp(): void
{
parent::setUp();
$this->context = new TestNotifiableModel();
$this->notifier = $this->createMock(TestNotifier::class);
}
public function test_can_send_message_synchronously(): void
{
$this->notifier->expects($this->once())
->method('process')
->with($this->context);
$this->context->sendMessage($this->notifier);
}
public function test_message_sends_to_correct_channels(): void
{
$notifier = new TestNotifier();
$channels = $notifier->channels($this->context);
$this->assertIsArray($channels);
$this->assertCount(5, $channels);
$this->assertEquals(['mail', 'database', 'slack', 'sms', 'telegram'], $channels);
}
public function test_message_can_send_to_mail(): void
{
$message = new TestNotifier();
$mailMessage = $message->toMail($this->context);
$this->assertInstanceOf(Envelop::class, $mailMessage);
[$email] = $mailMessage->getTo();
$this->assertEquals('test@example.com', $email[1]);
$this->assertEquals('Test Message', $mailMessage->getSubject());
}
public function test_message_can_send_to_database(): void
{
$message = new TestNotifier();
$data = $message->toDatabase($this->context);
$this->assertIsArray($data);
$this->assertArrayHasKey('type', $data);
$this->assertArrayHasKey('data', $data);
$this->assertEquals('test_message', $data['type']);
$this->assertIsArray($data['data']);
$this->assertArrayHasKey('message', $data['data']);
$this->assertEquals('Test message content', $data['data']['message']);
}
public function test_message_can_send_to_slack(): void
{
$message = new TestNotifier();
$data = $message->toSlack($this->context);
$this->assertIsArray($data);
$this->assertArrayHasKey('webhook_url', $data);
$this->assertArrayHasKey('content', $data);
$this->assertEquals('https://hooks.slack.com/services/test', $data['webhook_url']);
$this->assertIsArray($data['content']);
$this->assertArrayHasKey('text', $data['content']);
$this->assertEquals('Test message for Slack', $data['content']['text']);
}
public function test_message_can_send_to_sms(): void
{
$message = new TestNotifier();
$data = $message->toSms($this->context);
$this->assertIsArray($data);
$this->assertArrayHasKey('to', $data);
$this->assertArrayHasKey('message', $data);
$this->assertEquals('+1234567890', $data['to']);
$this->assertEquals('Test SMS message', $data['message']);
}
public function test_message_can_send_to_telegram(): void
{
$message = new TestNotifier();
$data = $message->toTelegram($this->context);
$this->assertIsArray($data);
$this->assertArrayHasKey('chat_id', $data);
$this->assertArrayHasKey('message', $data);
$this->assertArrayHasKey('parse_mode', $data);
$this->assertEquals('123456789', $data['chat_id']);
$this->assertEquals('Test Telegram message', $data['message']);
$this->assertEquals('HTML', $data['parse_mode']);
}
public function test_process_calls_all_channels(): void
{
$message = $this->getMockBuilder(TestNotifier::class)
->onlyMethods(['channels', 'toMail', 'toDatabase'])
->getMock();
$envelop = (new Envelop())->to('test@example.com')->subject('Test')->message('Test message');
$message->expects($this->once())
->method('channels')
->willReturn(['mail', 'database']);
$message->expects($this->once())
->method('toMail')
->willReturn($envelop);
$message->expects($this->once())
->method('toDatabase')
->willReturn(['type' => 'test', 'data' => []]);
$message->process($this->context);
// Assert that the mock expectations were met
$this->assertTrue(true);
}
public function test_message_returns_empty_array_for_unconfigured_channels(): void
{
$messaging = new class extends Notifier {
public function channels(Model $context): array
{
return [];
}
};
$this->assertEquals([], $messaging->toDatabase($this->context));
$this->assertEquals([], $messaging->toSms($this->context));
$this->assertEquals([], $messaging->toSlack($this->context));
$this->assertEquals([], $messaging->toTelegram($this->context));
$this->assertNull($messaging->toMail($this->context));
}
public function test_can_push_custom_channels(): void
{
$customChannels = [
'custom' => \stdClass::class,
];
$result = Notifier::pushChannels($customChannels);
$this->assertIsArray($result);
$this->assertArrayHasKey('custom', $result);
$this->assertArrayHasKey('mail', $result);
$this->assertArrayHasKey('database', $result);
}
public function test_message_process_skips_invalid_channels(): void
{
$message = $this->getMockBuilder(TestNotifier::class)
->onlyMethods(['channels', 'toMail'])
->getMock();
$envelop = (new Envelop())->to('test@example.com')->subject('Test')->message('Test message');
$message->expects($this->once())
->method('channels')
->willReturn(['invalid_channel', 'mail']);
$message->expects($this->once())
->method('toMail')
->willReturn($envelop);
// Should not throw exception for invalid channel
$message->process($this->context);
}
public function test_mail_message_returns_correct_envelop_instance(): void
{
$message = new TestNotifier();
$mailMessage = $message->toMail($this->context);
$this->assertInstanceOf(Envelop::class, $mailMessage);
$this->assertNotNull($mailMessage->getSubject());
$this->assertNotEmpty($mailMessage->getTo());
}
public function test_database_message_has_required_structure(): void
{
$message = new TestNotifier();
$data = $message->toDatabase($this->context);
// Verify required structure
$this->assertIsArray($data);
$this->assertArrayHasKey('type', $data);
$this->assertIsString($data['type']);
$this->assertNotEmpty($data['type']);
}
public function test_slack_message_has_valid_webhook_url(): void
{
$message = new TestNotifier();
$data = $message->toSlack($this->context);
$this->assertArrayHasKey('webhook_url', $data);
$this->assertIsString($data['webhook_url']);
$this->assertStringStartsWith('https://', $data['webhook_url']);
}
public function test_sms_message_has_valid_phone_number(): void
{
$message = new TestNotifier();
$data = $message->toSms($this->context);
$this->assertArrayHasKey('to', $data);
$this->assertIsString($data['to']);
$this->assertStringStartsWith('+', $data['to']);
}
public function test_telegram_message_has_valid_parse_mode(): void
{
$message = new TestNotifier();
$data = $message->toTelegram($this->context);
$this->assertArrayHasKey('parse_mode', $data);
$this->assertContains($data['parse_mode'], ['HTML', 'Markdown', 'MarkdownV2']);
}
public function test_context_has_send_message_trait(): void
{
$this->assertTrue(
method_exists($this->context, 'sendMessage'),
'Context should have sendMessage method from SendNotifier trait'
);
$this->assertTrue(
method_exists($this->context, 'setMessageQueue'),
'Context should have setMessageQueue method from SendNotifier trait'
);
$this->assertTrue(
method_exists($this->context, 'sendMessageQueueOn'),
'Context should have sendMessageQueueOn method from SendNotifier trait'
);
}
public function test_channels_method_is_abstract_and_must_be_implemented(): void
{
$message = new TestNotifier();
$this->assertTrue(
method_exists($message, 'channels'),
'Message class must implement channels method'
);
$channels = $message->channels($this->context);
$this->assertIsArray($channels);
}
}