Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SenderBuilderTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class SenderBuilderTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $senderBuilder;
18 
23 
28 
32  protected $transportBuilder;
33 
37  private $storeMock;
38 
42  private $transportBuilderByStore;
43 
44  protected function setUp()
45  {
46  $templateId = 'test_template_id';
47  $templateOptions = ['option1', 'option2'];
48  $templateVars = ['var1', 'var2'];
49  $emailIdentity = 'email_identity_test';
50  $emailCopyTo = ['[email protected]'];
51 
52  $this->templateContainerMock = $this->createPartialMock(
53  \Magento\Sales\Model\Order\Email\Container\Template::class,
54  ['getTemplateVars', 'getTemplateOptions', 'getTemplateId']
55  );
56 
57  $this->storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, [
58  'getStoreId',
59  '__wakeup',
60  'getId',
61  ]);
62 
63  $this->identityContainerMock = $this->createPartialMock(
64  \Magento\Sales\Model\Order\Email\Container\ShipmentIdentity::class,
65  [
66  'getEmailIdentity',
67  'getCustomerEmail',
68  'getCustomerName',
69  'getTemplateOptions',
70  'getEmailCopyTo',
71  'getCopyMethod',
72  'getStore',
73  ]
74  );
75 
76  $this->transportBuilder = $this->createPartialMock(
77  \Magento\Framework\Mail\Template\TransportBuilder::class,
78  [
79  'addTo',
80  'addBcc',
81  'getTransport',
82  'setTemplateIdentifier',
83  'setTemplateOptions',
84  'setTemplateVars',
85  ]
86  );
87 
88  $this->transportBuilderByStore = $this->createMock(TransportBuilderByStore::class);
89 
90  $this->templateContainerMock->expects($this->once())
91  ->method('getTemplateId')
92  ->will($this->returnValue($templateId));
93  $this->transportBuilder->expects($this->once())
94  ->method('setTemplateIdentifier')
95  ->with($this->equalTo($templateId));
96  $this->templateContainerMock->expects($this->once())
97  ->method('getTemplateOptions')
98  ->will($this->returnValue($templateOptions));
99  $this->transportBuilder->expects($this->once())
100  ->method('setTemplateOptions')
101  ->with($this->equalTo($templateOptions));
102  $this->templateContainerMock->expects($this->once())
103  ->method('getTemplateVars')
104  ->will($this->returnValue($templateVars));
105  $this->transportBuilder->expects($this->once())
106  ->method('setTemplateVars')
107  ->with($this->equalTo($templateVars));
108 
109  $this->identityContainerMock->expects($this->once())
110  ->method('getEmailIdentity')
111  ->will($this->returnValue($emailIdentity));
112  $this->transportBuilderByStore->expects($this->once())
113  ->method('setFromByStore')
114  ->with($this->equalTo($emailIdentity));
115 
116  $this->identityContainerMock->expects($this->once())
117  ->method('getEmailCopyTo')
118  ->will($this->returnValue($emailCopyTo));
119 
120  $this->senderBuilder = new SenderBuilder(
121  $this->templateContainerMock,
122  $this->identityContainerMock,
123  $this->transportBuilder,
124  $this->transportBuilderByStore
125  );
126  }
127 
128  public function testSend()
129  {
130  $customerName = 'test_name';
131  $customerEmail = 'test_email';
132  $transportMock = $this->createMock(
133  \Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
134  );
135 
136  $this->identityContainerMock->expects($this->once())
137  ->method('getEmailCopyTo')
138  ->will($this->returnValue(['[email protected]']));
139  $this->identityContainerMock->expects($this->once())
140  ->method('getCopyMethod')
141  ->will($this->returnValue('bcc'));
142  $this->identityContainerMock->expects($this->once())
143  ->method('getCustomerEmail')
144  ->will($this->returnValue($customerEmail));
145  $this->identityContainerMock->expects($this->once())
146  ->method('getCustomerName')
147  ->will($this->returnValue($customerName));
148  $this->identityContainerMock->expects($this->once())
149  ->method('getStore')
150  ->willReturn($this->storeMock);
151  $this->storeMock->expects($this->once())
152  ->method('getId')
153  ->willReturn(1);
154  $this->transportBuilder->expects($this->once())
155  ->method('addTo')
156  ->with($this->equalTo($customerEmail), $this->equalTo($customerName));
157 
158  $this->transportBuilder->expects($this->once())
159  ->method('getTransport')
160  ->will($this->returnValue($transportMock));
161 
162  $this->senderBuilder->send();
163  }
164 
165  public function testSendCopyTo()
166  {
167  $transportMock = $this->createMock(
168  \Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
169  );
170  $this->identityContainerMock->expects($this->once())
171  ->method('getCopyMethod')
172  ->will($this->returnValue('copy'));
173  $this->identityContainerMock->expects($this->never())
174  ->method('getCustomerEmail');
175  $this->identityContainerMock->expects($this->never())
176  ->method('getCustomerName');
177  $this->transportBuilder->expects($this->once())
178  ->method('addTo')
179  ->with($this->equalTo('[email protected]'));
180  $this->identityContainerMock->expects($this->once())
181  ->method('getStore')
182  ->willReturn($this->storeMock);
183  $this->storeMock->expects($this->once())
184  ->method('getId')
185  ->willReturn(1);
186  $this->transportBuilder->expects($this->once())
187  ->method('getTransport')
188  ->will($this->returnValue($transportMock));
189 
190  $this->senderBuilder->sendCopyTo();
191  }
192 }
$templateId
Definition: queue.php:15