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>
This commit is contained in:
parent
e6e6b5648a
commit
be7db1573d
|
@ -57,7 +57,7 @@ use OCP\Mail\IMessage;
|
||||||
* @package OC\Mail
|
* @package OC\Mail
|
||||||
*/
|
*/
|
||||||
class Mailer implements IMailer {
|
class Mailer implements IMailer {
|
||||||
/** @var \Swift_SmtpTransport|\Swift_SendmailTransport Cached transport */
|
/** @var \Swift_Mailer Cached mailer */
|
||||||
private $instance = null;
|
private $instance = null;
|
||||||
/** @var IConfig */
|
/** @var IConfig */
|
||||||
private $config;
|
private $config;
|
||||||
|
@ -220,27 +220,24 @@ class Mailer implements IMailer {
|
||||||
return $name.'@'.$domain;
|
return $name.'@'.$domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected function getInstance(): \Swift_Mailer {
|
||||||
* Returns whatever transport is configured within the config
|
|
||||||
*
|
|
||||||
* @return \Swift_SmtpTransport|\Swift_SendmailTransport
|
|
||||||
*/
|
|
||||||
protected function getInstance() {
|
|
||||||
if (!is_null($this->instance)) {
|
if (!is_null($this->instance)) {
|
||||||
return $this->instance;
|
return $this->instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$transport = null;
|
||||||
|
|
||||||
switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) {
|
switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) {
|
||||||
case 'sendmail':
|
case 'sendmail':
|
||||||
$this->instance = $this->getSendMailInstance();
|
$transport = $this->getSendMailInstance();
|
||||||
break;
|
break;
|
||||||
case 'smtp':
|
case 'smtp':
|
||||||
default:
|
default:
|
||||||
$this->instance = $this->getSmtpInstance();
|
$transport = $this->getSmtpInstance();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->instance;
|
return new \Swift_Mailer($transport);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -262,7 +259,7 @@ class Mailer implements IMailer {
|
||||||
if (!empty($smtpSecurity)) {
|
if (!empty($smtpSecurity)) {
|
||||||
$transport->setEncryption($smtpSecurity);
|
$transport->setEncryption($smtpSecurity);
|
||||||
}
|
}
|
||||||
$transport->start();
|
|
||||||
return $transport;
|
return $transport;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -272,7 +269,7 @@ class Mailer implements IMailer {
|
||||||
* @return \Swift_SendmailTransport
|
* @return \Swift_SendmailTransport
|
||||||
*/
|
*/
|
||||||
protected function getSendMailInstance(): \Swift_SendmailTransport {
|
protected function getSendMailInstance(): \Swift_SendmailTransport {
|
||||||
switch ($this->config->getSystemValue('mail_smtpmode', 'smpt')) {
|
switch ($this->config->getSystemValue('mail_smtpmode', 'smtp')) {
|
||||||
case 'qmail':
|
case 'qmail':
|
||||||
$binaryPath = '/var/qmail/bin/sendmail';
|
$binaryPath = '/var/qmail/bin/sendmail';
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -48,50 +48,41 @@ class MailerTest extends TestCase {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetMailInstance() {
|
|
||||||
$this->assertEquals(\Swift_MailTransport::newInstance(), self::invokePrivate($this->mailer, 'getMailinstance'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testGetSendMailInstanceSendMail() {
|
public function testGetSendMailInstanceSendMail() {
|
||||||
$this->config
|
$this->config
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('getSystemValue')
|
->method('getSystemValue')
|
||||||
->with('mail_smtpmode', 'php')
|
->with('mail_smtpmode', 'smtp')
|
||||||
->will($this->returnValue('sendmail'));
|
->will($this->returnValue('sendmail'));
|
||||||
|
|
||||||
$this->assertEquals(\Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
$this->assertEquals(new \Swift_SendmailTransport('/usr/sbin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetSendMailInstanceSendMailQmail() {
|
public function testGetSendMailInstanceSendMailQmail() {
|
||||||
$this->config
|
$this->config
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('getSystemValue')
|
->method('getSystemValue')
|
||||||
->with('mail_smtpmode', 'php')
|
->with('mail_smtpmode', 'smtp')
|
||||||
->will($this->returnValue('qmail'));
|
->will($this->returnValue('qmail'));
|
||||||
|
|
||||||
$this->assertEquals(\Swift_SendmailTransport::newInstance('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
$this->assertEquals(new \Swift_SendmailTransport('/var/qmail/bin/sendmail -bs'), self::invokePrivate($this->mailer, 'getSendMailInstance'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetInstanceDefault() {
|
public function testGetInstanceDefault() {
|
||||||
$this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
||||||
}
|
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
|
||||||
|
$this->assertInstanceOf(\Swift_SmtpTransport::class, $mailer->getTransport());
|
||||||
public function testGetInstancePhp() {
|
|
||||||
$this->config
|
|
||||||
->expects($this->any())
|
|
||||||
->method('getSystemValue')
|
|
||||||
->will($this->returnValue('php'));
|
|
||||||
|
|
||||||
$this->assertInstanceOf('\Swift_MailTransport', self::invokePrivate($this->mailer, 'getInstance'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetInstanceSendmail() {
|
public function testGetInstanceSendmail() {
|
||||||
$this->config
|
$this->config
|
||||||
->expects($this->any())
|
|
||||||
->method('getSystemValue')
|
->method('getSystemValue')
|
||||||
->will($this->returnValue('sendmail'));
|
->with('mail_smtpmode', 'smtp')
|
||||||
|
->willReturn('sendmail');
|
||||||
|
|
||||||
$this->assertInstanceOf('\Swift_Mailer', self::invokePrivate($this->mailer, 'getInstance'));
|
$mailer = self::invokePrivate($this->mailer, 'getInstance');
|
||||||
|
$this->assertInstanceOf(\Swift_Mailer::class, $mailer);
|
||||||
|
$this->assertInstanceOf(\Swift_SendmailTransport::class, $mailer->getTransport());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreateMessage() {
|
public function testCreateMessage() {
|
||||||
|
|
Loading…
Reference in New Issue