-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.php
124 lines (115 loc) · 3.34 KB
/
tests.php
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
<?php /** @noinspection PhpUnused PhpUnhandledExceptionInspection PhpRedundantOptionalArgumentInspection */
namespace Email;
require "vendor/autoload.php";
use Nose;
function testQuotedPrintable()
{
foreach([
"Hêlló, wörld!",
"Hello,\r\nworld!",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 76 characters
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 77 characters
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaö", // 75 characters + special character
] as $sample)
{
Nose::assertEquals($sample, EncodingQuotedPrintable::decode(EncodingQuotedPrintable::encode($sample)));
}
}
function testEncodedWords()
{
foreach([
"=?UTF-8?B?VGhpcyBpcyBhIGhvcnNleTog8J+Qjg==?=",
"=?UTF-8?Q?This_is_a_horsey:_=F0=9F=90=8E?=",
] as $subject)
{
$email = new Email([
"Subject: $subject"
]);
Nose::assertEquals($email->getSubject(), "This is a horsey: \u{0001F40E}");
}
}
function testAddressParsing()
{
// DO NOT SEND EMAILS TO php-mail-transfer@nirvana.admins.ws
// This is a trap for spam crawlers!
$str = "Test <php-mail-transfer@nirvana.admins.ws>";
$address = new Address($str, null);
Nose::assertEquals($address->__toString(), $str);
Nose::assertEquals($address->name, "Test");
Nose::assertEquals($address->address, "php-mail-transfer@nirvana.admins.ws");
Nose::assertEquals((new Address("<php-mail-transfer@nirvana.admins.ws>"))->address, "php-mail-transfer@nirvana.admins.ws");
}
function testHeaderCasing()
{
$headers = [
"From",
"To",
"Date",
"MIME-Version",
"Content-Type",
"Content-Transfer-Encoding",
"Subject",
"DKIM-Signature",
"Message-ID",
"In-Reply-To",
"References",
];
foreach($headers as $header)
{
Nose::assertEquals(Email::normaliseHeaderCasing(strtolower($header)), $header);
}
}
function testSmtpData()
{
$email = new Email([
"Test: ".str_repeat("a", 50)." ".str_repeat("a", 50)
], new ContentTextPlain(str_repeat("a", 200)));
$smtp_data = $email->getSmtpData(78);
foreach(explode("\r\n", $smtp_data) as $line)
{
Nose::assertTrue(strlen($line) <= 80);
}
$email_from_data = Email::fromSmtpData($smtp_data);
Nose::assertEquals($email->getFirstHeaderValue("Test"), $email_from_data->getFirstHeaderValue("Test"));
Nose::assertEquals($email->content->text, $email_from_data->content->text);
}
function testUnfoldableHeader()
{
$email = new Email([
"Test" => str_repeat("a", 200)
]);
$smtp_data = $email->getSmtpData(78);
$i = 0;
foreach(explode("\r\n", $smtp_data) as $line)
{
if($line)
{
$i = 0;
}
else
{
Nose::assertTrue(++$i < 3);
}
}
}
function testNlSmtpData()
{
$text = "Hello, this is a more nuanced text.\r\n\r\nIn that it has multiple lines.\r\n\r\nAnd somewhat lengthy text, that isn't (too) contrived.";
$email = new Email();
$email->content = new ContentTextPlain($text);
$email_from_data = Email::fromSmtpData($email->getSmtpData());
Nose::assertEquals($email_from_data->content->text, $text);
}
function testDate()
{
$email = new Email();
$email->setDate(1337);
Nose::assertEquals($email->getDate(), 1337);
}
function testLookups()
{
// With MX Record
Nose::assertEquals(["mxresult.hell.sh"], (new Address("nobody@mxtest.hell.sh"))->getServers());
// Without MX Record
Nose::assertEquals(["trash-mail.com"], (new Address("nobody@trash-mail.com"))->getServers());
}