-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailerTest.php
More file actions
355 lines (299 loc) · 11.9 KB
/
MailerTest.php
File metadata and controls
355 lines (299 loc) · 11.9 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php declare(strict_types=1);
namespace Tests\Mail;
use PHPUnit\Framework\TestCase;
/**
* 测试 php-mailer\Mail\Mailer
*
* @author fdipzone
*/
final class MailerTest extends TestCase
{
/**
* @covers \Mail\Mailer::__construct
*/
public function testConstruct()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$this->assertEquals('Mail\Mailer', get_class($mailer));
}
/**
* @covers \Mail\Mailer::setSender
* @covers \Mail\Mailer::sender
*/
public function testSender()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$from_email = 'technology@zone.com';
$from_name = 'fdipzone';
$sender = new \Mail\Sender($from_email, $from_name);
$mailer->setSender($sender);
$mailer_sender = $mailer->sender();
$this->assertEquals($from_email, $mailer_sender->fromEmail());
$this->assertEquals($from_name, $mailer_sender->fromName());
$this->assertEquals('', $mailer_sender->senderEmail());
}
/**
* @covers \Mail\Mailer::addReplier
* @covers \Mail\Mailer::repliers
*/
public function testReplier()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$email = 'technology@zone.com';
$name = 'fdipzone';
$recipient = new \Mail\Recipient($email, $name);
$mailer->addReplier($recipient);
$mailer_repliers = $mailer->repliers();
$this->assertSame(1, count($mailer_repliers));
$this->assertEquals($email, $mailer_repliers[$email]->email());
$this->assertEquals($name, $mailer_repliers[$email]->name());
}
/**
* @covers \Mail\Mailer::addRecipient
* @covers \Mail\Mailer::recipients
*/
public function testRecipient()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$email = 'technology@zone.com';
$name = 'fdipzone';
$recipient = new \Mail\Recipient($email, $name);
$mailer->addRecipient($recipient);
$mailer_recipients = $mailer->recipients();
$this->assertSame(1, count($mailer_recipients));
$this->assertEquals($email, $mailer_recipients[$email]->email());
$this->assertEquals($name, $mailer_recipients[$email]->name());
}
/**
* @covers \Mail\Mailer::addCcRecipient
* @covers \Mail\Mailer::ccRecipients
*/
public function testCcRecipient()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$email = 'technology@zone.com';
$name = 'fdipzone';
$recipient = new \Mail\Recipient($email, $name);
$mailer->addCcRecipient($recipient);
$mailer_cc_recipients = $mailer->ccRecipients();
$this->assertSame(1, count($mailer_cc_recipients));
$this->assertEquals($email, $mailer_cc_recipients[$email]->email());
$this->assertEquals($name, $mailer_cc_recipients[$email]->name());
}
/**
* @covers \Mail\Mailer::addBccRecipient
* @covers \Mail\Mailer::bccRecipients
*/
public function testBccRecipient()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$email = 'technology@zone.com';
$name = 'fdipzone';
$recipient = new \Mail\Recipient($email, $name);
$mailer->addBccRecipient($recipient);
$mailer_bcc_recipients = $mailer->bccRecipients();
$this->assertSame(1, count($mailer_bcc_recipients));
$this->assertEquals($email, $mailer_bcc_recipients[$email]->email());
$this->assertEquals($name, $mailer_bcc_recipients[$email]->name());
}
/**
* @covers \Mail\Mailer::addAttachment
* @covers \Mail\Mailer::attachments
*/
public function testAttachment()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$attachment_file = '/tmp/attachment.txt';
file_put_contents($attachment_file, 'attachment file');
$name = '附件.txt';
$attachment = new \Mail\Attachment($attachment_file, $name);
$mailer->addAttachment($attachment);
$mailer_attachments = $mailer->attachments();
$this->assertSame(1, count($mailer_attachments));
$this->assertEquals($attachment_file, $mailer_attachments[$attachment_file]->file());
$this->assertEquals($name, $mailer_attachments[$attachment_file]->name());
if(file_exists($attachment_file))
{
unlink($attachment_file);
}
}
/**
* @covers \Mail\Mailer::setIsHtml
* @covers \Mail\Mailer::isHtml
*/
public function testIsHtml()
{
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
// 默认
$this->assertTrue($mailer->isHtml());
// 设置使用 html 格式发送
$mailer->setIsHtml(true);
$this->assertTrue($mailer->isHtml());
// 设置不使用 html 格式发送
$mailer->setIsHtml(false);
$this->assertFalse($mailer->isHtml());
}
/**
* @covers \Mail\Mailer::send
*/
public function testSend()
{
// 私有配置文件
$private_config_file = dirname(__FILE__).'/private.config';
// 没有私有配置文件,则不需要执行这个单元测试
if(!file_exists($private_config_file))
{
$this->markTestSkipped('This test should not be run.');
}
// 解析私有配置文件
$private_config = json_decode(file_get_contents($private_config_file), true);
$smtp_host = $private_config['smtp_host'];
$smtp_username = $private_config['smtp_username'];
$smtp_password = $private_config['smtp_password'];
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
// sender
$from_email = $private_config['from_email'];
$from_name = $private_config['from_name'];
$sender = new \Mail\Sender($from_email, $from_name);
$mailer->setSender($sender);
// replier
$replier_email = $private_config['replier_email'];
$replier_name = $private_config['replier_name'];
$replier = new \Mail\Recipient($replier_email, $replier_name);
$mailer->addReplier($replier);
// recipients
$recipient_email = $private_config['recipient_email'];
$recipient_name = $private_config['recipient_name'];
$recipient = new \Mail\Recipient($recipient_email, $recipient_name);
$mailer->addRecipient($recipient);
// cc recipients
$cc_recipient_email = $private_config['cc_recipient_email'];
$cc_recipient_name = $private_config['cc_recipient_name'];
$cc_recipient = new \Mail\Recipient($cc_recipient_email, $cc_recipient_name);
$mailer->addCcRecipient($cc_recipient);
// bcc recipients
$bcc_recipient_email = $private_config['bcc_recipient_email'];
$bcc_recipient_name = $private_config['bcc_recipient_name'];
$bcc_recipient = new \Mail\Recipient($bcc_recipient_email, $bcc_recipient_name);
$mailer->addBccRecipient($bcc_recipient);
// attachments
$attachment_file = '/tmp/attachment.txt';
file_put_contents($attachment_file, 'attachment file');
$attachment_name = '附件.txt';
$attachment = new \Mail\Attachment($attachment_file, $attachment_name);
$mailer->addAttachment($attachment);
// html
$mailer->setIsHtml(true);
// send
$subject = '测试电邮标题';
$body = '测试电邮内容';
// use mock php mailer
$ret = $mailer->send($subject, $body, new \Tests\Mail\MockPHPMailer);
$this->assertTrue($ret);
if(file_exists($attachment_file))
{
unlink($attachment_file);
}
}
/**
* @covers \Mail\Mailer::send
*/
public function testSendSubjectEmptyException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('mailer: email subject is empty');
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$subject = '';
$body = '测试电邮内容';
$mailer->send($subject, $body);
}
/**
* @covers \Mail\Mailer::send
*/
public function testSendBodyEmptyException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('mailer: email body is empty');
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$subject = '测试电邮标题';
$body = '';
$mailer->send($subject, $body);
}
/**
* @covers \Mail\Mailer::send
*/
public function testSendSenderNotSetupException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('mailer: sender not setup');
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$subject = '测试电邮标题';
$body = '测试电邮内容';
$mailer->send($subject, $body);
}
/**
* @covers \Mail\Mailer::send
*/
public function testSendRecipientNotSetupException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('mailer: recipient not setup');
$smtp_host = 'smtp.mxhichina.com';
$smtp_username = 'fdipzone';
$smtp_password = '123456abc!@#$';
$server_config = new \Mail\ServerConfig($smtp_host, $smtp_username, $smtp_password);
$mailer = new \Mail\Mailer($server_config);
$from_email = 'technology@zone.com';
$from_name = 'fdipzone';
$sender = new \Mail\Sender($from_email, $from_name);
$mailer->setSender($sender);
$subject = '测试电邮标题';
$body = '测试电邮内容';
$mailer->send($subject, $body);
}
}