Skip to content

Commit

Permalink
Merge pull request #66 from Silarn/develop
Browse files Browse the repository at this point in the history
Magento 2.2.4: Ensure Message objects are the same on both TransportB…
  • Loading branch information
centerax authored May 24, 2018
2 parents 8795a2e + eb1ba95 commit de4cab5
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
#- 5.6
- 7.0
- 7.1

env:
- TEST_SUITE=unittests
Expand All @@ -16,7 +17,7 @@ script:
- cd ..
- wget https://gist.githubusercontent.com/centerax/5e42abec89d62a1308ba/raw/4b3242361c0690e234266afb3520c2940ac1055a/auth.json
- /bin/cp auth.json ~/.composer/
- composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.1.9
- composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition=2.2.4
- 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"
Expand Down
39 changes: 39 additions & 0 deletions Model/SenderBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Ebizmarts\Mandrill\Model;

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\Template\TransportBuilderByStore;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\ObjectManagerInterface;

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
/**
* SenderBuilder constructor.
*
* @param Template $templateContainer
* @param IdentityInterface $identityContainer
* @param ObjectManagerInterface $objectManager
*/
public function __construct(
Template $templateContainer,
IdentityInterface $identityContainer,
ObjectManagerInterface $objectManager
) {
/** @var MessageInterface $message */
$message = $objectManager->create(MessageInterface::class);
/** @var TransportBuilder $transportBuilder */
$transportBuilder = $objectManager->create(
TransportBuilder::class,
["message" => $message]
);
/** @var TransportBuilderByStore $transportBuilderByStore */
$transportBuilderByStore = $objectManager->create(
TransportBuilderByStore::class,
["message" => $message]
);
parent::__construct($templateContainer, $identityContainer, $transportBuilder, $transportBuilderByStore);
}
}
2 changes: 1 addition & 1 deletion Test/Integration/ModuleConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Magento\Framework\Module\ModuleList;
use Magento\TestFramework\ObjectManager;

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

Expand Down
8 changes: 5 additions & 3 deletions Test/Unit/Controller/Adminhtml/Email/TestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class TestTest extends \PHPUnit_Framework_TestCase
class TestTest extends \PHPUnit\Framework\TestCase
{
protected $request;
protected $helper;
Expand All @@ -25,7 +25,9 @@ class TestTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->request = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
$this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
->disableOriginalConstructor()
->getMock();
$this->request->expects($this->any())
->method('getParam')
->with('email')
Expand Down Expand Up @@ -70,7 +72,7 @@ protected function setUp()
}

/**
* @covers Ebizmarts\Mandrill\Controller\Adminhtml\Email\Test::execute
* @covers \Ebizmarts\Mandrill\Controller\Adminhtml\Email\Test::execute
*/
public function testExecute()
{
Expand Down
2 changes: 1 addition & 1 deletion Test/Unit/Controller/Autoresponder/UnsubscribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class UnsubscribeTest extends \PHPUnit_Framework_TestCase
class UnsubscribeTest extends \PHPUnit\Framework\TestCase
{

const EMAIL = "[email protected]";
Expand Down
2 changes: 1 addition & 1 deletion Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class DataTest extends \PHPUnit_Framework_TestCase
class DataTest extends \PHPUnit\Framework\TestCase
{
protected $_scopeMock;
protected $_helper;
Expand Down
2 changes: 1 addition & 1 deletion Test/Unit/Model/Config/Source/DetailsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

class DetailsTest extends \PHPUnit_Framework_TestCase
class DetailsTest extends \PHPUnit\Framework\TestCase
{
protected $_details;

Expand Down
8 changes: 4 additions & 4 deletions Test/Unit/Model/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use \Ebizmarts\Mandrill\Model\Message;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

class MessageTest extends \PHPUnit_Framework_TestCase
class MessageTest extends \PHPUnit\Framework\TestCase
{
private $helperMock;
private $apiMock;
Expand Down Expand Up @@ -71,7 +71,7 @@ public function testSetSubjectMandrillNotEnabled()
{
$this->disableMandrill();
$this->_message->setSubject('subject');
\PHPUnit_Framework_Assert::assertAttributeEquals(null, "subject", $this->_message);
\PHPUnit\Framework\Assert::assertAttributeEquals(null, "subject", $this->_message);
}
/**
* @covers \Ebizmarts\Mandrill\Model\Message::setBody
Expand Down Expand Up @@ -137,10 +137,10 @@ public function testAddToMandrillDisabled()
{
$this->disableMandrill();
$this->_message->addTo('to');
\PHPUnit_Framework_Assert::assertAttributeEquals(array(), "mandrillTo", $this->_message);
\PHPUnit\Framework\Assert::assertAttributeEquals(array(), "mandrillTo", $this->_message);

$this->_message->addTo(array('to1','to2'));
\PHPUnit_Framework_Assert::assertAttributeEquals(array(), "mandrillTo", $this->_message);
\PHPUnit\Framework\Assert::assertAttributeEquals(array(), "mandrillTo", $this->_message);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Test/Unit/Model/Plugin/TransportInterfaceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

class TransportInterfaceFactoryTest extends \PHPUnit_Framework_TestCase
class TransportInterfaceFactoryTest extends \PHPUnit\Framework\TestCase
{
/**
* Main TransportInterfaceFactory test class
Expand Down
78 changes: 78 additions & 0 deletions Test/Unit/Model/SenderBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Ebizmarts_Mandrill Magento JS component
*
* @category Ebizmarts
* @package Ebizmarts_Mandrill
* @author Ebizmarts Team <[email protected]>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/


namespace Ebizmarts\Mandrill\Test\Unit\Model;

use Ebizmarts\Mandrill\Model\SenderBuilder;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Mail\Template\TransportBuilderByStore;
use \Magento\Framework\ObjectManagerInterface;
use Magento\Sales\Model\Order\Email\Container\IdentityInterface;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Framework\Mail\MessageInterface;

class SenderBuilderTest extends \PHPUnit\Framework\TestCase
{
public function testSameMessage()
{
// Core mock class
$builder = $this->getMockBuilder(SenderBuilder::class)
->disableOriginalConstructor()
->getMock();

// Mock parameters
$templateMock = $this->getMockBuilder(Template::class)
->disableOriginalConstructor()
->getMock();
$identityMock = $this->getMockBuilder(IdentityInterface::class)
->disableOriginalConstructor()
->getMock();
$objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
->disableOriginalConstructor()
->getMock();

// Message object to get and test against
$messageMock = $this->getMockBuilder(MessageInterface::class)
->disableOriginalConstructor()
->getMock();

// Transport builders created by ObjectManager
$transportBuilderMock = $this->getMockBuilder(TransportBuilder::class)
->disableOriginalConstructor()
->getMock();
$transportBuilderSenderMock = $this->getMockBuilder(TransportBuilderByStore::class)
->disableOriginalConstructor()
->getMock();

// Make sure the ObjectManager returns our mock Message
$objectManagerMock->expects($this->at(0))
->method('create')
->will($this->returnValue($messageMock));

// Tests that both TransportBuilder models are created with the same Message object
$objectManagerMock->expects($this->at(1))
->method('create')
->with($this->equalTo(TransportBuilder::class), $this->contains($messageMock))
->will($this->returnValue($transportBuilderMock));
$objectManagerMock->expects($this->at(2))
->method('create')
->with($this->equalTo(TransportBuilderByStore::class), $this->contains($messageMock))
->will($this->returnValue($transportBuilderSenderMock));

// Create a reflection of the SenderBuilder constructor to invoke on our mock Builder
$reflection = new \ReflectionClass(SenderBuilder::class);
$constructor = $reflection->getConstructor();

// Invoke the mock Builder with a reflection of the constructor and our mock objects
$constructor->invoke($builder, $templateMock, $identityMock, $objectManagerMock);
}
}
2 changes: 1 addition & 1 deletion Test/Unit/Model/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use \Ebizmarts\Mandrill\Model\Transport;

class TransportTest extends \PHPUnit_Framework_TestCase
class TransportTest extends \PHPUnit\Framework\TestCase
{
private $mandrillApiMock;
private $objectManager;
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"forum": "http://ebizmarts.com/forums/view/1"
},
"require" : {
"mandrill/mandrill": "1.0.*"
"mandrill/mandrill": "1.0.*",
"magento/module-sales": "^101.0.3"
}
}
4 changes: 3 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Framework\Mail\Message" type="Ebizmarts\Mandrill\Model\Message"/>
<preference for="Magento\Framework\Mail\Message" type="Ebizmarts\Mandrill\Model\Message"/>
<preference for="Magento\Sales\Model\Order\Email\SenderBuilder" type="Ebizmarts\Mandrill\Model\SenderBuilder"/>
<type name="Magento\Framework\Mail\TransportInterfaceFactory">
<plugin name="mandrill-send-message" type="Ebizmarts\Mandrill\Model\Plugin\TransportInterfaceFactory" sortOrder="1" />
</type>
<type name="Magento\Config\Model\Config">
<plugin name="mandrill-save" type="Ebizmarts\Mandrill\Model\Plugin\Config" sortOrder="10"/>
</type>
<type name="Ebizmarts\Mandrill\Model\Message"/>
</config>

0 comments on commit de4cab5

Please sign in to comment.