Skip to content

Commit

Permalink
Merge pull request #43 from ebizmarts/develop
Browse files Browse the repository at this point in the history
Version 3.0.12
  • Loading branch information
centerax authored Jul 24, 2017
2 parents f621961 + ce752bc commit 0c7f60a
Show file tree
Hide file tree
Showing 12 changed files with 266 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ script:
- mkdir -p project-community-edition/app/code/Ebizmarts/Mandrill
- /bin/cp -r magento2-mandrill/* project-community-edition/app/code/Ebizmarts/Mandrill
- sh -c "if [ '$TEST_SUITE' = 'unittests' ]; then cd project-community-edition && /bin/cp app/code/Ebizmarts/Mandrill/phpunit_config.xml dev/tests/unit/ && ./vendor/bin/phpunit -c dev/tests/unit/phpunit_config.xml app/code/Ebizmarts/Mandrill/Test/Unit --coverage-text; fi"
- sh -c "if [ '$TEST_SUITE' = 'ecgcodingstandard' ]; then cd project-community-edition && composer require --prefer-source --no-interaction "mandrill/mandrill=1.0.*" "squizlabs/php_codesniffer=2.8.*" magento-ecg/coding-standard && vendor/squizlabs/php_codesniffer/scripts/phpcs --config-set installed_paths vendor/magento-ecg/coding-standard && ./vendor/squizlabs/php_codesniffer/scripts/phpcs -n --standard="EcgM2" app/code/Ebizmarts/Mandrill/; fi"
- sh -c "if [ '$TEST_SUITE' = 'ecgcodingstandard' ]; then cd project-community-edition && composer require --prefer-source --no-interaction "mandrill/mandrill=1.0.*" "squizlabs/php_codesniffer=2.8.*" "magento-ecg/coding-standard=^2.0" && vendor/squizlabs/php_codesniffer/scripts/phpcs --config-set installed_paths vendor/magento-ecg/coding-standard && ./vendor/squizlabs/php_codesniffer/scripts/phpcs -n --standard="EcgM2" app/code/Ebizmarts/Mandrill/; fi"
- sh -c "if [ '$TEST_SUITE' = 'marketplaceeqp' ]; then cd project-community-edition && composer require --prefer-source --no-interaction "squizlabs/php_codesniffer=2.8.*" magento/marketplace-eqp && vendor/squizlabs/php_codesniffer/scripts/phpcs --config-set installed_paths vendor/magento/marketplace-eqp && ./vendor/squizlabs/php_codesniffer/scripts/phpcs -n --standard="MEQP2" app/code/Ebizmarts/Mandrill/; fi"

notifications:
Expand Down
1 change: 1 addition & 0 deletions Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Message extends \Magento\Framework\Mail\Message implements \Magento\Framew
public function __construct(\Ebizmarts\Mandrill\Helper\Data $helper)
{
$this->mandrillHelper = $helper;
parent::__construct();
}

public function setSubject($subject)
Expand Down
73 changes: 73 additions & 0 deletions Model/Plugin/TransportInterfaceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Ebizmarts_Mandrill Magento JS component
*
* @category Ebizmarts
* @package Ebizmarts_Mandrill
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace Ebizmarts\Mandrill\Model\Plugin;

class TransportInterfaceFactory
{
/**
* Mandrill Transport Factory
*
* @var \Ebizmarts\Mandrill\Model\TransportFactory
*/
protected $mandrillTransportFactory;

/**
* Mandrill Helper class
*
* @var \Ebizmarts\Mandrill\Helper\Data
*/
protected $mandrillHelper;

/**
* TransportBuilder constructor.
* @param \Ebizmarts\Mandrill\Helper\Data $mandrillHelper
* @param \Ebizmarts\Mandrill\Model\TransportFactory $mandrillTransportFactory
*/
public function __construct(
\Ebizmarts\Mandrill\Helper\Data $mandrillHelper,
\Ebizmarts\Mandrill\Model\TransportFactory $mandrillTransportFactory
) {
$this->mandrillHelper = $mandrillHelper;
$this->mandrillTransportFactory = $mandrillTransportFactory;
}

/**
* Replace mail transport with Mandrill if needed
*
* @param \Magento\Framework\Mail\TransportInterfaceFactory $subject
* @param \Closure $proceed
* @param array $data
*
* @return \Magento\Framework\Mail\TransportInterface
*/
public function aroundCreate(
\Magento\Framework\Mail\TransportInterfaceFactory $subject,
\Closure $proceed,
array $data = []
) {
if ($this->isMandrillEnabled() === false) {
/** @var \Magento\Framework\Mail\TransportInterface $transport */
return $proceed($data);
} else {
return $this->mandrillTransportFactory->create($data);
}
}

/**
* Get status of Mandrill
*
* @return bool
*/
private function isMandrillEnabled()
{
return $this->mandrillHelper->isMandrillEnabled();
}
}
39 changes: 0 additions & 39 deletions Model/Plugin/TransportPlugin.php

This file was deleted.

59 changes: 31 additions & 28 deletions Model/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,25 @@ class Transport implements \Magento\Framework\Mail\TransportInterface
/**
* @var \Ebizmarts\Mandrill\Model\Message
*/
protected $_message;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $_logger;
/**
* @var \Ebizmarts\Mandrill\Helper\Data
*/
protected $_helper;
private $message;

/**
* @var Api\Mandrill
*/
protected $_api;
private $api;

/**
* @param \Magento\Framework\Mail\MessageInterface $message
* @param \Psr\Log\LoggerInterface $logger
* @param \Ebizmarts\Mandrill\Helper\Data $helper
*/
public function __construct(
\Ebizmarts\Mandrill\Model\Message $message,
\Psr\Log\LoggerInterface $logger,
\Ebizmarts\Mandrill\Helper\Data $helper,
\Ebizmarts\Mandrill\Model\Api\Mandrill $api
) {

$this->_message = $message;
$this->_logger = $logger;
$this->_helper = $helper;
$this->_api = $api;
$this->message = $message;
$this->api = $api;
}
public function sendMessage()
{
Expand All @@ -55,46 +45,59 @@ public function sendMessage()
}

$message = array(
'subject' => $this->_message->getSubject(),
'from_name' => $this->_message->getFromName(),
'from_email'=> $this->_message->getFrom(),
'subject' => $this->message->getSubject(),
'from_name' => $this->message->getFromName(),
'from_email'=> $this->message->getFrom(),
);
foreach ($this->_message->getTo() as $to) {
foreach ($this->message->getTo() as $to) {
$message['to'][] = array(
'email' => $to
);
}
foreach ($this->_message->getBcc() as $bcc) {
foreach ($this->message->getBcc() as $bcc) {
$message['to'][] = array(
'email' => $bcc,
'type' => 'bcc'
);
}
if ($att = $this->_message->getAttachments()) {
if ($att = $this->message->getAttachments()) {
$message['attachments'] = $att;
}
if ($headers = $this->_message->getHeaders()) {
if ($headers = $this->message->getHeaders()) {
$message['headers'] = $headers;
}
switch ($this->_message->getType()) {
switch ($this->message->getType()) {
case \Magento\Framework\Mail\MessageInterface::TYPE_HTML:
$message['html'] = $this->_message->getBody();
$message['html'] = $this->message->getBody();
break;
case \Magento\Framework\Mail\MessageInterface::TYPE_TEXT:
$message['text'] = $this->_message->getBody();
$message['text'] = $this->message->getBody();
break;
}

$mandrillApiInstance->messages->send($message);
$result = $mandrillApiInstance->messages->send($message);

$this->processApiCallResult($result);

return true;
}

private function processApiCallResult($result)
{
$currentResult = current($result);

if (array_key_exists('status', $currentResult) && $currentResult['status'] == 'rejected') {
throw new \Magento\Framework\Exception\MailException(
new \Magento\Framework\Phrase("Email sending failed: %1", [$currentResult['reject_reason']])
);
}
}

/**
* @return \Mandrill
*/
private function getMandrillApiInstance()
{
return $this->_api->getApi();
return $this->api->getApi();
}
}
60 changes: 60 additions & 0 deletions Test/Integration/ModuleConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Ebizmarts\Mandrill\Test\Integration;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Module\ModuleList;
use Magento\TestFramework\ObjectManager;

class ModuleConfigTest extends \PHPUnit_Framework_TestCase
{
const MODULE_NAME = 'Ebizmarts_Mandrill';

/**
* @var $objectManager ObjectManager
*/
private $objectManager;

protected function setUp()
{
/** @var ObjectManager objectManager */
$this->objectManager = ObjectManager::getInstance();
}

public function testTheModuleIsRegistered()
{
$registrar = new ComponentRegistrar();
$this->assertArrayHasKey(self::MODULE_NAME, $registrar->getPaths(ComponentRegistrar::MODULE));
}

public function testTheModuleIsConfiguredInTheTestEnvironment()
{
/** @var $moduleList ModuleList */
$moduleList = $this->objectManager->create(ModuleList::class);
$this->assertTrue($moduleList->has(self::MODULE_NAME));
}

public function testTheModuleIsConfiguredInTheRealEnvironment()
{
// The tests by default point to the wrong config directory for this test.
$directoryList = $this->objectManager->create(
\Magento\Framework\App\Filesystem\DirectoryList::class,
['root' => BP]
);
$deploymentConfigReader = $this->objectManager->create(
\Magento\Framework\App\DeploymentConfig\Reader::class,
['dirList' => $directoryList]
);
$deploymentConfig = $this->objectManager->create(
\Magento\Framework\App\DeploymentConfig::class,
['reader' => $deploymentConfigReader]
);

/** @var $moduleList ModuleList */
$moduleList = $this->objectManager->create(
ModuleList::class,
['config' => $deploymentConfig]
);
$this->assertTrue($moduleList->has(self::MODULE_NAME));
}
}
85 changes: 85 additions & 0 deletions Test/Unit/Model/Plugin/TransportInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Ebizmarts\Mandrill\Test\Unit\Model\Plugin;

class TransportInterfaceFactoryTest extends \PHPUnit_Framework_TestCase
{
/**
* Main TransportInterfaceFactory test class
*
* @var \Ebizmarts\Mandrill\Model\Plugin\TransportInterfaceFactory
*/
protected $factoryClass;

/**
* Returned Mandrill transport model
*
* @var \Ebizmarts\Mandrill\Model\Transport
*/
protected $mandrillTransportMock;

/**
* Constructor message data
*
* @var array
*/
protected $data;

public function setUp()
{
$mailMessageMock = $this->getMockBuilder(\Magento\Framework\Mail\Message::class)
->disableOriginalConstructor()
->getMock();

$this->data = [
'message' => $mailMessageMock
];

$mandrillHelperMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Helper\Data::class)
->setMethods(['isMandrillEnabled'])
->disableOriginalConstructor()
->getMock();
$mandrillHelperMock->expects($this->at(0))->method('isMandrillEnabled')->will($this->returnValue(false));
$mandrillHelperMock->expects($this->at(1))->method('isMandrillEnabled')->will($this->returnValue(true));

$mandrillTransportFactoryMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Model\TransportFactory::class)
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();

$this->mandrillTransportMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Model\Transport::class)
->disableOriginalConstructor()
->getMock();

$mandrillTransportFactoryMock->expects($this->once())->method('create')
->with($this->equalTo($this->data))->willReturn($this->mandrillTransportMock);

$this->factoryClass = new \Ebizmarts\Mandrill\Model\Plugin\TransportInterfaceFactory($mandrillHelperMock, $mandrillTransportFactoryMock);
}

public function testAroundCreate()
{
$transportFactoryMock = $this->getMockBuilder(\Magento\Framework\Mail\TransportInterfaceFactory::class)
->disableOriginalConstructor()
->getMock();

$transportInterfaceMock = $this->getMockBuilder(\Magento\Framework\Mail\TrasportInterface::class)
->disableOriginalConstructor()
->getMock();

$closure = function ($data) use ($transportInterfaceMock) {
$this->assertSame($this->data, $data);
return $transportInterfaceMock;
};

$this->assertEquals(
$transportInterfaceMock,
$this->factoryClass->aroundCreate($transportFactoryMock, $closure, $this->data)
);

$this->assertEquals(
$this->mandrillTransportMock,
$this->factoryClass->aroundCreate($transportFactoryMock, $closure, $this->data)
);
}
}
Loading

0 comments on commit 0c7f60a

Please sign in to comment.