EmailSenderHandlerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. /**
  9. * Unit test of sales emails sending observer.
  10. */
  11. class EmailSenderHandlerTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * Subject of testing.
  15. *
  16. * @var \Magento\Sales\Model\EmailSenderHandler
  17. */
  18. protected $object;
  19. /**
  20. * Email sender model mock.
  21. *
  22. * @var \Magento\Sales\Model\Order\Email\Sender|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $emailSender;
  25. /**
  26. * Entity resource model mock.
  27. *
  28. * @var \Magento\Sales\Model\ResourceModel\EntityAbstract|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $entityResource;
  31. /**
  32. * Entity collection model mock.
  33. *
  34. * @var \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $entityCollection;
  37. /**
  38. * Global configuration storage mock.
  39. *
  40. * @var \Magento\Framework\App\Config|\PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $globalConfig;
  43. /**
  44. * @var \Magento\Sales\Model\Order\Email\Container\IdentityInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $identityContainerMock;
  47. /**
  48. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  49. */
  50. private $storeManagerMock;
  51. protected function setUp()
  52. {
  53. $objectManager = new ObjectManager($this);
  54. $this->emailSender = $this->createPartialMock(\Magento\Sales\Model\Order\Email\Sender::class, ['send']);
  55. $this->entityResource = $this->getMockForAbstractClass(
  56. \Magento\Sales\Model\ResourceModel\EntityAbstract::class,
  57. [],
  58. '',
  59. false,
  60. false,
  61. true,
  62. ['save']
  63. );
  64. $this->entityCollection = $this->getMockForAbstractClass(
  65. \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection::class,
  66. [],
  67. '',
  68. false,
  69. false,
  70. true,
  71. ['addFieldToFilter', 'getItems', 'addAttributeToSelect', 'getSelect']
  72. );
  73. $this->globalConfig = $this->createMock(\Magento\Framework\App\Config::class);
  74. $this->identityContainerMock = $this->createMock(
  75. \Magento\Sales\Model\Order\Email\Container\IdentityInterface::class
  76. );
  77. $this->storeManagerMock = $this->createMock(
  78. \Magento\Store\Model\StoreManagerInterface::class
  79. );
  80. $this->object = $objectManager->getObject(
  81. \Magento\Sales\Model\EmailSenderHandler::class,
  82. [
  83. 'emailSender' => $this->emailSender,
  84. 'entityResource' => $this->entityResource,
  85. 'entityCollection' => $this->entityCollection,
  86. 'globalConfig' => $this->globalConfig,
  87. 'identityContainer' => $this->identityContainerMock,
  88. 'storeManager' => $this->storeManagerMock,
  89. ]
  90. );
  91. }
  92. /**
  93. * @param int $configValue
  94. * @param array|null $collectionItems
  95. * @param bool|null $emailSendingResult
  96. * @dataProvider executeDataProvider
  97. * @return void
  98. */
  99. public function testExecute($configValue, $collectionItems, $emailSendingResult)
  100. {
  101. $path = 'sales_email/general/async_sending';
  102. $this->globalConfig
  103. ->expects($this->at(0))
  104. ->method('getValue')
  105. ->with($path)
  106. ->willReturn($configValue);
  107. if ($configValue) {
  108. $this->entityCollection
  109. ->expects($this->at(0))
  110. ->method('addFieldToFilter')
  111. ->with('send_email', ['eq' => 1]);
  112. $this->entityCollection
  113. ->expects($this->at(1))
  114. ->method('addFieldToFilter')
  115. ->with('email_sent', ['null' => true]);
  116. $this->entityCollection
  117. ->expects($this->any())
  118. ->method('addAttributeToSelect')
  119. ->with('store_id')
  120. ->willReturnSelf();
  121. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  122. $selectMock
  123. ->expects($this->atLeastOnce())
  124. ->method('group')
  125. ->with('store_id')
  126. ->willReturnSelf();
  127. $this->entityCollection
  128. ->expects($this->any())
  129. ->method('getSelect')
  130. ->willReturn($selectMock);
  131. $this->entityCollection
  132. ->expects($this->any())
  133. ->method('getItems')
  134. ->willReturn($collectionItems);
  135. if ($collectionItems) {
  136. /** @var \Magento\Sales\Model\AbstractModel|\PHPUnit_Framework_MockObject_MockObject $collectionItem */
  137. $collectionItem = $collectionItems[0];
  138. $this->emailSender
  139. ->expects($this->once())
  140. ->method('send')
  141. ->with($collectionItem, true)
  142. ->willReturn($emailSendingResult);
  143. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  144. $this->storeManagerMock
  145. ->expects($this->any())
  146. ->method('getStore')
  147. ->willReturn($storeMock);
  148. $this->identityContainerMock
  149. ->expects($this->any())
  150. ->method('setStore')
  151. ->with($storeMock);
  152. $this->identityContainerMock
  153. ->expects($this->any())
  154. ->method('isEnabled')
  155. ->willReturn(true);
  156. if ($emailSendingResult) {
  157. $collectionItem
  158. ->expects($this->once())
  159. ->method('setEmailSent')
  160. ->with(true)
  161. ->willReturn($collectionItem);
  162. $this->entityResource
  163. ->expects($this->once())
  164. ->method('save')
  165. ->with($collectionItem);
  166. }
  167. }
  168. }
  169. $this->object->sendEmails();
  170. }
  171. /**
  172. * @return array
  173. */
  174. public function executeDataProvider()
  175. {
  176. $entityModel = $this->getMockForAbstractClass(
  177. \Magento\Sales\Model\AbstractModel::class,
  178. [],
  179. '',
  180. false,
  181. false,
  182. true,
  183. ['setEmailSent', 'getOrder']
  184. );
  185. return [
  186. [
  187. 'configValue' => 1,
  188. 'collectionItems' => [clone $entityModel],
  189. 'emailSendingResult' => true,
  190. ],
  191. [
  192. 'configValue' => 1,
  193. 'collectionItems' => [clone $entityModel],
  194. 'emailSendingResult' => false,
  195. ],
  196. [
  197. 'configValue' => 1,
  198. 'collectionItems' => [],
  199. 'emailSendingResult' => null,
  200. ],
  201. [
  202. 'configValue' => 0,
  203. 'collectionItems' => null,
  204. 'emailSendingResult' => null,
  205. ]
  206. ];
  207. }
  208. }