SenderBuilderTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Unit\Model\Order\Email;
  7. use Magento\Sales\Model\Order\Email\SenderBuilder;
  8. class SenderBuilderTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var SenderBuilder
  12. */
  13. protected $senderBuilder;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $templateContainerMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $identityContainerMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $transportBuilder;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $storeMock;
  30. protected function setUp()
  31. {
  32. $templateId = 'test_template_id';
  33. $templateOptions = ['option1', 'option2'];
  34. $templateVars = ['var1', 'var2'];
  35. $emailIdentity = 'email_identity_test';
  36. $emailCopyTo = ['example@mail.com'];
  37. $this->templateContainerMock = $this->createPartialMock(
  38. \Magento\Sales\Model\Order\Email\Container\Template::class,
  39. ['getTemplateVars', 'getTemplateOptions', 'getTemplateId']
  40. );
  41. $this->storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, [
  42. 'getStoreId',
  43. '__wakeup',
  44. 'getId',
  45. ]);
  46. $this->identityContainerMock = $this->createPartialMock(
  47. \Magento\Sales\Model\Order\Email\Container\ShipmentIdentity::class,
  48. [
  49. 'getEmailIdentity',
  50. 'getCustomerEmail',
  51. 'getCustomerName',
  52. 'getTemplateOptions',
  53. 'getEmailCopyTo',
  54. 'getCopyMethod',
  55. 'getStore',
  56. ]
  57. );
  58. $this->transportBuilder = $this->createPartialMock(
  59. \Magento\Framework\Mail\Template\TransportBuilder::class,
  60. [
  61. 'addTo',
  62. 'addBcc',
  63. 'getTransport',
  64. 'setTemplateIdentifier',
  65. 'setTemplateOptions',
  66. 'setTemplateVars',
  67. 'setFromByScope',
  68. ]
  69. );
  70. $this->templateContainerMock->expects($this->once())
  71. ->method('getTemplateId')
  72. ->will($this->returnValue($templateId));
  73. $this->transportBuilder->expects($this->once())
  74. ->method('setTemplateIdentifier')
  75. ->with($this->equalTo($templateId));
  76. $this->templateContainerMock->expects($this->once())
  77. ->method('getTemplateOptions')
  78. ->will($this->returnValue($templateOptions));
  79. $this->transportBuilder->expects($this->once())
  80. ->method('setTemplateOptions')
  81. ->with($this->equalTo($templateOptions));
  82. $this->templateContainerMock->expects($this->once())
  83. ->method('getTemplateVars')
  84. ->will($this->returnValue($templateVars));
  85. $this->transportBuilder->expects($this->once())
  86. ->method('setTemplateVars')
  87. ->with($this->equalTo($templateVars));
  88. $this->identityContainerMock->expects($this->once())
  89. ->method('getEmailIdentity')
  90. ->will($this->returnValue($emailIdentity));
  91. $this->transportBuilder->expects($this->once())
  92. ->method('setFromByScope')
  93. ->with($this->equalTo($emailIdentity), 1);
  94. $this->identityContainerMock->expects($this->once())
  95. ->method('getEmailCopyTo')
  96. ->will($this->returnValue($emailCopyTo));
  97. $this->senderBuilder = new SenderBuilder(
  98. $this->templateContainerMock,
  99. $this->identityContainerMock,
  100. $this->transportBuilder
  101. );
  102. }
  103. public function testSend()
  104. {
  105. $customerName = 'test_name';
  106. $customerEmail = 'test_email';
  107. $identity = 'email_identity_test';
  108. $transportMock = $this->createMock(
  109. \Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
  110. );
  111. $this->identityContainerMock->expects($this->once())
  112. ->method('getEmailCopyTo')
  113. ->will($this->returnValue(['example@mail.com']));
  114. $this->identityContainerMock->expects($this->once())
  115. ->method('getCopyMethod')
  116. ->will($this->returnValue('bcc'));
  117. $this->identityContainerMock->expects($this->once())
  118. ->method('getCustomerEmail')
  119. ->will($this->returnValue($customerEmail));
  120. $this->identityContainerMock->expects($this->once())
  121. ->method('getCustomerName')
  122. ->will($this->returnValue($customerName));
  123. $this->identityContainerMock->expects($this->once())
  124. ->method('getStore')
  125. ->willReturn($this->storeMock);
  126. $this->storeMock->expects($this->once())
  127. ->method('getId')
  128. ->willReturn(1);
  129. $this->transportBuilder->expects($this->once())
  130. ->method('setFromByScope')
  131. ->with($identity, 1);
  132. $this->transportBuilder->expects($this->once())
  133. ->method('addTo')
  134. ->with($this->equalTo($customerEmail), $this->equalTo($customerName));
  135. $this->transportBuilder->expects($this->once())
  136. ->method('getTransport')
  137. ->will($this->returnValue($transportMock));
  138. $this->senderBuilder->send();
  139. }
  140. public function testSendCopyTo()
  141. {
  142. $identity = 'email_identity_test';
  143. $transportMock = $this->createMock(
  144. \Magento\Sales\Test\Unit\Model\Order\Email\Stub\TransportInterfaceMock::class
  145. );
  146. $this->identityContainerMock->expects($this->once())
  147. ->method('getCopyMethod')
  148. ->will($this->returnValue('copy'));
  149. $this->identityContainerMock->expects($this->never())
  150. ->method('getCustomerEmail');
  151. $this->identityContainerMock->expects($this->never())
  152. ->method('getCustomerName');
  153. $this->transportBuilder->expects($this->once())
  154. ->method('addTo')
  155. ->with($this->equalTo('example@mail.com'));
  156. $this->transportBuilder->expects($this->once())
  157. ->method('setFromByScope')
  158. ->with($identity, 1);
  159. $this->identityContainerMock->expects($this->once())
  160. ->method('getStore')
  161. ->willReturn($this->storeMock);
  162. $this->storeMock->expects($this->once())
  163. ->method('getId')
  164. ->willReturn(1);
  165. $this->transportBuilder->expects($this->once())
  166. ->method('getTransport')
  167. ->will($this->returnValue($transportMock));
  168. $this->senderBuilder->sendCopyTo();
  169. }
  170. }