Skip to content

Commit d8465e3

Browse files
committed
changes to make Mailer testable, tests for Mailer and a bugfix
1 parent 642b6d9 commit d8465e3

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

lib/MailTemplateGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static function mailTemplateCallToActionButton($mailTokens) {
116116
<table width="100%" border="0" cellpadding="0" cellspacing="0">
117117
<tr>
118118
<th valign="top" align="center" style="padding: 0px 0px 30px 0px; text-align: center; font-weight: normal;">
119-
<a href="{buttonLink}" style="display: inline-block; box-sizing: border-box; border-radius: 8px; background-color: <?php echo $buttonBackgroundColor; ?>; padding: 14px 18px 14px 18px; vertical-align: top; text-align: center; text-decoration: none;" target="_blank"><span style="font-size: 20px;line-height: 30px;color:<?php echo $buttonTextColor; ?>;font-weight:500;font-style:normal;display:inline-block;vertical-align: top;"><span style="display:inline-block;"><span style="font-family: <?php echo $fontFamily; ?>;line-height: 150%;">{buttonText}</span></span></span></span>
119+
<a href="{buttonLink}" style="display: inline-block; box-sizing: border-box; border-radius: 8px; background-color: <?php echo $buttonBackgroundColor; ?>; padding: 14px 18px 14px 18px; vertical-align: top; text-align: center; text-decoration: none;" target="_blank"><span style="font-size: 20px;line-height: 30px;color:<?php echo $buttonTextColor; ?>;font-weight:500;font-style:normal;display:inline-block;vertical-align: top;"><span style="display:inline-block;"><span style="font-family: <?php echo $fontFamily; ?>;line-height: 150%;">{buttonText}</span></span></span></a>
120120
</th>
121121
</tr>
122122
</table>

lib/Mailer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
use PHPMailer\PHPMailer\PHPMailer;
66

77
class Mailer {
8-
private static function getMailer() {
8+
public static $mailer = null;
9+
public static function getMailer() {
10+
if (self::$mailer) {
11+
return self::$mailer;
12+
}
913
$mailer = new PHPMailer();
1014
// Settings
1115
$mailer->IsSMTP();

tests/phpunit/MailerTest.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid;
3+
4+
use Pdsinterop\PhpSolid\Mailer;
5+
6+
const MAILER = [
7+
"host" => "mailerHost",
8+
"user" => "mailerUser",
9+
"password" => "mailerPass",
10+
"port" => "1337",
11+
"from" => "[email protected]"
12+
];
13+
14+
const MAILSTYLES = [];
15+
16+
const BASEURL = "https://example.com";
17+
18+
class MailerMock {
19+
public $Subject;
20+
public $Body;
21+
public $AltBody;
22+
public $addresses = [];
23+
24+
public function addAddress($address) {
25+
$this->addresses[] = $address;
26+
}
27+
public function send() {
28+
return true;
29+
}
30+
}
31+
32+
class MailerTest extends \PHPUnit\Framework\TestCase
33+
{
34+
public function testGetMailer() {
35+
$mailer = Mailer::getMailer();
36+
$this->assertInstanceOf('\PHPMailer\PHPMailer\PHPMailer', $mailer);
37+
$this->assertEquals($mailer->Host, MAILER['host']);
38+
$this->assertEquals($mailer->From, MAILER['from']);
39+
$this->assertEquals($mailer->Port, MAILER['port']);
40+
$this->assertEquals($mailer->Username, MAILER['user']);
41+
$this->assertEquals($mailer->Password, MAILER['password']);
42+
$this->assertEquals($mailer->SMTPAuth, true);
43+
$this->assertEquals($mailer->SMTPDebug, 0);
44+
$this->assertEquals($mailer->XMailer, null);
45+
$this->assertEquals($mailer->ContentType, "text/html");
46+
$this->assertEquals($mailer->Mailer, "smtp");
47+
}
48+
49+
public function testAccountCreated() {
50+
Mailer::$mailer = new MailerMock();
51+
Mailer::sendAccountCreated([
52+
'email' => '[email protected]',
53+
'webId' => 'aliceWebId'
54+
]);
55+
$this->assertContains("[email protected]", Mailer::$mailer->addresses);
56+
$this->assertMatchesRegularExpression("/aliceWebId/", Mailer::$mailer->AltBody);
57+
$this->assertMatchesRegularExpression("/aliceWebId/", Mailer::$mailer->Body);
58+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
59+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
60+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
61+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
62+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
63+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
64+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
65+
$doc = new \DOMDocument();
66+
$doc->loadHTML(Mailer::$mailer->Body);
67+
$this->assertEquals("Welkom bij Solid!", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
68+
}
69+
70+
public function testVerify() {
71+
Mailer::$mailer = new MailerMock();
72+
Mailer::sendVerify([
73+
'email' => '[email protected]',
74+
'code' => '654321'
75+
]);
76+
$this->assertContains("[email protected]", Mailer::$mailer->addresses);
77+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
78+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
79+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
80+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
81+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
82+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
83+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
84+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
85+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
86+
$doc = new \DOMDocument();
87+
$doc->loadHTML(Mailer::$mailer->Body);
88+
$this->assertEquals("Bevestig je e-mail", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
89+
}
90+
91+
public function testResetPassword() {
92+
Mailer::$mailer = new MailerMock();
93+
Mailer::sendResetPassword([
94+
'email' => '[email protected]',
95+
'code' => '654321'
96+
]);
97+
$this->assertContains("[email protected]", Mailer::$mailer->addresses);
98+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
99+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
100+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
101+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
102+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
103+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
104+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
105+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
106+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
107+
$doc = new \DOMDocument();
108+
$doc->loadHTML(Mailer::$mailer->Body);
109+
$this->assertEquals("Wachtwoordherstel", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
110+
}
111+
112+
public function testDeleteAccount() {
113+
Mailer::$mailer = new MailerMock();
114+
Mailer::sendDeleteAccount([
115+
'email' => '[email protected]',
116+
'code' => '654321'
117+
]);
118+
$this->assertContains("[email protected]", Mailer::$mailer->addresses);
119+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->AltBody);
120+
$this->assertMatchesRegularExpression("/654321/", Mailer::$mailer->Body);
121+
$this->assertMatchesRegularExpression("/<!-- Header -->/", Mailer::$mailer->Body);
122+
$this->assertMatchesRegularExpression("/<!-- Footer -->/", Mailer::$mailer->Body);
123+
$this->assertMatchesRegularExpression("/<!-- Call to action -->/", Mailer::$mailer->Body);
124+
$this->assertMatchesRegularExpression("|<html>|", Mailer::$mailer->Body);
125+
$this->assertMatchesRegularExpression("|<body style=.*>|", Mailer::$mailer->Body);
126+
$this->assertMatchesRegularExpression("|</body|", Mailer::$mailer->Body);
127+
$this->assertMatchesRegularExpression("|</html>|", Mailer::$mailer->Body);
128+
$doc = new \DOMDocument();
129+
$doc->loadHTML(Mailer::$mailer->Body);
130+
$this->assertEquals("Je account verwijderen", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
131+
}
132+
}

0 commit comments

Comments
 (0)