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]
This commit is contained in:
Rostislav Wolny
2018-08-30 10:42:58 +02:00
parent 8cf5d17cfd
commit 0923c892c1
22 changed files with 517 additions and 211 deletions

View File

@ -0,0 +1,46 @@
<?php
namespace MailPoet\Test\Mailer\Methods\ErrorMappers;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
class SMTPMapperTest extends \MailPoetTest {
/** @var SMTPMapper */
private $mapper;
function _before() {
$this->mapper = new SMTPMapper();
}
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));
expect($error->getMessage())
->equals('Connection could not be established with host localhost [Connection refused #111]');
}
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) Unprocessed subscriber: test@example.com');
}
function testItReturnsGenericMessageWhenLogMessageDoesNotExist() {
$error = $this->mapper->getErrorFromLog(null, 'test@example.com');
expect($error->getMessage())
->equals(Mailer::METHOD_SMTP . ' has returned an unknown error. Unprocessed subscriber: test@example.com');
}
}