Skip to content

Commit 40b7584

Browse files
committed
Added tests for non-connectivity related functions
1 parent 64cd753 commit 40b7584

File tree

8 files changed

+556
-7
lines changed

8 files changed

+556
-7
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"technicalguru/utils" : "~1",
2020
"phpmailer/phpmailer" : "^6.1",
2121
"symfony/polyfill-mbstring" : "^1.20",
22-
"technicalguru/database" : "~0"
22+
"technicalguru/database" : "~0",
23+
"phpunit/phpunit": "^9.4"
2324
},
2425
"autoload" : {
2526
"psr-4" : {
@@ -31,6 +32,5 @@
3132
}
3233
},
3334
"require-dev" : {
34-
"phpunit/phpunit" : "^9"
3535
}
36-
}
36+
}

src/TgEmail/Config/SmtpConfig.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function getCharset() {
105105
}
106106

107107
public function setCharset($charset) {
108-
$this->charset;
108+
$this->charset = $charset;
109109
return $this;
110110
}
111111

@@ -154,12 +154,31 @@ public static function from($config) {
154154
} else {
155155
throw new EmailException('Cannot configure credentialsProvider from given config');
156156
}
157+
} else if (isset($config->credentials)) {
158+
$username = NULL;
159+
$password = NULL;
160+
if (isset($config->credentials->username)) {
161+
$username = $config->credentials->username;
162+
}
163+
if (isset($config->credentials->user)) {
164+
$username = $config->credentials->user;
165+
}
166+
if (isset($config->credentials->password)) {
167+
$password = $config->credentials->password;
168+
}
169+
if (isset($config->credentials->passwd)) {
170+
$password = $config->credentials->passwd;
171+
}
172+
if (isset($config->credentials->pass)) {
173+
$password = $config->credentials->pass;
174+
}
175+
$rc->setCredentials($username, $password);
157176
}
158177
if (isset($config->secureOption)) {
159178
$rc->setSecureOption($config->secureOption);
160179
}
161180
if (isset($config->charset)) {
162-
$rc->setPort($config->charset);
181+
$rc->setCharset($config->charset);
163182
}
164183
return $rc;
165184
}

src/TgEmail/EmailAddress.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __toString() {
4242
if ($this->name != NULL) {
4343
return $this->name . ' <' . $this->email . '>';
4444
}
45-
return $this->email;
45+
return '<' . $this->email . '>';
4646
}
4747

4848
/**
@@ -75,7 +75,7 @@ public static function from($s, $name = NULL) {
7575
} else if (is_a($s, 'TgEmail\\EmailAddress')) {
7676
return $s;
7777
} else if (is_object($s)) {
78-
return new EmailAddress($s-email, $s->name);
78+
return new EmailAddress($s->email, $s->name);
7979
}
8080
}
8181
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgEmail\Config;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use TgEmail\EmailAddress;
8+
9+
/**
10+
* Tests the BccConfig.
11+
*
12+
* @author ralph
13+
*
14+
*/
15+
class BccConfigTest extends TestCase {
16+
17+
public function testAddRecipientsWithString(): void {
18+
$config = new BccConfig();
19+
$config->addRecipients('John Doe <john.doe@example.com');
20+
21+
$recipients = $config->getRecipients();
22+
$this->assertEquals(1, count($recipients));
23+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
24+
}
25+
26+
public function testAddRecipientsWithObject(): void {
27+
$config = new BccConfig();
28+
$config->addRecipients(EmailAddress::from('John Doe <john.doe@example.com'));
29+
30+
$recipients = $config->getRecipients();
31+
$this->assertEquals(1, count($recipients));
32+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
33+
}
34+
35+
public function testAddRecipientsWithArray(): void {
36+
$config = new BccConfig();
37+
$config->addRecipients(array(
38+
EmailAddress::from('John Doe <john.doe@example.com'),
39+
EmailAddress::from('Jane Doe <jane.doe@example.com'),
40+
));
41+
42+
$recipients = $config->getRecipients();
43+
$this->assertEquals(2, count($recipients));
44+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
45+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
46+
}
47+
48+
public function testFromWithArray(): void {
49+
$origConfig = array(
50+
'recipients' => array(
51+
'John Doe <john.doe@example.com',
52+
array(
53+
'name' => 'Jane Doe',
54+
'email' => 'jane.doe@example.com',
55+
),
56+
),
57+
);
58+
59+
$config = BccConfig::from($origConfig);
60+
$recipients = $config->getRecipients();
61+
$this->assertEquals(2, count($recipients));
62+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
63+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
64+
}
65+
66+
public function testFromWithString(): void {
67+
$origConfig = self::getJsonTestString();
68+
69+
$config = BccConfig::from($origConfig);
70+
$recipients = $config->getRecipients();
71+
$this->assertEquals(2, count($recipients));
72+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
73+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
74+
}
75+
76+
public function testFromWithObject(): void {
77+
$origConfig = json_decode(self::getJsonTestString());
78+
79+
$config = BccConfig::from($origConfig);
80+
$recipients = $config->getRecipients();
81+
$this->assertEquals(2, count($recipients));
82+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
83+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
84+
}
85+
86+
public static function getJsonTestString() {
87+
return '{"recipients":["John Doe <john.doe@example.com",{"name":"Jane Doe","email":"jane.doe@example.com"}]}';
88+
}
89+
}
90+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgEmail\Config;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use TgEmail\EmailAddress;
8+
9+
/**
10+
* Tests the RerouteConfig.
11+
*
12+
* @author ralph
13+
*
14+
*/
15+
class RerouteConfigTest extends TestCase {
16+
17+
public function testAddRecipientsWithString(): void {
18+
$config = new RerouteConfig();
19+
$config->addRecipients('John Doe <john.doe@example.com');
20+
21+
$recipients = $config->getRecipients();
22+
$this->assertEquals(1, count($recipients));
23+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
24+
}
25+
26+
public function testAddRecipientsWithObject(): void {
27+
$config = new RerouteConfig();
28+
$config->addRecipients(EmailAddress::from('John Doe <john.doe@example.com'));
29+
30+
$recipients = $config->getRecipients();
31+
$this->assertEquals(1, count($recipients));
32+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
33+
}
34+
35+
public function testAddRecipientsWithArray(): void {
36+
$config = new RerouteConfig();
37+
$config->addRecipients(array(
38+
EmailAddress::from('John Doe <john.doe@example.com'),
39+
EmailAddress::from('Jane Doe <jane.doe@example.com'),
40+
));
41+
42+
$recipients = $config->getRecipients();
43+
$this->assertEquals(2, count($recipients));
44+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
45+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
46+
}
47+
48+
public function testSetSubjectPrefix(): void {
49+
$config = new RerouteConfig();
50+
$config->setSubjectPrefix('A specific prefix');
51+
$this->assertEquals('A specific prefix', $config->getSubjectPrefix());
52+
}
53+
54+
public function testFromWithArray(): void {
55+
$origConfig = array(
56+
'recipients' => array(
57+
'John Doe <john.doe@example.com',
58+
array(
59+
'name' => 'Jane Doe',
60+
'email' => 'jane.doe@example.com',
61+
),
62+
),
63+
'subjectPrefix' => 'A specific prefix',
64+
);
65+
66+
$config = RerouteConfig::from($origConfig);
67+
$recipients = $config->getRecipients();
68+
$this->assertEquals(2, count($recipients));
69+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
70+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
71+
$this->assertEquals('A specific prefix', $config->getSubjectPrefix());
72+
}
73+
74+
public function testFromWithString(): void {
75+
$origConfig = self::getJsonTestString();
76+
77+
$config = RerouteConfig::from($origConfig);
78+
$recipients = $config->getRecipients();
79+
$this->assertEquals(2, count($recipients));
80+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
81+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
82+
$this->assertEquals('A specific prefix', $config->getSubjectPrefix());
83+
}
84+
85+
public function testFromWithObject(): void {
86+
$origConfig = json_decode(self::getJsonTestString());
87+
88+
$config = RerouteConfig::from($origConfig);
89+
$recipients = $config->getRecipients();
90+
$this->assertEquals(2, count($recipients));
91+
$this->assertEquals('John Doe <john.doe@example.com>', $recipients[0]->__toString());
92+
$this->assertEquals('Jane Doe <jane.doe@example.com>', $recipients[1]->__toString());
93+
$this->assertEquals('A specific prefix', $config->getSubjectPrefix());
94+
}
95+
96+
public static function getJsonTestString() {
97+
return '{"recipients":["John Doe <john.doe@example.com",{"name":"Jane Doe","email":"jane.doe@example.com"}],"subjectPrefix":"A specific prefix"}';
98+
}
99+
}
100+
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgEmail\Config;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use TgEmail\EmailAddress;
8+
use PHPMailer\PHPMailer\PHPMailer;
9+
use PHPMailer\PHPMailer\SMTP;
10+
11+
/**
12+
* Tests the SmtpConfig.
13+
*
14+
* @author ralph
15+
*
16+
*/
17+
class SmtpConfigTest extends TestCase {
18+
19+
public function testSetHost(): void {
20+
$config = new SmtpConfig();
21+
$config->setHost('www.example.com');
22+
$this->assertEquals('www.example.com', $config->getHost());
23+
}
24+
25+
public function testSetPort(): void {
26+
$config = new SmtpConfig();
27+
$config->setPort(587);
28+
$this->assertEquals(587, $config->getPort());
29+
}
30+
31+
public function testSetDebugLevel(): void {
32+
$config = new SmtpConfig();
33+
$config->setDebuglevel(SMTP::DEBUG_SERVER);
34+
$this->assertEquals(SMTP::DEBUG_SERVER, $config->getDebuglevel());
35+
}
36+
37+
38+
public function testSetAuth(): void {
39+
$config = new SmtpConfig();
40+
$config->setAuth(TRUE);
41+
$this->assertTrue($config->isAuth());
42+
}
43+
44+
45+
public function testSetCredentials(): void {
46+
$config = new SmtpConfig();
47+
$config->setCredentials('username', 'password');
48+
$this->assertEquals('username', $config->getUsername());
49+
$this->assertEquals('password', $config->getPassword());
50+
}
51+
52+
53+
public function testSetSecureOption(): void {
54+
$config = new SmtpConfig();
55+
$config->setSecureOption(PHPMailer::ENCRYPTION_STARTTLS);
56+
$this->assertEquals(PHPMailer::ENCRYPTION_STARTTLS, $config->getSecureOption());
57+
}
58+
59+
60+
public function testSetCharset(): void {
61+
$config = new SmtpConfig();
62+
$config->setCharset('utf8');
63+
$this->assertEquals('utf8', $config->getCharset());
64+
}
65+
66+
public function testFromWithArray(): void {
67+
$origConfig = array(
68+
'host' => 'www.example.com',
69+
'port' => 587,
70+
'debugLevel' => SMTP::DEBUG_SERVER,
71+
'auth' => TRUE,
72+
'credentials' => array(
73+
'user' => 'username',
74+
'pass' => 'password',
75+
),
76+
'secureOption' => PHPMailer::ENCRYPTION_STARTTLS,
77+
'charset' => 'utf8',
78+
);
79+
80+
$config = SmtpConfig::from($origConfig);
81+
$this->assertEquals('www.example.com', $config->getHost());
82+
$this->assertEquals(587, $config->getPort());
83+
$this->assertEquals(SMTP::DEBUG_SERVER, $config->getDebuglevel());
84+
$this->assertTrue($config->isAuth());
85+
$this->assertEquals('username', $config->getUsername());
86+
$this->assertEquals('password', $config->getPassword());
87+
$this->assertEquals(PHPMailer::ENCRYPTION_STARTTLS, $config->getSecureOption());
88+
$this->assertEquals('utf8', $config->getCharset());
89+
}
90+
91+
public function testFromWithString(): void {
92+
$origConfig = self::getJsonTestString();
93+
94+
$config = SmtpConfig::from($origConfig);
95+
$this->assertEquals('www.example.com', $config->getHost());
96+
$this->assertEquals(587, $config->getPort());
97+
$this->assertEquals(SMTP::DEBUG_SERVER, $config->getDebuglevel());
98+
$this->assertTrue($config->isAuth());
99+
$this->assertEquals('username', $config->getUsername());
100+
$this->assertEquals('password', $config->getPassword());
101+
$this->assertEquals(PHPMailer::ENCRYPTION_STARTTLS, $config->getSecureOption());
102+
$this->assertEquals('utf8', $config->getCharset());
103+
}
104+
105+
public function testFromWithObject(): void {
106+
$origConfig = json_decode(self::getJsonTestString());
107+
108+
$config = SmtpConfig::from($origConfig);
109+
$this->assertEquals('www.example.com', $config->getHost());
110+
$this->assertEquals(587, $config->getPort());
111+
$this->assertEquals(SMTP::DEBUG_SERVER, $config->getDebuglevel());
112+
$this->assertTrue($config->isAuth());
113+
$this->assertEquals('username', $config->getUsername());
114+
$this->assertEquals('password', $config->getPassword());
115+
$this->assertEquals(PHPMailer::ENCRYPTION_STARTTLS, $config->getSecureOption());
116+
$this->assertEquals('utf8', $config->getCharset());
117+
}
118+
119+
public static function getJsonTestString() {
120+
return '{"host":"www.example.com","port":587,"debugLevel":2,"auth":true,"credentials":{"user":"username","pass":"password"},"secureOption":"tls","charset":"utf8"}';
121+
}
122+
}
123+

0 commit comments

Comments
 (0)