From e97d85ce489379c7f352e321dda8794fe9f3a052 Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 17 Jul 2017 12:22:28 -0300 Subject: [PATCH 1/9] resolves #38 --- Model/Message.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Model/Message.php b/Model/Message.php index b03d996..2bfe9fc 100755 --- a/Model/Message.php +++ b/Model/Message.php @@ -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) From c4782fd576d557e6d8ebf368f175c7d8519e5539 Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 17 Jul 2017 12:22:42 -0300 Subject: [PATCH 2/9] config test --- Test/Integration/ModuleConfigTest.php | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Test/Integration/ModuleConfigTest.php diff --git a/Test/Integration/ModuleConfigTest.php b/Test/Integration/ModuleConfigTest.php new file mode 100644 index 0000000..6e69a10 --- /dev/null +++ b/Test/Integration/ModuleConfigTest.php @@ -0,0 +1,60 @@ +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)); + } +} \ No newline at end of file From d0387c37791ce0e10096bb65f8815cd33c101552 Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 17 Jul 2017 15:37:46 -0300 Subject: [PATCH 3/9] v3.0.11 on config --- etc/module.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/etc/module.xml b/etc/module.xml index 651c80a..0c66bfd 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - - + + \ No newline at end of file From 6000c33c30cee3862a4e125339cc161a84d4775c Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 17 Jul 2017 15:38:14 -0300 Subject: [PATCH 4/9] remove unused dependencies and throw exception when email cannot be sent --- Model/Transport.php | 59 ++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/Model/Transport.php b/Model/Transport.php index d3e6a97..32e516d 100755 --- a/Model/Transport.php +++ b/Model/Transport.php @@ -16,19 +16,13 @@ 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 @@ -36,15 +30,11 @@ class Transport implements \Magento\Framework\Mail\TransportInterface */ 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() { @@ -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(); } } From b5f13a0f4d50275e4cc7e5a78a6385fb92953f34 Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 17 Jul 2017 15:42:49 -0300 Subject: [PATCH 5/9] tests update --- Test/Unit/Model/TransportTest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Test/Unit/Model/TransportTest.php b/Test/Unit/Model/TransportTest.php index b780ef2..97de013 100755 --- a/Test/Unit/Model/TransportTest.php +++ b/Test/Unit/Model/TransportTest.php @@ -45,7 +45,16 @@ protected function setUp() ->getMock(); $this->messagesMock = $this->getMockBuilder('Mandrill\Messages')->disableOriginalConstructor()->disableAutoload()->setMethods(array('send'))->getMock(); - $this->messagesMock->expects($this->any())->method('send')->willReturn(true); + $this->messagesMock->expects($this->any())->method('send')->willReturn( + [ + [ + 'status' => 'accepted', + 'email' => 'gonzalo@ebizmarts.com', + '_id' => 'da911aasd132', + 'reject_reason' => '' + ] + ] + ); } /** From 1901c1c088ee770c98ec791f041a7e176caedf94 Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 17 Jul 2017 16:25:39 -0300 Subject: [PATCH 6/9] coding-standard version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1987a01..7719306 100755 --- a/.travis.yml +++ b/.travis.yml @@ -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: From 0e18c0becdd855850464570fe19b4f37d4887338 Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 17 Jul 2017 16:31:33 -0300 Subject: [PATCH 7/9] Expected 1 newline at end of file; 0 found --- Test/Integration/ModuleConfigTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Test/Integration/ModuleConfigTest.php b/Test/Integration/ModuleConfigTest.php index 6e69a10..beb9cdc 100644 --- a/Test/Integration/ModuleConfigTest.php +++ b/Test/Integration/ModuleConfigTest.php @@ -57,4 +57,4 @@ public function testTheModuleIsConfiguredInTheRealEnvironment() ); $this->assertTrue($moduleList->has(self::MODULE_NAME)); } -} \ No newline at end of file +} From 47f58f5709835e73482da5c618f003e8238a1772 Mon Sep 17 00:00:00 2001 From: Jeremy Rimpo Date: Fri, 21 Jul 2017 12:21:58 -0500 Subject: [PATCH 8/9] Plug into TransportInterfaceFactory instead of Transport --- Model/Plugin/TransportInterfaceFactory.php | 73 ++++++++++++++++ Model/Plugin/TransportPlugin.php | 39 --------- .../Plugin/TransportInterfaceFactoryTest.php | 85 +++++++++++++++++++ .../Unit/Model/Plugin/TransportPluginTest.php | 79 ----------------- etc/di.xml | 4 +- 5 files changed, 160 insertions(+), 120 deletions(-) create mode 100644 Model/Plugin/TransportInterfaceFactory.php delete mode 100644 Model/Plugin/TransportPlugin.php create mode 100644 Test/Unit/Model/Plugin/TransportInterfaceFactoryTest.php delete mode 100644 Test/Unit/Model/Plugin/TransportPluginTest.php diff --git a/Model/Plugin/TransportInterfaceFactory.php b/Model/Plugin/TransportInterfaceFactory.php new file mode 100644 index 0000000..3647610 --- /dev/null +++ b/Model/Plugin/TransportInterfaceFactory.php @@ -0,0 +1,73 @@ + + * @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(); + } +} diff --git a/Model/Plugin/TransportPlugin.php b/Model/Plugin/TransportPlugin.php deleted file mode 100644 index 951128f..0000000 --- a/Model/Plugin/TransportPlugin.php +++ /dev/null @@ -1,39 +0,0 @@ -mandrillHelper = $mandrillHelper; - $this->mandrillTransportFactory = $mandrillTransportFactory; - } - - public function aroundSendMessage(\Magento\Framework\Mail\Transport $mailTransport, callable $proceed) - { - if ($this->isMandrillEnabled() === false) { - $proceed(); - } else { - /** @var \Ebizmarts\Mandrill\Model\Transport $mandrillTransport */ - $mandrillTransport = $this->mandrillTransportFactory->create(); - $mandrillTransport->sendMessage(); - } - - return null; - } - - /** - * @return bool - */ - private function isMandrillEnabled() - { - return $this->mandrillHelper->isMandrillEnabled(); - } -} diff --git a/Test/Unit/Model/Plugin/TransportInterfaceFactoryTest.php b/Test/Unit/Model/Plugin/TransportInterfaceFactoryTest.php new file mode 100644 index 0000000..98f818c --- /dev/null +++ b/Test/Unit/Model/Plugin/TransportInterfaceFactoryTest.php @@ -0,0 +1,85 @@ +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) + ); + } +} diff --git a/Test/Unit/Model/Plugin/TransportPluginTest.php b/Test/Unit/Model/Plugin/TransportPluginTest.php deleted file mode 100644 index e16c04e..0000000 --- a/Test/Unit/Model/Plugin/TransportPluginTest.php +++ /dev/null @@ -1,79 +0,0 @@ -getMockBuilder(\Magento\Framework\Mail\Transport::class) - ->disableOriginalConstructor() - ->getMock(); - - $callable = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); - $callable->expects($this->once())->method('__invoke'); - - $mandrillHelperMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Helper\Data::class) - ->setMethods(['isActive']) - ->disableOriginalConstructor() - ->getMock(); - $mandrillHelperMock->expects($this->once())->method('isActive')->willReturn(0); - - $mandrillTransportFactoryMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Model\TransportFactory::class) - ->disableOriginalConstructor() - ->getMock(); - $mandrillTransportFactoryMock->expects($this->never())->method('sendMessage'); - - $this->callAroundMessage($mandrillHelperMock, $mandrillTransportFactoryMock, $mailTransportMock, $callable); - } - - public function testAroundSaveMandrill() - { - $mailTransportMock = $this->getMockBuilder(\Magento\Framework\Mail\Transport::class) - ->disableOriginalConstructor() - ->getMock(); - - $callable = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); - $callable->expects($this->never())->method('__invoke'); - - $mandrillHelperMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Helper\Data::class) - ->setMethods(['isActive']) - ->disableOriginalConstructor() - ->getMock(); - $mandrillHelperMock->expects($this->once())->method('isActive')->willReturn(1); - - $mandrillTransportMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Model\Transport::class) - ->disableOriginalConstructor() - ->setMethods(['sendMessage']) - ->getMock(); - $mandrillTransportMock->expects($this->once())->method('sendMessage'); - - $mandrillTransportFactoryMock = $this->getMockBuilder(\Ebizmarts\Mandrill\Model\TransportFactory::class) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $mandrillTransportFactoryMock->expects($this->once())->method('create')->willReturn($mandrillTransportMock); - - $this->callAroundMessage($mandrillHelperMock, $mandrillTransportFactoryMock, $mailTransportMock, $callable); - } - - /** - * @param $mandrillHelperMock - * @param $mandrillTransportFactoryMock - * @param $mailTransportMock - * @param $callable - */ - private function callAroundMessage( - $mandrillHelperMock, - $mandrillTransportFactoryMock, - $mailTransportMock, - $callable - ) { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $transportPlugin = $objectManager->getObject("Ebizmarts\Mandrill\Model\Plugin\TransportPlugin", [ - "mandrillHelper" => $mandrillHelperMock, - "mandrillTransportFactory" => $mandrillTransportFactoryMock - ]); - $transportPlugin->aroundSendMessage($mailTransportMock, $callable); - } -} diff --git a/etc/di.xml b/etc/di.xml index c24bf75..b42e1fc 100755 --- a/etc/di.xml +++ b/etc/di.xml @@ -12,8 +12,8 @@ --> - - + + From ce752bc0394403ab3b1f240e933a9c1cb482db89 Mon Sep 17 00:00:00 2001 From: centerax Date: Mon, 24 Jul 2017 09:40:34 -0300 Subject: [PATCH 9/9] v3.0.12 --- composer.json | 2 +- etc/module.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 7c0ab4b..6dbba62 100755 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "description": "Connect Mandrill with Magento", "type": "magento2-module", - "version": "3.0.11", + "version": "3.0.12", "authors": [ { "name": "Ebizmarts Corp", diff --git a/etc/module.xml b/etc/module.xml index 0c66bfd..578abee 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file