Skip to content

Commit e9a6f7e

Browse files
committed
Swift to \Swift_Mailer as abstraction
* \Swift_Mailer handles starting the transport etc properly * Fixed tests Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 7a01ddb commit e9a6f7e

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

lib/private/Mail/Mailer.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* @package OC\Mail
5858
*/
5959
class Mailer implements IMailer {
60-
/** @var \Swift_SmtpTransport|\Swift_SendmailTransport Cached transport */
60+
/** @var \Swift_Mailer Cached mailer */
6161
private $instance = null;
6262
/** @var IConfig */
6363
private $config;
@@ -220,27 +220,24 @@ protected function convertEmail(string $email): string {
220220
return $name.'@'.$domain;
221221
}
222222

223-
/**
224-
* Returns whatever transport is configured within the config
225-
*
226-
* @return \Swift_SmtpTransport|\Swift_SendmailTransport
227-
*/
228-
protected function getInstance() {
223+
protected function getInstance(): \Swift_Mailer {
229224
if (!is_null($this->instance)) {
230225
return $this->instance;
231226
}
232227

228+
$transport = null;
229+
233230
switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) {
234231
case 'sendmail':
235-
$this->instance = $this->getSendMailInstance();
232+
$transport = $this->getSendMailInstance();
236233
break;
237234
case 'smtp':
238235
default:
239-
$this->instance = $this->getSmtpInstance();
236+
$transport = $this->getSmtpInstance();
240237
break;
241238
}
242239

243-
return $this->instance;
240+
return new \Swift_Mailer($transport);
244241
}
245242

246243
/**
@@ -262,7 +259,7 @@ protected function getSmtpInstance(): \Swift_SmtpTransport {
262259
if (!empty($smtpSecurity)) {
263260
$transport->setEncryption($smtpSecurity);
264261
}
265-
$transport->start();
262+
266263
return $transport;
267264
}
268265

@@ -272,7 +269,7 @@ protected function getSmtpInstance(): \Swift_SmtpTransport {
272269
* @return \Swift_SendmailTransport
273270
*/
274271
protected function getSendMailInstance(): \Swift_SendmailTransport {
275-
switch ($this->config->getSystemValue('mail_smtpmode', 'smpt')) {
272+
switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) {
276273
case 'qmail':
277274
$binaryPath = '/var/qmail/bin/sendmail';
278275
break;

tests/lib/Mail/MailerTest.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,50 +48,41 @@ public function setUp() {
4848
);
4949
}
5050

51-
public function testGetMailInstance() {
52-
$this->assertEquals(\Swift_MailTransport::newInstance(), self::invokePrivate($this->mailer, 'getMailinstance'));
53-
}
54-
5551
public function testGetSendMailInstanceSendMail() {
5652
$this->config
5753
->expects($this->once())
5854
->method('getSystemValue')
59-
->with('mail_smtpmode', 'php')
55+
->with('mail_smtpmode', 'smtp')
6056
->will($this->returnValue('sendmail'));
6157

62-
$this->assertEquals(\Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
58+
$this->assertEquals(new \Swift_SendmailTransport('/usr/sbin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
6359
}
6460

6561
public function testGetSendMailInstanceSendMailQmail() {
6662
$this->config
6763
->expects($this->once())
6864
->method('getSystemValue')
69-
->with('mail_smtpmode', 'php')
65+
->with('mail_smtpmode', 'smtp')
7066
->will($this->returnValue('qmail'));
7167

72-
$this->assertEquals(\Swift_SendmailTransport::newInstance('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
68+
$this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
7369
}
7470

7571
public function testGetInstanceDefault() {
76-
$this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
77-
}
78-
79-
public function testGetInstancePhp() {
80-
$this->config
81-
->expects($this->any())
82-
->method('getSystemValue')
83-
->will($this->returnValue('php'));
84-
85-
$this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
72+
$mailer = self::invokePrivate($this->mailer, 'getInstance');
73+
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
74+
$this->assertInstanceOf(\Swift_SmtpTransport::class, $mailer->getTransport());
8675
}
8776

8877
public function testGetInstanceSendmail() {
8978
$this->config
90-
->expects($this->any())
9179
->method('getSystemValue')
92-
->will($this->returnValue('sendmail'));
80+
->with('mail_smtpmode', 'smtp')
81+
->willReturn('sendmail');
9382

94-
$this->assertInstanceOf('\Swift_Mailer', self::invokePrivate($this->mailer, 'getInstance'));
83+
$mailer = self::invokePrivate($this->mailer, 'getInstance');
84+
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
85+
$this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport());
9586
}
9687

9788
public function testCreateMessage() {

0 commit comments

Comments
 (0)