Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EmailSenderTest.php
Go to the documentation of this file.
1 <?php
7 
14 class EmailSenderTest extends \PHPUnit\Framework\TestCase
15 {
19  private $subject;
20 
24  private $orderMock;
25 
29  private $storeMock;
30 
34  private $senderMock;
35 
39  private $loggerMock;
40 
44  private $shipmentMock;
45 
49  private $commentMock;
50 
54  private $addressMock;
55 
59  private $globalConfigMock;
60 
64  private $eventManagerMock;
65 
69  private $paymentInfoMock;
70 
74  private $paymentHelperMock;
75 
79  private $shipmentResourceMock;
80 
84  private $addressRendererMock;
85 
89  private $templateContainerMock;
90 
94  private $identityContainerMock;
95 
99  private $senderBuilderFactoryMock;
100 
104  protected function setUp()
105  {
106  $this->orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109 
110  $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
111  ->setMethods(['getStoreId'])
112  ->disableOriginalConstructor()
113  ->getMock();
114 
115  $this->storeMock->expects($this->any())
116  ->method('getStoreId')
117  ->willReturn(1);
118  $this->orderMock->expects($this->any())
119  ->method('getStore')
120  ->willReturn($this->storeMock);
121 
122  $this->senderMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Email\Sender::class)
123  ->disableOriginalConstructor()
124  ->setMethods(['send', 'sendCopyTo'])
125  ->getMock();
126 
127  $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
128  ->disableOriginalConstructor()
129  ->getMockForAbstractClass();
130 
131  $this->shipmentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class)
132  ->disableOriginalConstructor()
133  ->setMethods(['setSendEmail', 'setEmailSent'])
134  ->getMock();
135 
136  $this->commentMock = $this->getMockBuilder(\Magento\Sales\Api\Data\ShipmentCommentCreationInterface::class)
137  ->disableOriginalConstructor()
138  ->getMockForAbstractClass();
139 
140  $this->commentMock->expects($this->any())
141  ->method('getComment')
142  ->willReturn('Comment text');
143 
144  $this->addressMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Address::class)
145  ->disableOriginalConstructor()
146  ->getMock();
147 
148  $this->orderMock->expects($this->any())
149  ->method('getBillingAddress')
150  ->willReturn($this->addressMock);
151  $this->orderMock->expects($this->any())
152  ->method('getShippingAddress')
153  ->willReturn($this->addressMock);
154 
155  $this->globalConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
156  ->disableOriginalConstructor()
157  ->getMockForAbstractClass();
158 
159  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
160  ->disableOriginalConstructor()
161  ->getMockForAbstractClass();
162 
163  $this->paymentInfoMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
164  ->disableOriginalConstructor()
165  ->getMock();
166 
167  $this->orderMock->expects($this->any())
168  ->method('getPayment')
169  ->willReturn($this->paymentInfoMock);
170 
171  $this->paymentHelperMock = $this->getMockBuilder(\Magento\Payment\Helper\Data::class)
172  ->disableOriginalConstructor()
173  ->getMock();
174 
175  $this->paymentHelperMock->expects($this->any())
176  ->method('getInfoBlockHtml')
177  ->with($this->paymentInfoMock, 1)
178  ->willReturn('Payment Info Block');
179 
180  $this->shipmentResourceMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Shipment::class)
181  ->disableOriginalConstructor()
182  ->getMock();
183 
184  $this->addressRendererMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Address\Renderer::class)
185  ->disableOriginalConstructor()
186  ->getMock();
187 
188  $this->addressRendererMock->expects($this->any())
189  ->method('format')
190  ->with($this->addressMock, 'html')
191  ->willReturn('Formatted address');
192 
193  $this->templateContainerMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Email\Container\Template::class)
194  ->disableOriginalConstructor()
195  ->getMock();
196 
197  $this->identityContainerMock = $this->getMockBuilder(
198  \Magento\Sales\Model\Order\Email\Container\ShipmentIdentity::class
199  )
200  ->disableOriginalConstructor()
201  ->getMock();
202 
203  $this->identityContainerMock->expects($this->any())
204  ->method('getStore')
205  ->willReturn($this->storeMock);
206 
207  $this->senderBuilderFactoryMock = $this->getMockBuilder(
208  \Magento\Sales\Model\Order\Email\SenderBuilderFactory::class
209  )
210  ->disableOriginalConstructor()
211  ->setMethods(['create'])
212  ->getMock();
213 
214  $this->subject = new \Magento\Sales\Model\Order\Shipment\Sender\EmailSender(
215  $this->templateContainerMock,
216  $this->identityContainerMock,
217  $this->senderBuilderFactoryMock,
218  $this->loggerMock,
219  $this->addressRendererMock,
220  $this->paymentHelperMock,
221  $this->shipmentResourceMock,
222  $this->globalConfigMock,
223  $this->eventManagerMock
224  );
225  }
226 
239  public function testSend($configValue, $forceSyncMode, $isComment, $emailSendingResult)
240  {
241  $this->globalConfigMock->expects($this->once())
242  ->method('getValue')
243  ->with('sales_email/general/async_sending')
244  ->willReturn($configValue);
245 
246  if (!$isComment) {
247  $this->commentMock = null;
248  }
249 
250  $this->shipmentMock->expects($this->once())
251  ->method('setSendEmail')
252  ->with(true);
253 
254  if (!$configValue || $forceSyncMode) {
255  $transport = [
256  'order' => $this->orderMock,
257  'shipment' => $this->shipmentMock,
258  'comment' => $isComment ? 'Comment text' : '',
259  'billing' => $this->addressMock,
260  'payment_html' => 'Payment Info Block',
261  'store' => $this->storeMock,
262  'formattedShippingAddress' => 'Formatted address',
263  'formattedBillingAddress' => 'Formatted address',
264  ];
265  $transport = new \Magento\Framework\DataObject($transport);
266 
267  $this->eventManagerMock->expects($this->once())
268  ->method('dispatch')
269  ->with(
270  'email_shipment_set_template_vars_before',
271  [
272  'sender' => $this->subject,
273  'transport' => $transport->getData(),
274  'transportObject' => $transport,
275  ]
276  );
277 
278  $this->templateContainerMock->expects($this->once())
279  ->method('setTemplateVars')
280  ->with($transport->getData());
281 
282  $this->identityContainerMock->expects($this->once())
283  ->method('isEnabled')
284  ->willReturn($emailSendingResult);
285 
286  if ($emailSendingResult) {
287  $this->senderBuilderFactoryMock->expects($this->once())
288  ->method('create')
289  ->willReturn($this->senderMock);
290 
291  $this->senderMock->expects($this->once())
292  ->method('send');
293 
294  $this->senderMock->expects($this->once())
295  ->method('sendCopyTo');
296 
297  $this->shipmentMock->expects($this->once())
298  ->method('setEmailSent')
299  ->with(true);
300 
301  $this->shipmentResourceMock->expects($this->once())
302  ->method('saveAttribute')
303  ->with($this->shipmentMock, ['send_email', 'email_sent']);
304 
305  $this->assertTrue(
306  $this->subject->send(
307  $this->orderMock,
308  $this->shipmentMock,
309  $this->commentMock,
310  $forceSyncMode
311  )
312  );
313  } else {
314  $this->shipmentResourceMock->expects($this->once())
315  ->method('saveAttribute')
316  ->with($this->shipmentMock, 'send_email');
317 
318  $this->assertFalse(
319  $this->subject->send(
320  $this->orderMock,
321  $this->shipmentMock,
322  $this->commentMock,
323  $forceSyncMode
324  )
325  );
326  }
327  } else {
328  $this->shipmentMock->expects($this->once())
329  ->method('setEmailSent')
330  ->with(null);
331 
332  $this->shipmentResourceMock->expects($this->at(0))
333  ->method('saveAttribute')
334  ->with($this->shipmentMock, 'email_sent');
335  $this->shipmentResourceMock->expects($this->at(1))
336  ->method('saveAttribute')
337  ->with($this->shipmentMock, 'send_email');
338 
339  $this->assertFalse(
340  $this->subject->send(
341  $this->orderMock,
342  $this->shipmentMock,
343  $this->commentMock,
344  $forceSyncMode
345  )
346  );
347  }
348  }
349 
353  public function sendDataProvider()
354  {
355  return [
356  'Successful sync sending with comment' => [0, false, true, true],
357  'Successful sync sending without comment' => [0, false, false, true],
358  'Failed sync sending with comment' => [0, false, true, false],
359  'Successful forced sync sending with comment' => [1, true, true, true],
360  'Async sending' => [1, false, false, false],
361  ];
362  }
363 }
testSend($configValue, $forceSyncMode, $isComment, $emailSendingResult)