Files
piratepoet/tests/unit/Mailer/Methods/PHPMailTest.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

123 lines
3.6 KiB
PHP

<?php
namespace MailPoet\Test\Mailer\Methods;
use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper;
use MailPoet\Mailer\Methods\PHPMail;
class PHPMailTest extends \MailPoetTest {
function _before() {
$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 PHPMail(
$this->sender,
$this->reply_to,
$this->return_path,
new PHPMailMapper()
);
$this->subscriber = 'Recipient <mailpoet-phoenix-test@mailinator.com>';
$this->newsletter = array(
'subject' => 'testing local method (PHP mail)',
'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)->isInstanceOf('PHPMailer');
expect($mailer->Mailer)->equals('mail'); // uses PHP's mail() function
}
function testWhenReturnPathIsNullItIsSetToSenderEmail() {
$mailer = new PHPMail(
$this->sender,
$this->reply_to,
$return_path = false,
new PHPMailMapper()
);
expect($mailer->return_path)->equals($this->sender['from_email']);
}
function testItCanConfigureMailerWithMessage() {
$mailer = $this->mailer
->configureMailerWithMessage($this->newsletter, $this->subscriber, $this->extra_params);
expect($mailer->CharSet)->equals('UTF-8');
expect($mailer->getToAddresses())->equals(
array(
array(
'mailpoet-phoenix-test@mailinator.com',
'Recipient'
)
)
);
expect($mailer->getAllRecipientAddresses())
->equals(array('mailpoet-phoenix-test@mailinator.com' => true));
expect($mailer->From)->equals($this->sender['from_email']);
expect($mailer->FromName)->equals($this->sender['from_name']);
expect($mailer->getReplyToAddresses())->equals(
array(
'reply-to@mailpoet.com' => array(
'reply-to@mailpoet.com',
'Reply To'
)
)
);
expect($mailer->Sender)->equals($this->return_path);
expect($mailer->ContentType)->equals('text/html');
expect($mailer->Subject)->equals($this->newsletter['subject']);
expect($mailer->Body)
->equals($this->newsletter['body']['html']);
expect($mailer->AltBody)
->equals($this->newsletter['body']['text']);
expect($mailer->getCustomHeaders())->equals(
array(
array(
'List-Unsubscribe',
'http://www.mailpoet.com'
)
)
);
}
function testItCanProcessSubscriber() {
expect($this->mailer->processSubscriber('test@test.com'))->equals(
array(
'email' => 'test@test.com',
'name' => ''
));
expect($this->mailer->processSubscriber('First <test@test.com>'))->equals(
array(
'email' => 'test@test.com',
'name' => 'First'
));
expect($this->mailer->processSubscriber('First Last <test@test.com>'))->equals(
array(
'email' => 'test@test.com',
'name' => 'First Last'
));
}
function testItCanSend() {
if(getenv('WP_TEST_MAILER_ENABLE_SENDING') !== 'true') return;
$result = $this->mailer->send(
$this->newsletter,
$this->subscriber
);
expect($result['response'])->true();
}
}