Skip to content

Commit

Permalink
Merge pull request #6 from byjg/2.0.1
Browse files Browse the repository at this point in the history
Fix PHPUnit
  • Loading branch information
byjg committed Nov 29, 2017
2 parents 7ea1059 + b90026e commit f7c21b2
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/Exception/InvalidMailHandlerException.php
@@ -0,0 +1,8 @@
<?php

namespace ByJG\Mail\Exception;

class InvalidMailHandlerException extends \Exception
{

}
1 change: 0 additions & 1 deletion src/Exception/InvalidMessageFormatException.php
Expand Up @@ -2,7 +2,6 @@

namespace ByJG\Mail\Exception;


class InvalidMessageFormatException extends \Exception
{

Expand Down
1 change: 0 additions & 1 deletion src/Exception/MailApiException.php
Expand Up @@ -2,7 +2,6 @@

namespace ByJG\Mail\Exception;


class MailApiException extends \Exception
{

Expand Down
8 changes: 8 additions & 0 deletions src/Exception/ProtocolNotRegisteredException.php
@@ -0,0 +1,8 @@
<?php

namespace ByJG\Mail\Exception;

class ProtocolNotRegisteredException extends \Exception
{

}
16 changes: 14 additions & 2 deletions src/MailerFactory.php
Expand Up @@ -7,26 +7,38 @@

namespace ByJG\Mail;

use ByJG\Mail\Exception\InvalidMailHandlerException;
use ByJG\Mail\Exception\ProtocolNotRegisteredException;
use ByJG\Util\Uri;

class MailerFactory
{
private static $config = [];

/**
* @param string $protocol
* @param string $class
* @throws \ByJG\Mail\Exception\InvalidMailHandlerException
*/
public static function registerMailer($protocol, $class)
{
if (!class_exists($class, true)) {
throw new \Exception('Class not found!');
throw new InvalidMailHandlerException('Class not found!');
}
self::$config[$protocol] = $class;
}

/**
* @param $connection
* @return mixed
* @throws \ByJG\Mail\Exception\ProtocolNotRegisteredException
*/
public static function create($connection)
{
$uri = new Uri($connection);

if (!isset(self::$config[$uri->getScheme()])) {
throw new \Exception('Protocol not found/registered!');
throw new ProtocolNotRegisteredException('Protocol not found/registered!');
}

$class = self::$config[$uri->getScheme()];
Expand Down
16 changes: 16 additions & 0 deletions src/Override/PHPMailerOverride.php
Expand Up @@ -6,13 +6,29 @@

class PHPMailerOverride extends \PHPMailer
{
public function __construct($exceptions = null)
{
parent::__construct($exceptions);
$this->XMailer = 'PHPMailer (https://github.com/PHPMailer/PHPMailer)';
}

/**
* @return string
* @throws \ByJG\Mail\Exception\InvalidMessageFormatException
* @throws \phpmailerException
*/
public function getFullMessageEnvelope()
{
$parts = $this->getMessageEnvelopeParts();

return $parts['header'] . $parts['body'];
}

/**
* @return array
* @throws \ByJG\Mail\Exception\InvalidMessageFormatException
* @throws \phpmailerException
*/
public function getMessageEnvelopeParts()
{
if (!$this->preSend()) {
Expand Down
5 changes: 4 additions & 1 deletion src/Util.php
Expand Up @@ -41,7 +41,10 @@ public static function decomposeEmail($fullEmail)
if (array_key_exists("email", $parts)) {
$email = $parts["email"];
}
} else if (preg_match($pat2, $fullEmail, $parts)) {
return array("email" => $email, "name" => $name);
}

if (preg_match($pat2, $fullEmail, $parts)) {
if (array_key_exists("email", $parts)) {
$email = $parts["email"];
}
Expand Down
1 change: 1 addition & 0 deletions src/Wrapper/AmazonSesWrapper.php
Expand Up @@ -29,6 +29,7 @@ public function getSesClient()
*
* @param Envelope $envelope
* @return bool
* @throws \Exception
*/
public function send(Envelope $envelope)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Wrapper/BaseWrapper.php
Expand Up @@ -22,6 +22,10 @@ public function __construct(Uri $uri)
$this->uri = $uri;
}

/**
* @param \ByJG\Mail\Envelope $envelope
* @throws \Exception
*/
public function validate(Envelope $envelope)
{
if (0 === count($envelope->getTo())) {
Expand Down
2 changes: 2 additions & 0 deletions src/Wrapper/MailgunApiWrapper.php
Expand Up @@ -27,6 +27,8 @@ public function getRequestObject()
* @param Envelope $envelope
* @return bool
* @throws \ByJG\Mail\Exception\MailApiException
* @throws \ByJG\Util\CurlException
* @throws \Exception
*/
public function send(Envelope $envelope)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Wrapper/PHPMailerWrapper.php
Expand Up @@ -20,9 +20,9 @@ public function getMailer()
}

/**
*
* @param Envelope $envelope
* @return PHPMailerOverride
* @throws \phpmailerException
*/
protected function prepareMailer(Envelope $envelope)
{
Expand Down Expand Up @@ -95,6 +95,8 @@ protected function prepareMailer(Envelope $envelope)
* @param Envelope $envelope
* @return bool
* @throws \ByJG\Mail\Exception\MailApiException
* @throws \Exception
* @throws \phpmailerException
*/
public function send(Envelope $envelope)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Wrapper/SendMailWrapper.php
Expand Up @@ -14,6 +14,12 @@
class SendMailWrapper extends PHPMailerWrapper
{

/**
* @param \ByJG\Mail\Envelope $envelope
* @return bool
* @throws \ByJG\Mail\Exception\InvalidMessageFormatException
* @throws \Exception
*/
public function send(Envelope $envelope)
{
$this->validate($envelope);
Expand Down
2 changes: 1 addition & 1 deletion tests/BaseWrapperTest.php
Expand Up @@ -53,7 +53,7 @@ protected function fixVariableFields($text)
{
$text = preg_replace(
[
'~\w+, \d+ \w+ \w+ \d+:\d+:\d+ \+\d+~',
'~\w+, \d+ \w+ \w+ \d+:\d+:\d+ [+-]\d+~',
'~([_<])\w{32}([@"\n-])~',
'~<boundarydelimiter@[^>]+>~'
],
Expand Down
61 changes: 61 additions & 0 deletions tests/MailerWrapperTest.php
@@ -0,0 +1,61 @@
<?php

namespace ByJG\Mail;

use \PHPUnit\Framework\TestCase;
use ByJG\Mail\Wrapper\AmazonSesWrapper;
use ByJG\Mail\Wrapper\MailgunApiWrapper;
use ByJG\Mail\Wrapper\PHPMailerWrapper;
use ByJG\Mail\Wrapper\SendMailWrapper;

// backward compatibility
if (!class_exists('\PHPUnit\Framework\TestCase')) {
class_alias('\PHPUnit_Framework_TestCase', '\PHPUnit\Framework\TestCase');
}

class MailerWrapperTest extends TestCase
{

/**
* @throws \ByJG\Mail\Exception\InvalidMailHandlerException
*/
public function testRegisterMailer()
{
MailerFactory::registerMailer('smtp', PHPMailerWrapper::class);
MailerFactory::registerMailer('tls', PHPMailerWrapper::class);
MailerFactory::registerMailer('ssl', PHPMailerWrapper::class);
MailerFactory::registerMailer('sendmail', SendMailWrapper::class);
MailerFactory::registerMailer('mailgun', MailgunApiWrapper::class);
MailerFactory::registerMailer('ses', AmazonSesWrapper::class);
}

/**
* @throws \ByJG\Mail\Exception\InvalidMailHandlerException
* @expectedException \ByJG\Mail\Exception\InvalidMailHandlerException
*/
public function testRegisterMailerFail()
{
MailerFactory::registerMailer('some', '\\Non\\Existant\\Class');
}

/**
* @throws \ByJG\Mail\Exception\InvalidMailHandlerException
* @throws \ByJG\Mail\Exception\ProtocolNotRegisteredException
*/
public function testCreate()
{
MailerFactory::registerMailer('smtp', PHPMailerWrapper::class);
MailerFactory::create('smtp://localhost');
}

/**
* @throws \ByJG\Mail\Exception\ProtocolNotRegisteredException
* @expectedException \ByJG\Mail\Exception\ProtocolNotRegisteredException
* @throws \ByJG\Mail\Exception\InvalidMailHandlerException
*/
public function testCreateFail()
{
MailerFactory::registerMailer('smtp', PHPMailerWrapper::class);
MailerFactory::create('some://localhost');
}
}
2 changes: 1 addition & 1 deletion tests/resources/attachmentenvelope.txt
Expand Up @@ -5,7 +5,7 @@ Cc: cc1@email.com, cc2@email.com
Reply-To: from@email.com
Subject: Subject
Message-ID: <90dfa8c5628ceef4d0579b59b42b784c@jg-Latitude-E6520>
X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
X-Mailer: PHPMailer (https://github.com/PHPMailer/PHPMailer)
Bcc: bcc1@email.com
Bcc: bcc2@email.com
MIME-Version: 1.0
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/basicenvelope.txt
Expand Up @@ -4,7 +4,7 @@ From: from@email.com
Reply-To: from@email.com
Subject: Subject
Message-ID: <90dfa8c5628ceef4d0579b59b42b784c@jg-Latitude-E6520>
X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
X-Mailer: PHPMailer (https://github.com/PHPMailer/PHPMailer)
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="b1_90dfa8c5628ceef4d0579b59b42b784c"
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/embedenvelope.txt
Expand Up @@ -5,7 +5,7 @@ Cc: cc1@email.com, cc2@email.com
Reply-To: from@email.com
Subject: Subject
Message-ID: <90dfa8c5628ceef4d0579b59b42b784c@jg-Latitude-E6520>
X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
X-Mailer: PHPMailer (https://github.com/PHPMailer/PHPMailer)
Bcc: bcc1@email.com
Bcc: bcc2@email.com
MIME-Version: 1.0
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/fullenvelope.txt
Expand Up @@ -5,7 +5,7 @@ Cc: cc1@email.com, cc2@email.com
Reply-To: from@email.com
Subject: Subject
Message-ID: <90dfa8c5628ceef4d0579b59b42b784c@jg-Latitude-E6520>
X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
X-Mailer: PHPMailer (https://github.com/PHPMailer/PHPMailer)
Bcc: bcc1@email.com
Bcc: bcc2@email.com
MIME-Version: 1.0
Expand Down

0 comments on commit f7c21b2

Please sign in to comment.