Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EmailSenderHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class EmailSenderHandlerTest extends \PHPUnit\Framework\TestCase
14 {
20  protected $object;
21 
27  protected $emailSender;
28 
34  protected $entityResource;
35 
41  protected $entityCollection;
42 
48  protected $globalConfig;
49 
53  private $identityContainerMock;
54 
58  private $storeManagerMock;
59 
60  protected function setUp()
61  {
62  $objectManager = new ObjectManager($this);
63 
64  $this->emailSender = $this->createPartialMock(\Magento\Sales\Model\Order\Email\Sender::class, ['send']);
65 
66  $this->entityResource = $this->getMockForAbstractClass(
67  \Magento\Sales\Model\ResourceModel\EntityAbstract::class,
68  [],
69  '',
70  false,
71  false,
72  true,
73  ['save']
74  );
75 
76  $this->entityCollection = $this->getMockForAbstractClass(
77  \Magento\Sales\Model\ResourceModel\Collection\AbstractCollection::class,
78  [],
79  '',
80  false,
81  false,
82  true,
83  ['addFieldToFilter', 'getItems', 'addAttributeToSelect', 'getSelect']
84  );
85 
86  $this->globalConfig = $this->createMock(\Magento\Framework\App\Config::class);
87 
88  $this->identityContainerMock = $this->createMock(
89  \Magento\Sales\Model\Order\Email\Container\IdentityInterface::class
90  );
91 
92  $this->storeManagerMock = $this->createMock(
93  \Magento\Store\Model\StoreManagerInterface::class
94  );
95 
96  $this->object = $objectManager->getObject(
97  \Magento\Sales\Model\EmailSenderHandler::class,
98  [
99  'emailSender' => $this->emailSender,
100  'entityResource' => $this->entityResource,
101  'entityCollection' => $this->entityCollection,
102  'globalConfig' => $this->globalConfig,
103  'identityContainer' => $this->identityContainerMock,
104  'storeManager' => $this->storeManagerMock,
105  ]
106  );
107  }
108 
116  public function testExecute($configValue, $collectionItems, $emailSendingResult)
117  {
118  $path = 'sales_email/general/async_sending';
119 
120  $this->globalConfig
121  ->expects($this->at(0))
122  ->method('getValue')
123  ->with($path)
124  ->willReturn($configValue);
125 
126  if ($configValue) {
127  $this->entityCollection
128  ->expects($this->at(0))
129  ->method('addFieldToFilter')
130  ->with('send_email', ['eq' => 1]);
131 
132  $this->entityCollection
133  ->expects($this->at(1))
134  ->method('addFieldToFilter')
135  ->with('email_sent', ['null' => true]);
136 
137  $this->entityCollection
138  ->expects($this->any())
139  ->method('addAttributeToSelect')
140  ->with('store_id')
141  ->willReturnSelf();
142 
143  $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
144 
145  $selectMock
146  ->expects($this->atLeastOnce())
147  ->method('group')
148  ->with('store_id')
149  ->willReturnSelf();
150 
151  $this->entityCollection
152  ->expects($this->any())
153  ->method('getSelect')
154  ->willReturn($selectMock);
155 
156  $this->entityCollection
157  ->expects($this->any())
158  ->method('getItems')
159  ->willReturn($collectionItems);
160 
161  if ($collectionItems) {
162 
164  $collectionItem = $collectionItems[0];
165 
166  $this->emailSender
167  ->expects($this->once())
168  ->method('send')
169  ->with($collectionItem, true)
170  ->willReturn($emailSendingResult);
171 
172  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
173 
174  $this->storeManagerMock
175  ->expects($this->any())
176  ->method('getStore')
177  ->willReturn($storeMock);
178 
179  $this->identityContainerMock
180  ->expects($this->any())
181  ->method('setStore')
182  ->with($storeMock);
183 
184  $this->identityContainerMock
185  ->expects($this->any())
186  ->method('isEnabled')
187  ->willReturn(true);
188 
189  if ($emailSendingResult) {
190  $collectionItem
191  ->expects($this->once())
192  ->method('setEmailSent')
193  ->with(true)
194  ->willReturn($collectionItem);
195 
196  $this->entityResource
197  ->expects($this->once())
198  ->method('save')
199  ->with($collectionItem);
200  }
201  }
202  }
203 
204  $this->object->sendEmails();
205  }
206 
210  public function executeDataProvider()
211  {
212  $entityModel = $this->getMockForAbstractClass(
213  \Magento\Sales\Model\AbstractModel::class,
214  [],
215  '',
216  false,
217  false,
218  true,
219  ['setEmailSent', 'getOrder']
220  );
221 
222  return [
223  [
224  'configValue' => 1,
225  'collectionItems' => [clone $entityModel],
226  'emailSendingResult' => true,
227  ],
228  [
229  'configValue' => 1,
230  'collectionItems' => [clone $entityModel],
231  'emailSendingResult' => false,
232  ],
233  [
234  'configValue' => 1,
235  'collectionItems' => [],
236  'emailSendingResult' => null,
237  ],
238  [
239  'configValue' => 0,
240  'collectionItems' => null,
241  'emailSendingResult' => null,
242  ]
243  ];
244  }
245 }
$objectManager
Definition: bootstrap.php:17