diff --git a/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php b/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php index a8374be59ff65..cd8cf94ab50ee 100644 --- a/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php +++ b/lib/internal/Magento/Framework/Mail/Template/TransportBuilder.php @@ -11,6 +11,7 @@ use Magento\Framework\App\TemplateTypesInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Mail\MessageInterface; +use Magento\Framework\Mail\MessageInterfaceFactory; use Magento\Framework\Mail\TransportInterfaceFactory; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Phrase; @@ -88,25 +89,36 @@ class TransportBuilder */ protected $mailTransportFactory; + /** + * @var \Magento\Framework\Mail\MessageInterfaceFactory + */ + private $messageFactory; + /** * @param FactoryInterface $templateFactory * @param MessageInterface $message * @param SenderResolverInterface $senderResolver * @param ObjectManagerInterface $objectManager * @param TransportInterfaceFactory $mailTransportFactory + * @param MessageInterfaceFactory $messageFactory + * + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( FactoryInterface $templateFactory, MessageInterface $message, SenderResolverInterface $senderResolver, ObjectManagerInterface $objectManager, - TransportInterfaceFactory $mailTransportFactory + TransportInterfaceFactory $mailTransportFactory, + MessageInterfaceFactory $messageFactory = null ) { $this->templateFactory = $templateFactory; - $this->message = $message; $this->objectManager = $objectManager; $this->_senderResolver = $senderResolver; $this->mailTransportFactory = $mailTransportFactory; + $this->messageFactory = $messageFactory ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(MessageInterfaceFactory::class); + $this->message = $this->messageFactory->create(); } /** @@ -242,7 +254,7 @@ public function getTransport() */ protected function reset() { - $this->message = $this->objectManager->create(\Magento\Framework\Mail\Message::class); + $this->message = $this->messageFactory->create(); $this->templateIdentifier = null; $this->templateVars = null; $this->templateOptions = null; diff --git a/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php b/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php index e79d12310436c..e1ebbb421468a 100644 --- a/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php +++ b/lib/internal/Magento/Framework/Mail/Test/Unit/Template/TransportBuilderTest.php @@ -8,6 +8,7 @@ use Magento\Framework\App\TemplateTypesInterface; use Magento\Framework\Mail\MessageInterface; +use Magento\Framework\Mail\MessageInterfaceFactory; class TransportBuilderTest extends \PHPUnit\Framework\TestCase { @@ -41,6 +42,11 @@ class TransportBuilderTest extends \PHPUnit\Framework\TestCase */ protected $senderResolverMock; + /** + * @var \Magento\Framework\Mail\MessageInterfaceFactory| \PHPUnit_Framework_MockObject_MockObject + */ + private $messageFactoryMock; + /** * @var \PHPUnit_Framework_MockObject_MockObject */ @@ -60,7 +66,12 @@ protected function setUp() \Magento\Framework\Mail\TransportInterfaceFactory::class )->disableOriginalConstructor() ->setMethods(['create']) - ->getMock(); + ->getMockForAbstractClass(); + $this->messageFactoryMock = $this->getMockBuilder(\Magento\Framework\Mail\MessageInterfaceFactory::class) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMockForAbstractClass(); + $this->messageFactoryMock->expects($this->atLeastOnce())->method('create')->willReturn($this->messageMock); $this->builder = $objectManagerHelper->getObject( $this->builderClassName, [ @@ -68,7 +79,8 @@ protected function setUp() 'message' => $this->messageMock, 'objectManager' => $this->objectManagerMock, 'senderResolver' => $this->senderResolverMock, - 'mailTransportFactory' => $this->mailTransportFactoryMock + 'mailTransportFactory' => $this->mailTransportFactoryMock, + 'messageFactory' => $this->messageFactoryMock ] ); } @@ -122,10 +134,7 @@ public function testGetTransport($templateType, $messageType, $bodyText, $templa ->with($this->equalTo(['message' => $this->messageMock])) ->willReturn($transport); - $this->objectManagerMock->expects($this->at(0)) - ->method('create') - ->with($this->equalTo(\Magento\Framework\Mail\Message::class)) - ->willReturn($transport); + $this->messageFactoryMock->expects($this->once())->method('create')->willReturn($transport); $this->builder->setTemplateIdentifier('identifier')->setTemplateVars($vars)->setTemplateOptions($options); $this->assertInstanceOf(\Magento\Framework\Mail\TransportInterface::class, $this->builder->getTransport());