Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InvoiceOrderTest.php
Go to the documentation of this file.
1 <?php
7 
16 use Magento\Sales\Model\Order\Config as OrderConfig;
25 use Psr\Log\LoggerInterface;
26 
33 class InvoiceOrderTest extends \PHPUnit\Framework\TestCase
34 {
38  private $resourceConnectionMock;
39 
43  private $orderRepositoryMock;
44 
48  private $invoiceDocumentFactoryMock;
49 
53  private $invoiceOrderValidatorMock;
54 
58  private $paymentAdapterMock;
59 
63  private $orderStateResolverMock;
64 
68  private $configMock;
69 
73  private $invoiceRepositoryMock;
74 
78  private $notifierInterfaceMock;
79 
83  private $invoiceOrder;
84 
88  private $invoiceCommentCreationMock;
89 
93  private $invoiceCreationArgumentsMock;
94 
98  private $orderMock;
99 
103  private $invoiceMock;
104 
108  private $adapterInterface;
109 
113  private $loggerMock;
114 
118  private $errorMessagesMock;
119 
120  protected function setUp()
121  {
122  $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
123  ->disableOriginalConstructor()
124  ->getMock();
125 
126  $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
127  ->disableOriginalConstructor()
128  ->getMock();
129 
130  $this->invoiceDocumentFactoryMock = $this->getMockBuilder(InvoiceDocumentFactory::class)
131  ->disableOriginalConstructor()
132  ->getMock();
133 
134  $this->paymentAdapterMock = $this->getMockBuilder(PaymentAdapterInterface::class)
135  ->disableOriginalConstructor()
136  ->getMock();
137 
138  $this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class)
139  ->disableOriginalConstructor()
140  ->getMock();
141 
142  $this->configMock = $this->getMockBuilder(OrderConfig::class)
143  ->disableOriginalConstructor()
144  ->getMock();
145 
146  $this->invoiceRepositoryMock = $this->getMockBuilder(InvoiceRepository::class)
147  ->disableOriginalConstructor()
148  ->getMock();
149 
150  $this->notifierInterfaceMock = $this->getMockBuilder(NotifierInterface::class)
151  ->disableOriginalConstructor()
152  ->getMock();
153 
154  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
155  ->disableOriginalConstructor()
156  ->getMock();
157 
158  $this->invoiceCommentCreationMock = $this->getMockBuilder(InvoiceCommentCreationInterface::class)
159  ->disableOriginalConstructor()
160  ->getMock();
161 
162  $this->invoiceCreationArgumentsMock = $this->getMockBuilder(InvoiceCreationArgumentsInterface::class)
163  ->disableOriginalConstructor()
164  ->getMock();
165 
166  $this->orderMock = $this->getMockBuilder(OrderInterface::class)
167  ->disableOriginalConstructor()
168  ->getMock();
169 
170  $this->invoiceMock = $this->getMockBuilder(InvoiceInterface::class)
171  ->disableOriginalConstructor()
172  ->getMock();
173 
174  $this->adapterInterface = $this->getMockBuilder(AdapterInterface::class)
175  ->disableOriginalConstructor()
176  ->getMock();
177 
178  $this->invoiceOrderValidatorMock = $this->getMockBuilder(InvoiceOrderInterface::class)
179  ->disableOriginalConstructor()
180  ->getMock();
181 
182  $this->errorMessagesMock = $this->getMockBuilder(ValidatorResultInterface::class)
183  ->disableOriginalConstructor()
184  ->setMethods(['hasMessages', 'getMessages', 'addMessage'])
185  ->getMock();
186 
187  $this->invoiceOrder = new InvoiceOrder(
188  $this->resourceConnectionMock,
189  $this->orderRepositoryMock,
190  $this->invoiceDocumentFactoryMock,
191  $this->paymentAdapterMock,
192  $this->orderStateResolverMock,
193  $this->configMock,
194  $this->invoiceRepositoryMock,
195  $this->invoiceOrderValidatorMock,
196  $this->notifierInterfaceMock,
197  $this->loggerMock
198  );
199  }
200 
211  public function testOrderInvoice($orderId, $capture, $items, $notify, $appendComment)
212  {
213  $this->resourceConnectionMock->expects($this->once())
214  ->method('getConnection')
215  ->with('sales')
216  ->willReturn($this->adapterInterface);
217  $this->orderRepositoryMock->expects($this->once())
218  ->method('get')
219  ->willReturn($this->orderMock);
220  $this->invoiceDocumentFactoryMock->expects($this->once())
221  ->method('create')
222  ->with(
223  $this->orderMock,
224  $items,
225  $this->invoiceCommentCreationMock,
226  ($appendComment && $notify),
227  $this->invoiceCreationArgumentsMock
228  )->willReturn($this->invoiceMock);
229  $this->invoiceOrderValidatorMock->expects($this->once())
230  ->method('validate')
231  ->with(
232  $this->orderMock,
233  $this->invoiceMock,
234  $capture,
235  $items,
236  $notify,
237  $appendComment,
238  $this->invoiceCommentCreationMock,
239  $this->invoiceCreationArgumentsMock
240  )
241  ->willReturn($this->errorMessagesMock);
242  $hasMessages = false;
243  $this->errorMessagesMock->expects($this->once())
244  ->method('hasMessages')->willReturn($hasMessages);
245  $this->paymentAdapterMock->expects($this->once())
246  ->method('pay')
247  ->with($this->orderMock, $this->invoiceMock, $capture)
248  ->willReturn($this->orderMock);
249  $this->orderStateResolverMock->expects($this->once())
250  ->method('getStateForOrder')
251  ->with($this->orderMock, [OrderStateResolverInterface::IN_PROGRESS])
252  ->willReturn(Order::STATE_PROCESSING);
253  $this->orderMock->expects($this->once())
254  ->method('setState')
256  ->willReturnSelf();
257  $this->orderMock->expects($this->once())
258  ->method('getState')
259  ->willReturn(Order::STATE_PROCESSING);
260  $this->configMock->expects($this->once())
261  ->method('getStateDefaultStatus')
263  ->willReturn('Processing');
264  $this->orderMock->expects($this->once())
265  ->method('setStatus')
266  ->with('Processing')
267  ->willReturnSelf();
268  $this->invoiceMock->expects($this->once())
269  ->method('setState')
270  ->with(\Magento\Sales\Model\Order\Invoice::STATE_PAID)
271  ->willReturnSelf();
272  $this->invoiceRepositoryMock->expects($this->once())
273  ->method('save')
274  ->with($this->invoiceMock)
275  ->willReturn($this->invoiceMock);
276  $this->orderRepositoryMock->expects($this->once())
277  ->method('save')
278  ->with($this->orderMock)
279  ->willReturn($this->orderMock);
280  if ($notify) {
281  $this->notifierInterfaceMock->expects($this->once())
282  ->method('notify')
283  ->with($this->orderMock, $this->invoiceMock, $this->invoiceCommentCreationMock);
284  }
285  $this->invoiceMock->expects($this->once())
286  ->method('getEntityId')
287  ->willReturn(2);
288 
289  $this->assertEquals(
290  2,
291  $this->invoiceOrder->execute(
292  $orderId,
293  $capture,
294  $items,
295  $notify,
296  $appendComment,
297  $this->invoiceCommentCreationMock,
298  $this->invoiceCreationArgumentsMock
299  )
300  );
301  }
302 
307  {
308  $orderId = 1;
309  $capture = true;
310  $items = [1 => 2];
311  $notify = true;
312  $appendComment = true;
313  $errorMessages = ['error1', 'error2'];
314 
315  $this->orderRepositoryMock->expects($this->once())
316  ->method('get')
317  ->willReturn($this->orderMock);
318 
319  $this->invoiceDocumentFactoryMock->expects($this->once())
320  ->method('create')
321  ->with(
322  $this->orderMock,
323  $items,
324  $this->invoiceCommentCreationMock,
325  ($appendComment && $notify),
326  $this->invoiceCreationArgumentsMock
327  )->willReturn($this->invoiceMock);
328 
329  $this->invoiceOrderValidatorMock->expects($this->once())
330  ->method('validate')
331  ->with(
332  $this->orderMock,
333  $this->invoiceMock,
334  $capture,
335  $items,
336  $notify,
337  $appendComment,
338  $this->invoiceCommentCreationMock,
339  $this->invoiceCreationArgumentsMock
340  )
341  ->willReturn($this->errorMessagesMock);
342  $hasMessages = true;
343 
344  $this->errorMessagesMock->expects($this->once())
345  ->method('hasMessages')->willReturn($hasMessages);
346  $this->errorMessagesMock->expects($this->once())
347  ->method('getMessages')->willReturn($errorMessages);
348 
349  $this->invoiceOrder->execute(
350  $orderId,
351  $capture,
352  $items,
353  $notify,
354  $appendComment,
355  $this->invoiceCommentCreationMock,
356  $this->invoiceCreationArgumentsMock
357  );
358  }
359 
364  {
365  $orderId = 1;
366  $items = [1 => 2];
367  $capture = true;
368  $notify = true;
369  $appendComment = true;
370  $this->resourceConnectionMock->expects($this->once())
371  ->method('getConnection')
372  ->with('sales')
373  ->willReturn($this->adapterInterface);
374 
375  $this->orderRepositoryMock->expects($this->once())
376  ->method('get')
377  ->willReturn($this->orderMock);
378 
379  $this->invoiceDocumentFactoryMock->expects($this->once())
380  ->method('create')
381  ->with(
382  $this->orderMock,
383  $items,
384  $this->invoiceCommentCreationMock,
385  ($appendComment && $notify),
386  $this->invoiceCreationArgumentsMock
387  )->willReturn($this->invoiceMock);
388 
389  $this->invoiceOrderValidatorMock->expects($this->once())
390  ->method('validate')
391  ->with(
392  $this->orderMock,
393  $this->invoiceMock,
394  $capture,
395  $items,
396  $notify,
397  $appendComment,
398  $this->invoiceCommentCreationMock,
399  $this->invoiceCreationArgumentsMock
400  )
401  ->willReturn($this->errorMessagesMock);
402 
403  $hasMessages = false;
404  $this->errorMessagesMock->expects($this->once())
405  ->method('hasMessages')->willReturn($hasMessages);
406 
407  $e = new \Exception();
408  $this->paymentAdapterMock->expects($this->once())
409  ->method('pay')
410  ->with($this->orderMock, $this->invoiceMock, $capture)
411  ->willThrowException($e);
412 
413  $this->loggerMock->expects($this->once())
414  ->method('critical')
415  ->with($e);
416 
417  $this->adapterInterface->expects($this->once())
418  ->method('rollBack');
419 
420  $this->invoiceOrder->execute(
421  $orderId,
422  $capture,
423  $items,
424  $notify,
425  $appendComment,
426  $this->invoiceCommentCreationMock,
427  $this->invoiceCreationArgumentsMock
428  );
429  }
430 
434  public function dataProvider()
435  {
436  return [
437  'TestWithNotifyTrue' => [1, true, [1 => 2], true, true],
438  'TestWithNotifyFalse' => [1, true, [1 => 2], false, true],
439  ];
440  }
441 }
testOrderInvoice($orderId, $capture, $items, $notify, $appendComment)
$items