Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Graham Wharton committed Oct 16, 2018
1 parent 16afc09 commit 47c5f72
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/code/Magento/Sales/Model/Order/Email/SenderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\Sales\Model\Order\Email;

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;

Expand All @@ -30,11 +31,13 @@ class SenderBuilder
* @param Template $templateContainer
* @param IdentityInterface $identityContainer
* @param TransportBuilder $transportBuilder
* @param TransportBuilderByStore $transportBuilderByStore
*/
public function __construct(
Template $templateContainer,
IdentityInterface $identityContainer,
TransportBuilder $transportBuilder
TransportBuilder $transportBuilder,
TransportBuilderByStore $transportBuilderByStore = null
) {
$this->templateContainer = $templateContainer;
$this->identityContainer = $identityContainer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,17 @@ public function setReplyTo($email, $name = null)
/**
* Set mail from address
*
* @deprecated Use setFromByStore
* @deprecated This function sets the from address for the first store only.
* new function setFromByStore introduced to allow setting of from address
* based on store.
* @see setFromByStore()
*
* @param string|array $from
* @return $this
*/
public function setFrom($from)
{
$result = $this->_senderResolver->resolve($from);
$this->message->setFrom($result['email'], $result['name']);
return $this;
return($this->setFromByStore($from));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Mail\Template;

use Magento\Framework\Mail\MessageInterface;

/**
* Class TransportBuilderByStore
*
* @deprecated The ability to set From address based on store is now available
* in the \Magento\Framework\Mail\Template\TransportBuilder class
* @see \Magento\Framework\Mail\Template\TransportBuilder::setFromByStore
*/
class TransportBuilderByStore
{
/**
* Message.
*
* @var \Magento\Framework\Mail\Message
*/
protected $message;

/**
* Sender resolver.
*
* @var \Magento\Framework\Mail\Template\SenderResolverInterface
*/
private $senderResolver;

/**
* @param MessageInterface $message
* @param SenderResolverInterface $senderResolver
*/
public function __construct(
MessageInterface $message,
SenderResolverInterface $senderResolver
) {
$this->message = $message;
$this->senderResolver = $senderResolver;
}

/**
* Set mail from address by store.
*
* @param string|array $from
* @param string|int $store
*
* @return $this
*/
public function setFromByStore($from, $store)
{
$result = $this->senderResolver->resolve($from, $store);
$this->message->clearFrom();
$this->message->setFrom($result['email'], $result['name']);

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\Mail\Test\Unit\Template;

use Magento\Framework\Mail\Template\TransportBuilderByStore;

class TransportBuilderByStoreTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\Mail\Template\TransportBuilderByStore
*/
protected $model;

/**
* @var \Magento\Framework\Mail\Message | \PHPUnit_Framework_MockObject_MockObject
*/
protected $messageMock;

/**
* @var \Magento\Framework\Mail\Template\SenderResolverInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $senderResolverMock;

/**
* @return void
*/
protected function setUp()
{
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->messageMock = $this->createMock(\Magento\Framework\Mail\Message::class);
$this->senderResolverMock = $this->createMock(\Magento\Framework\Mail\Template\SenderResolverInterface::class);

$this->model = $objectManagerHelper->getObject(
TransportBuilderByStore::class,
[
'message' => $this->messageMock,
'senderResolver' => $this->senderResolverMock,
]
);
}

/**
* @return void
*/
public function testSetFromByStore()
{
$sender = ['email' => 'from@example.com', 'name' => 'name'];
$store = 1;
$this->senderResolverMock->expects($this->once())
->method('resolve')
->with($sender, $store)
->willReturn($sender);
$this->messageMock->expects($this->once())
->method('setFrom')
->with('from@example.com', 'name')
->willReturnSelf();
$this->messageMock->expects($this->once())
->method('clearFrom')
->willReturnSelf();

$this->model->setFromByStore($sender, $store);
}
}

0 comments on commit 47c5f72

Please sign in to comment.