28 use Psr\Log\LoggerInterface;
40 private $resourceConnectionMock;
45 private $orderRepositoryMock;
50 private $invoiceRepositoryMock;
55 private $creditmemoDocumentFactoryMock;
60 private $refundAdapterMock;
65 private $orderStateResolverMock;
75 private $creditmemoRepositoryMock;
80 private $notifierMock;
85 private $refundInvoice;
90 private $creditmemoCommentCreationMock;
95 private $creditmemoCreationArgumentsMock;
105 private $invoiceMock;
110 private $creditmemoMock;
115 private $adapterInterface;
120 private $creditmemoItemCreationMock;
125 private $refundInvoiceValidatorMock;
130 private $validationMessagesMock;
139 $this->resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class)
140 ->disableOriginalConstructor()
142 $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class)
143 ->disableOriginalConstructor()
145 $this->invoiceRepositoryMock = $this->getMockBuilder(InvoiceRepositoryInterface::class)
146 ->disableOriginalConstructor()
148 $this->creditmemoDocumentFactoryMock = $this->getMockBuilder(CreditmemoDocumentFactory::class)
149 ->disableOriginalConstructor()
151 $this->refundAdapterMock = $this->getMockBuilder(RefundAdapterInterface::class)
152 ->disableOriginalConstructor()
154 $this->refundInvoiceValidatorMock = $this->getMockBuilder(RefundInvoiceInterface::class)
155 ->disableOriginalConstructor()
157 $this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class)
158 ->disableOriginalConstructor()
159 ->getMockForAbstractClass();
160 $this->configMock = $this->getMockBuilder(OrderConfig::class)
161 ->disableOriginalConstructor()
164 $this->creditmemoRepositoryMock = $this->getMockBuilder(CreditmemoRepositoryInterface::class)
165 ->disableOriginalConstructor()
166 ->getMockForAbstractClass();
168 $this->notifierMock = $this->getMockBuilder(NotifierInterface::class)
169 ->disableOriginalConstructor()
170 ->getMockForAbstractClass();
172 $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
173 ->disableOriginalConstructor()
174 ->getMockForAbstractClass();
176 $this->creditmemoCommentCreationMock = $this->getMockBuilder(CreditmemoCommentCreationInterface::class)
177 ->disableOriginalConstructor()
178 ->getMockForAbstractClass();
180 $this->creditmemoCreationArgumentsMock = $this->getMockBuilder(CreditmemoCreationArgumentsInterface::class)
181 ->disableOriginalConstructor()
182 ->getMockForAbstractClass();
184 $this->orderMock = $this->getMockBuilder(OrderInterface::class)
185 ->disableOriginalConstructor()
186 ->getMockForAbstractClass();
188 $this->invoiceMock = $this->getMockBuilder(InvoiceInterface::class)
189 ->disableOriginalConstructor()
190 ->getMockForAbstractClass();
192 $this->creditmemoMock = $this->getMockBuilder(CreditmemoInterface::class)
193 ->disableOriginalConstructor()
194 ->getMockForAbstractClass();
196 $this->adapterInterface = $this->getMockBuilder(AdapterInterface::class)
197 ->disableOriginalConstructor()
198 ->getMockForAbstractClass();
200 $this->creditmemoItemCreationMock = $this->getMockBuilder(CreditmemoItemCreationInterface::class)
201 ->disableOriginalConstructor()
202 ->getMockForAbstractClass();
203 $this->validationMessagesMock = $this->getMockBuilder(ValidatorResultInterface::class)
204 ->disableOriginalConstructor()
205 ->setMethods([
'hasMessages',
'getMessages',
'addMessage'])
209 $this->resourceConnectionMock,
210 $this->orderStateResolverMock,
211 $this->orderRepositoryMock,
212 $this->invoiceRepositoryMock,
213 $this->refundInvoiceValidatorMock,
214 $this->creditmemoRepositoryMock,
215 $this->refundAdapterMock,
216 $this->creditmemoDocumentFactoryMock,
235 $this->resourceConnectionMock->expects($this->once())
236 ->method(
'getConnection')
238 ->willReturn($this->adapterInterface);
239 $this->invoiceRepositoryMock->expects($this->once())
241 ->willReturn($this->invoiceMock);
242 $this->orderRepositoryMock->expects($this->once())
244 ->willReturn($this->orderMock);
245 $this->creditmemoDocumentFactoryMock->expects($this->once())
246 ->method(
'createFromInvoice')
250 $this->creditmemoCommentCreationMock,
251 ($appendComment && $notify),
252 $this->creditmemoCreationArgumentsMock
253 )->willReturn($this->creditmemoMock);
254 $this->refundInvoiceValidatorMock->expects($this->once())
259 $this->creditmemoMock,
264 $this->creditmemoCommentCreationMock,
265 $this->creditmemoCreationArgumentsMock
267 ->willReturn($this->validationMessagesMock);
268 $hasMessages =
false;
269 $this->validationMessagesMock->expects($this->once())
270 ->method(
'hasMessages')->willReturn($hasMessages);
271 $this->refundAdapterMock->expects($this->once())
273 ->with($this->creditmemoMock, $this->orderMock)
274 ->willReturn($this->orderMock);
275 $this->orderStateResolverMock->expects($this->once())
276 ->method(
'getStateForOrder')
277 ->with($this->orderMock, [])
279 $this->orderMock->expects($this->once())
283 $this->orderMock->expects($this->once())
286 $this->configMock->expects($this->once())
287 ->method(
'getStateDefaultStatus')
289 ->willReturn(
'Closed');
290 $this->orderMock->expects($this->once())
291 ->method(
'setStatus')
294 $this->creditmemoMock->expects($this->once())
296 ->with(\
Magento\Sales\Model\
Order\Creditmemo::STATE_REFUNDED)
298 $this->creditmemoRepositoryMock->expects($this->once())
300 ->with($this->creditmemoMock)
301 ->willReturn($this->creditmemoMock);
302 $this->orderRepositoryMock->expects($this->once())
304 ->with($this->orderMock)
305 ->willReturn($this->orderMock);
307 $this->notifierMock->expects($this->once())
309 ->with($this->orderMock, $this->creditmemoMock, $this->creditmemoCommentCreationMock);
311 $this->creditmemoMock->expects($this->once())
312 ->method(
'getEntityId')
317 $this->refundInvoice->execute(
323 $this->creditmemoCommentCreationMock,
324 $this->creditmemoCreationArgumentsMock
335 $items = [1 => $this->creditmemoItemCreationMock];
337 $appendComment =
true;
339 $errorMessages = [
'error1',
'error2'];
341 $this->invoiceRepositoryMock->expects($this->once())
343 ->willReturn($this->invoiceMock);
344 $this->orderRepositoryMock->expects($this->once())
346 ->willReturn($this->orderMock);
348 $this->creditmemoDocumentFactoryMock->expects($this->once())
349 ->method(
'createFromInvoice')
353 $this->creditmemoCommentCreationMock,
354 ($appendComment && $notify),
355 $this->creditmemoCreationArgumentsMock
356 )->willReturn($this->creditmemoMock);
358 $this->refundInvoiceValidatorMock->expects($this->once())
363 $this->creditmemoMock,
368 $this->creditmemoCommentCreationMock,
369 $this->creditmemoCreationArgumentsMock
371 ->willReturn($this->validationMessagesMock);
373 $this->validationMessagesMock->expects($this->once())
374 ->method(
'hasMessages')->willReturn($hasMessages);
375 $this->validationMessagesMock->expects($this->once())
376 ->method(
'getMessages')->willReturn($errorMessages);
380 $this->refundInvoice->execute(
386 $this->creditmemoCommentCreationMock,
387 $this->creditmemoCreationArgumentsMock
398 $items = [1 => $this->creditmemoItemCreationMock];
400 $appendComment =
true;
402 $this->resourceConnectionMock->expects($this->once())
403 ->method(
'getConnection')
405 ->willReturn($this->adapterInterface);
407 $this->invoiceRepositoryMock->expects($this->once())
409 ->willReturn($this->invoiceMock);
410 $this->orderRepositoryMock->expects($this->once())
412 ->willReturn($this->orderMock);
414 $this->creditmemoDocumentFactoryMock->expects($this->once())
415 ->method(
'createFromInvoice')
419 $this->creditmemoCommentCreationMock,
420 ($appendComment && $notify),
421 $this->creditmemoCreationArgumentsMock
422 )->willReturn($this->creditmemoMock);
424 $this->refundInvoiceValidatorMock->expects($this->once())
429 $this->creditmemoMock,
434 $this->creditmemoCommentCreationMock,
435 $this->creditmemoCreationArgumentsMock
437 ->willReturn($this->validationMessagesMock);
438 $hasMessages =
false;
439 $this->validationMessagesMock->expects($this->once())
440 ->method(
'hasMessages')->willReturn($hasMessages);
441 $e = new \Exception();
443 $this->refundAdapterMock->expects($this->once())
445 ->with($this->creditmemoMock, $this->orderMock)
446 ->willThrowException($e);
448 $this->loggerMock->expects($this->once())
452 $this->adapterInterface->expects($this->once())
453 ->method(
'rollBack');
455 $this->refundInvoice->execute(
461 $this->creditmemoCommentCreationMock,
462 $this->creditmemoCreationArgumentsMock
471 $creditmemoItemCreationMock = $this->getMockBuilder(CreditmemoItemCreationInterface::class)
472 ->disableOriginalConstructor()
473 ->getMockForAbstractClass();
476 'TestWithNotifyTrue' => [1,
true, [1 => $creditmemoItemCreationMock],
true,
true],
477 'TestWithNotifyFalse' => [1,
true, [1 => $creditmemoItemCreationMock],
false,
true],
testOrderCreditmemo($invoiceId, $isOnline, $items, $notify, $appendComment)
testDocumentValidationException()
testCouldNotCreditmemoException()