public function testLoadByOrder()
 {
     $orderId = 1234;
     $invoiceId = 99;
     $qty = 1;
     $data = ['items' => [1 => ['qty' => $qty, 'back_to_stock' => true]]];
     $this->loader->setCreditmemoId(0);
     $this->loader->setOrderId($orderId);
     $this->loader->setCreditmemo($data);
     $this->loader->setInvoiceId($invoiceId);
     $orderMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->setMethods([])->getMock();
     $orderMock->expects($this->once())->method('load')->willReturnSelf();
     $orderMock->expects($this->once())->method('getId')->willReturn($orderId);
     $orderMock->expects($this->once())->method('canCreditmemo')->willReturn(true);
     $this->orderFactoryMock->expects($this->once())->method('create')->willReturn($orderMock);
     $invoiceMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Invoice')->disableOriginalConstructor()->getMock();
     $invoiceMock->expects($this->any())->method('setOrder')->willReturnSelf();
     $invoiceMock->expects($this->any())->method('getId')->willReturn(1);
     $this->invoiceRepositoryMock->expects($this->once())->method('get')->willReturn($invoiceMock);
     $creditmemoMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo')->disableOriginalConstructor()->setMethods([])->getMock();
     $orderItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Item')->disableOriginalConstructor()->setMethods([])->getMock();
     $creditmemoItemMock = $this->getMockBuilder('Magento\\Sales\\Model\\Order\\Creditmemo\\Item')->disableOriginalConstructor()->setMethods([])->getMock();
     $creditmemoItemMock->expects($this->any())->method('getOrderItem')->willReturn($orderItemMock);
     $items = [$creditmemoItemMock, $creditmemoItemMock, $creditmemoItemMock];
     $creditmemoMock->expects($this->any())->method('getAllItems')->willReturn($items);
     $data['qtys'] = [1 => $qty];
     $this->creditmemoFactoryMock->expects($this->any())->method('createByInvoice')->with($invoiceMock, $data)->willReturn($creditmemoMock);
     $this->assertEquals($creditmemoMock, $this->loader->load());
 }
 /**
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testRegisterRefundNotification()
 {
     $message = 'Registered notification about refunded amount of . Transaction ID: "' . self::TRANSACTION_ID . '-refund"';
     $amount = 50;
     $grandTotalCreditMemo = 50;
     $invoiceBaseGrandTotal = 50;
     $invoiceBaseTotalRefunded = 0;
     $this->invoiceMock->expects($this->any())->method('getBaseGrandTotal')->willReturn($invoiceBaseGrandTotal);
     $this->invoiceMock->expects($this->any())->method('getBaseTotalRefunded')->willReturn($invoiceBaseTotalRefunded);
     $this->creditMemoMock->expects($this->any())->method('getGrandTotal')->willReturn($grandTotalCreditMemo);
     $this->payment->setParentTransactionId($this->transactionId);
     $this->mockInvoice($this->transactionId, 1);
     $this->creditmemoFactoryMock->expects($this->once())->method('createByInvoice')->with($this->invoiceMock, ['adjustment_negative' => $invoiceBaseGrandTotal - $amount])->willReturn($this->creditMemoMock);
     $this->creditMemoMock->expects($this->once())->method('setPaymentRefundDisallowed')->willReturnSelf();
     $this->creditMemoMock->expects($this->once())->method('setAutomaticallyCreated')->willReturnSelf();
     $this->creditMemoMock->expects($this->once())->method('register')->willReturnSelf();
     $this->creditMemoMock->expects($this->once())->method('addComment')->willReturnSelf();
     $this->creditMemoMock->expects($this->once())->method('save')->willReturnSelf();
     $this->orderMock->expects($this->once())->method('getBaseCurrency')->willReturn($this->currencyMock);
     $parentTransaction = $this->getMock('Magento\\Sales\\Model\\Order\\Payment\\Transaction', ['setOrderId', 'setPaymentId', 'loadByTxnId', 'getId', 'getTxnId', 'setTxnId', 'getTxnType'], [], '', false);
     $newTransactionId = $this->transactionId . '-' . Transaction::TYPE_REFUND;
     $this->transactionRepositoryMock->expects($this->once())->method('getByTransactionId')->with($this->transactionId)->willReturn($parentTransaction);
     $this->transactionManagerMock->expects($this->once())->method('isTransactionExists')->with($newTransactionId)->willReturn(false);
     $this->transactionManagerMock->expects($this->once())->method('generateTransactionId')->with($this->payment, Transaction::TYPE_REFUND, $parentTransaction)->willReturn($newTransactionId);
     $status = 'status';
     $this->mockGetDefaultStatus(Order::STATE_PROCESSING, $status);
     $this->assertOrderUpdated(Order::STATE_PROCESSING, $status, $message);
     $additionalInformation = [];
     $failSafe = false;
     $transactionType = Transaction::TYPE_REFUND;
     $this->getTransactionBuilderMock($additionalInformation, $failSafe, $transactionType, $this->transactionId . '-' . Transaction::TYPE_REFUND);
     $this->assertSame($this->payment, $this->payment->registerRefundNotification($amount));
     $this->assertSame($this->creditMemoMock, $this->payment->getData('created_creditmemo'));
     $this->assertEquals($grandTotalCreditMemo, $this->payment->getData('amount_refunded'));
 }