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