Move Mailer error tests to unit [MAILPOET-2009]
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Methods\ErrorMappers\AmazonSESMapper;
|
||||
use SimpleXMLElement;
|
||||
|
||||
class AmazonSESMapperTest extends \MailPoetUnitTest {
|
||||
|
||||
/** @var AmazonSESMapper*/
|
||||
private $mapper;
|
||||
|
||||
/** @var array */
|
||||
private $response_data = [];
|
||||
|
||||
function _before() {
|
||||
parent::_before();
|
||||
$this->mapper = new AmazonSESMapper();
|
||||
$this->response_data = [
|
||||
'Error' => [
|
||||
'Type' => 'Sender',
|
||||
'Code' => 'ConfigurationSetDoesNotExist',
|
||||
'Message' => 'Some message',
|
||||
],
|
||||
'RequestId' => '01ca93ec-b5a3-11e8-bff8-49dd5ddf8019',
|
||||
];
|
||||
}
|
||||
|
||||
function testGetProperError() {
|
||||
$response = $this->buildXmlResponseFromArray($this->response_data, new SimpleXMLElement('<response/>'));
|
||||
$error = $this->mapper->getErrorFromResponse($response, 'john@rambo.com');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('Some message');
|
||||
expect($error->getSubscriberErrors()[0]->getEmail())->equals('john@rambo.com');
|
||||
}
|
||||
|
||||
function testGetSoftErrorForRejectedMessage() {
|
||||
$this->response_data['Error']['Code'] = 'MessageRejected';
|
||||
$response = $this->buildXmlResponseFromArray($this->response_data, new SimpleXMLElement('<response/>'));
|
||||
$error = $this->mapper->getErrorFromResponse($response, 'john@rambo.com');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SimpleXMLElement
|
||||
*/
|
||||
private function buildXmlResponseFromArray($response_data, SimpleXMLElement $xml) {
|
||||
foreach ($response_data as $tag => $value) {
|
||||
if (is_array($value)) {
|
||||
$this->buildXmlResponseFromArray($value, $xml->addChild($tag));
|
||||
} else {
|
||||
$xml->addChild($tag, $value);
|
||||
}
|
||||
}
|
||||
return $xml;
|
||||
}
|
||||
}
|
136
tests/unit/Mailer/Methods/ErrorMappers/MailPoetMapperTest.php
Normal file
136
tests/unit/Mailer/Methods/ErrorMappers/MailPoetMapperTest.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
__halt_compiler();
|
||||
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
|
||||
use MailPoet\Services\Bridge\API;
|
||||
use MailPoet\Util\Helpers;
|
||||
|
||||
class MailPoetMapperTest extends \MailPoetUnitTest {
|
||||
/** @var MailPoetMapper */
|
||||
private $mapper;
|
||||
|
||||
/** @var array */
|
||||
private $subscribers;
|
||||
|
||||
function _before() {
|
||||
parent::_before();
|
||||
$this->mapper = new MailPoetMapper();
|
||||
$this->subscribers = ['a@example.com', 'c d <b@example.com>'];
|
||||
}
|
||||
|
||||
function testCreateConnectionError() {
|
||||
$error = $this->mapper->getConnectionError('connection error');
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_CONNECT);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('connection error');
|
||||
}
|
||||
|
||||
function testGetErrorNotArray() {
|
||||
$api_result = [
|
||||
'code' => API::RESPONSE_CODE_NOT_ARRAY,
|
||||
'status' => API::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => 'error not array',
|
||||
];
|
||||
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
|
||||
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_SEND);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('JSON input is not an array');
|
||||
}
|
||||
|
||||
function testGetErrorBannedAccount() {
|
||||
$api_result = [
|
||||
'code' => API::RESPONSE_CODE_CAN_NOT_SEND,
|
||||
'status' => API::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => 'this is a spam',
|
||||
];
|
||||
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
|
||||
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_SEND);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals(Helpers::replaceLinkTags(
|
||||
__('You currently are not permitted to send any emails with MailPoet Sending Service, which may have happened due to poor deliverability. Please [link]contact our support team[/link] to resolve the issue.', 'mailpoet'),
|
||||
'https://www.mailpoet.com/support/',
|
||||
array(
|
||||
'target' => '_blank',
|
||||
'rel' => 'noopener noreferrer',
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
function testGetErrorUnauthorizedEmail() {
|
||||
$api_result = [
|
||||
'code' => API::RESPONSE_CODE_CAN_NOT_SEND,
|
||||
'status' => API::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => MailerError::MESSAGE_EMAIL_NOT_AUTHORIZED,
|
||||
];
|
||||
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
|
||||
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_AUTHORIZATION);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->contains('The MailPoet Sending Service did not send your latest email because the address');
|
||||
}
|
||||
|
||||
function testGetErrorPayloadTooBig() {
|
||||
$api_result = [
|
||||
'code' => API::RESPONSE_CODE_PAYLOAD_TOO_BIG,
|
||||
'status' => API::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => 'error too big',
|
||||
];
|
||||
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_SEND);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('error too big');
|
||||
}
|
||||
|
||||
function testGetPayloadError() {
|
||||
$api_result = [
|
||||
'code' => API::RESPONSE_CODE_PAYLOAD_ERROR,
|
||||
'status' => API::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => 'Api Error',
|
||||
];
|
||||
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_SEND);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('Error while sending. Api Error');
|
||||
}
|
||||
|
||||
function testGetPayloadErrorWithErrorMessage() {
|
||||
$api_result = [
|
||||
'code' => API::RESPONSE_CODE_PAYLOAD_ERROR,
|
||||
'status' => API::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => '[{"index":0,"errors":{"subject":"subject is missing"}},{"index":1,"errors":{"subject":"subject is missing"}}]'
|
||||
];
|
||||
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_SEND);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
|
||||
$subscriber_errors = $error->getSubscriberErrors();
|
||||
expect(count($subscriber_errors))->equals(2);
|
||||
expect($subscriber_errors[0]->getEmail())->equals('a@example.com');
|
||||
expect($subscriber_errors[0]->getMessage())->equals('subject is missing');
|
||||
expect($subscriber_errors[1]->getEmail())->equals('c d <b@example.com>');
|
||||
expect($subscriber_errors[1]->getMessage())->equals('subject is missing');
|
||||
}
|
||||
|
||||
function testGetPayloadErrorForMalformedMSSResponse() {
|
||||
$api_result = [
|
||||
'code' => API::RESPONSE_CODE_PAYLOAD_ERROR,
|
||||
'status' => API::SENDING_STATUS_SEND_ERROR,
|
||||
'message' => '[{"errors":{"subject":"subject is missing"}},{"errors":{"subject":"subject is missing"}}]'
|
||||
];
|
||||
$error = $this->mapper->getErrorForResult($api_result, $this->subscribers);
|
||||
expect($error)->isInstanceOf(MailerError::class);
|
||||
expect($error->getOperation())->equals(MailerError::OPERATION_SEND);
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('Error while sending. Invalid MSS response format.');
|
||||
}
|
||||
}
|
46
tests/unit/Mailer/Methods/ErrorMappers/PHPMailMapperTest.php
Normal file
46
tests/unit/Mailer/Methods/ErrorMappers/PHPMailMapperTest.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use Codeception\Stub;
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class PHPMailMapperTest extends \MailPoetUnitTest {
|
||||
|
||||
/** @var PHPMailMapper*/
|
||||
private $mapper;
|
||||
|
||||
function _before() {
|
||||
parent::_before();
|
||||
$this->mapper = new PHPMailMapper();
|
||||
WPFunctions::set(Stub::make(new WPFunctions, [
|
||||
'__' => function ($value) {
|
||||
return $value;
|
||||
}
|
||||
]));
|
||||
}
|
||||
|
||||
function testGetProperErrorForSubscriber() {
|
||||
$error = $this->mapper->getErrorForSubscriber('john@rambo.com');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('PHPMail has returned an unknown error.');
|
||||
expect($error->getSubscriberErrors()[0]->getEmail())->equals('john@rambo.com');
|
||||
}
|
||||
|
||||
function testGetProperErrorFromException() {
|
||||
$error = $this->mapper->getErrorFromException(new \Exception('Some message'), 'john@rambo.com');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('Some message');
|
||||
expect($error->getSubscriberErrors()[0]->getEmail())->equals('john@rambo.com');
|
||||
}
|
||||
|
||||
function testGetSoftErrorFromExceptionForInvalidEmail() {
|
||||
$error = $this->mapper->getErrorFromException(new \Exception('Invalid address. (Add ...'), 'john@rambo.com');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
|
||||
}
|
||||
|
||||
function _after() {
|
||||
WPFunctions::set(new WPFunctions);
|
||||
}
|
||||
}
|
69
tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php
Normal file
69
tests/unit/Mailer/Methods/ErrorMappers/SMTPMapperTest.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use Codeception\Stub;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class SMTPMapperTest extends \MailPoetUnitTest {
|
||||
|
||||
/** @var SMTPMapper */
|
||||
private $mapper;
|
||||
|
||||
function _before() {
|
||||
parent::_before();
|
||||
$this->mapper = new SMTPMapper();
|
||||
WPFunctions::set(Stub::make(new WPFunctions, [
|
||||
'__' => function ($value) {
|
||||
return $value;
|
||||
}
|
||||
]));
|
||||
}
|
||||
|
||||
function testItCanProcessExceptionMessage() {
|
||||
$message = 'Connection could not be established with host localhost [Connection refused #111]' . PHP_EOL
|
||||
. 'Log data:' . PHP_EOL
|
||||
. '++ Starting Swift_SmtpTransport' . PHP_EOL
|
||||
. '!! Connection could not be established with host localhost [Connection refused #111] (code: 0)';
|
||||
$error = $this->mapper->getErrorFromException(new \Exception($message), 'john@rambo.com');
|
||||
expect($error->getMessage())
|
||||
->equals('Connection could not be established with host localhost [Connection refused #111]');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getSubscriberErrors()[0]->getEmail())->equals('john@rambo.com');
|
||||
}
|
||||
|
||||
function testItCreatesSoftErrorForInvalidEmail() {
|
||||
$message = 'Invalid email';
|
||||
$error = $this->mapper->getErrorFromException(new \Swift_RfcComplianceException($message), 'john@rambo.com');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
|
||||
}
|
||||
|
||||
function testItCanProcessLogMessageWhenOneExists() {
|
||||
$log = '++ Swift_SmtpTransport started' . PHP_EOL
|
||||
. '>> MAIL FROM:<moi@mrcasual.com>' . PHP_EOL
|
||||
. '<< 250 OK' . PHP_EOL
|
||||
. '>> RCPT TO:<test2@ietsdoenofferte.nl>' . PHP_EOL
|
||||
. '<< 550 No such recipient here' . PHP_EOL
|
||||
. '!! Expected response code 250/251/252 but got code "550", with message "550 No such recipient here' . PHP_EOL
|
||||
. '" (code: 550)' . PHP_EOL
|
||||
. '>> RSET' . PHP_EOL
|
||||
. '<< 250 Reset OK' . PHP_EOL;
|
||||
$error = $this->mapper->getErrorFromLog($log, 'test@example.com');
|
||||
expect($error->getMessage())
|
||||
->equals('Expected response code 250/251/252 but got code "550", with message "550 No such recipient here" (code: 550)');
|
||||
expect($error->getSubscriberErrors()[0]->getEmail('moi@mrcasual.com'));
|
||||
}
|
||||
|
||||
function testItReturnsGenericMessageWhenLogMessageDoesNotExist() {
|
||||
$error = $this->mapper->getErrorFromLog(null, 'test@example.com');
|
||||
expect($error->getMessage())
|
||||
->equals(Mailer::METHOD_SMTP . ' has returned an unknown error.');
|
||||
expect($error->getSubscriberErrors()[0]->getEmail('moi@mrcasual.com'));
|
||||
}
|
||||
|
||||
function _after() {
|
||||
WPFunctions::set(new WPFunctions);
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
|
||||
|
||||
use MailPoet\Mailer\MailerError;
|
||||
use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
|
||||
|
||||
class SendGridMapperTest extends \MailPoetUnitTest {
|
||||
|
||||
/** @var SendGridMapper*/
|
||||
private $mapper;
|
||||
|
||||
/** @var array */
|
||||
private $response = [];
|
||||
|
||||
function _before() {
|
||||
parent::_before();
|
||||
$this->mapper = new SendGridMapper();
|
||||
$this->response = [
|
||||
'errors' => [
|
||||
'Some message',
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
function testGetProperError() {
|
||||
$error = $this->mapper->getErrorFromResponse($this->response, 'john@rambo.com');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_HARD);
|
||||
expect($error->getMessage())->equals('Some message');
|
||||
expect($error->getSubscriberErrors()[0]->getEmail())->equals('john@rambo.com');
|
||||
}
|
||||
|
||||
function testGetSoftErrorForInvalidEmail() {
|
||||
$this->response['errors'][0] = 'Invalid email address ,,@';
|
||||
$error = $this->mapper->getErrorFromResponse($this->response, ',,@');
|
||||
expect($error->getLevel())->equals(MailerError::LEVEL_SOFT);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user