Files
piratepoet/tests/unit/Mailer/Methods/SMTPTest.php
Rostislav Wolny 0923c892c1 Refactor sending methods to use error mappers
We want to add some logic to error handling.
This commit extracts error handling code from sending methods classes,
which already do a lot of other stuff, to error mappers which are responsible
for creating proper error object (MailerError). This error object is a replacement
for assoc. array which already had some special keys for certain usecases and
can not be properly type hinted.

[MAILPOET-1154]
2018-09-13 09:33:26 +02:00

188 lines
6.0 KiB
PHP

<?php
namespace MailPoet\Test\Mailer\Methods;
use Helper\WordPressHooks as WPHooksHelper;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\Mailer\Methods\SMTP;
use MailPoet\WP\Hooks;
class SMTPTest extends \MailPoetTest {
function _before() {
$this->settings = array(
'method' => 'SMTP',
'host' => getenv('WP_TEST_MAILER_SMTP_HOST') ?
getenv('WP_TEST_MAILER_SMTP_HOST') :
'example.com',
'port' => 587,
'login' => getenv('WP_TEST_MAILER_SMTP_LOGIN') ?
getenv('WP_TEST_MAILER_SMTP_LOGIN') :
'example.com',
'password' => getenv('WP_TEST_MAILER_SMTP_PASSWORD') ?
getenv('WP_TEST_MAILER_SMTP_PASSWORD') :
'example.com',
'authentication' => '1',
'encryption' => 'tls'
);
$this->sender = array(
'from_name' => 'Sender',
'from_email' => 'staff@mailpoet.com',
'from_name_email' => 'Sender <staff@mailpoet.com>'
);
$this->reply_to = array(
'reply_to_name' => 'Reply To',
'reply_to_email' => 'reply-to@mailpoet.com',
'reply_to_name_email' => 'Reply To <reply-to@mailpoet.com>'
);
$this->return_path = 'bounce@mailpoet.com';
$this->mailer = new SMTP(
$this->settings['host'],
$this->settings['port'],
$this->settings['authentication'],
$this->settings['login'],
$this->settings['password'],
$this->settings['encryption'],
$this->sender,
$this->reply_to,
$this->return_path,
new SMTPMapper()
);
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array(
'subject' => 'testing SMTP',
'body' => array(
'html' => 'HTML body',
'text' => 'TEXT body'
)
);
$this->extra_params = array(
'unsubscribe_url' => 'http://www.mailpoet.com'
);
}
function testItCanBuildMailer() {
$mailer = $this->mailer->buildMailer();
expect($mailer->getTransport()->getHost())
->equals($this->settings['host']);
expect($mailer->getTransport()->getPort())
->equals($this->settings['port']);
expect($mailer->getTransport()->getUsername())
->equals($this->settings['login']);
expect($mailer->getTransport()->getPassword())
->equals($this->settings['password']);
expect($mailer->getTransport()->getEncryption())
->equals($this->settings['encryption']);
}
function testWhenReturnPathIsNullItIsSetToSenderEmail() {
$mailer = new SMTP(
$this->settings['host'],
$this->settings['port'],
$this->settings['authentication'],
$this->settings['login'],
$this->settings['password'],
$this->settings['encryption'],
$this->sender,
$this->reply_to,
$return_path = false,
new SMTPMapper()
);
expect($mailer->return_path)->equals($this->sender['from_email']);
}
function testItCanCreateMessage() {
$message = $this->mailer
->createMessage($this->newsletter, $this->subscriber, $this->extra_params);
expect($message->getTo())
->equals(array('mailpoet-phoenix-test@mailinator.com' => 'Recipient'));
expect($message->getFrom())
->equals(array($this->sender['from_email'] => $this->sender['from_name']));
expect($message->getSender())
->equals(array($this->sender['from_email'] => null));
expect($message->getReplyTo())
->equals(array($this->reply_to['reply_to_email'] => $this->reply_to['reply_to_name']));
expect($message->getSubject())
->equals($this->newsletter['subject']);
expect($message->getBody())
->equals($this->newsletter['body']['html']);
expect($message->getChildren()[0]->getContentType())
->equals('text/plain');
expect($message->getHeaders()->get('List-Unsubscribe')->getValue())
->equals('<' . $this->extra_params['unsubscribe_url'] . '>');
}
function testItCanProcessSubscriber() {
expect($this->mailer->processSubscriber('test@test.com'))
->equals(array('test@test.com' => ''));
expect($this->mailer->processSubscriber('First <test@test.com>'))
->equals(array('test@test.com' => 'First'));
expect($this->mailer->processSubscriber('First Last <test@test.com>'))
->equals(array('test@test.com' => 'First Last'));
}
function testItCantSendWithoutProperAuthentication() {
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
$this->mailer->login = 'someone';
$this->mailer->mailer = $this->mailer->buildMailer();
$result = $this->mailer->send(
$this->newsletter,
$this->subscriber
);
expect($result['response'])->false();
}
function testItAppliesTransportFilter() {
$mailer = $this->mailer->buildMailer();
expect($mailer->getTransport()->getStreamOptions())->isEmpty();
Hooks::addFilter(
'mailpoet_mailer_smtp_transport_agent',
function($transport) {
$transport->setStreamOptions(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false
)
)
);
return $transport;
}
);
$mailer = $this->mailer->buildMailer();
expect($mailer->getTransport()->getStreamOptions())->equals(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false
)
)
);
}
function testItAppliesTimeoutFilter() {
$mailer = $this->mailer->buildMailer();
expect($mailer->getTransport()->getTimeout())->equals(\MailPoet\Mailer\Methods\SMTP::SMTP_CONNECTION_TIMEOUT);
Hooks::addFilter(
'mailpoet_mailer_smtp_connection_timeout',
function() {
return 20;
}
);
$mailer = $this->mailer->buildMailer();
expect($mailer->getTransport()->getTimeout())->equals(20);
}
function testItCanSend() {
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
$result = $this->mailer->send(
$this->newsletter,
$this->subscriber
);
expect($result['response'])->true();
}
function _after() {
WPHooksHelper::releaseAllHooks();
}
}