Skip to content

Commit

Permalink
Merge forwardport of #11550 to 2.3-develop branch
Browse files Browse the repository at this point in the history
Applied pull request patch https://github.com/magento/magento2/pull/11550.patch (created by @ajpevers) based on commit(s):
  1. 9380c95
  2. 8a6c3f2
  3. 480f8ae
  • Loading branch information
magento-engcom-team authored Jan 29, 2018
2 parents e2ff943 + d0b525b commit e27445d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/code/Magento/Sales/Model/Service/CreditmemoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function refund(
*/
protected function validateForRefund(\Magento\Sales\Api\Data\CreditmemoInterface $creditmemo)
{
if ($creditmemo->getId()) {
if ($creditmemo->getId() && $creditmemo->getState() != \Magento\Sales\Model\Order\Creditmemo::STATE_OPEN) {
throw new \Magento\Framework\Exception\LocalizedException(
__('We cannot register an existing credit memo.')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,78 @@ public function testRefund()
$this->assertSame($creditMemoMock, $this->creditmemoService->refund($creditMemoMock, true));
}

public function testRefundPendingCreditMemo()
{
$creditMemoMock = $this->getMockBuilder(\Magento\Sales\Api\Data\CreditmemoInterface::class)
->setMethods(['getId', 'getOrder', 'getState', 'getInvoice'])
->disableOriginalConstructor()
->getMockForAbstractClass();
$creditMemoMock->expects($this->once())->method('getId')->willReturn(444);
$creditMemoMock->expects($this->once())->method('getState')
->willReturn(\Magento\Sales\Model\Order\Creditmemo::STATE_OPEN);
$orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();

$creditMemoMock->expects($this->atLeastOnce())->method('getOrder')->willReturn($orderMock);
$orderMock->expects($this->once())->method('getBaseTotalRefunded')->willReturn(0);
$orderMock->expects($this->once())->method('getBaseTotalPaid')->willReturn(10);
$creditMemoMock->expects($this->once())->method('getBaseGrandTotal')->willReturn(10);

$this->priceCurrencyMock->expects($this->any())
->method('round')
->willReturnArgument(0);

// Set payment adapter dependency
$refundAdapterMock = $this->getMockBuilder(\Magento\Sales\Model\Order\RefundAdapterInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->objectManagerHelper->setBackwardCompatibleProperty(
$this->creditmemoService,
'refundAdapter',
$refundAdapterMock
);

// Set resource dependency
$resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
->disableOriginalConstructor()
->getMock();
$this->objectManagerHelper->setBackwardCompatibleProperty(
$this->creditmemoService,
'resource',
$resourceMock
);

// Set order repository dependency
$orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->objectManagerHelper->setBackwardCompatibleProperty(
$this->creditmemoService,
'orderRepository',
$orderRepositoryMock
);

$adapterMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$resourceMock->expects($this->once())->method('getConnection')->with('sales')->willReturn($adapterMock);
$adapterMock->expects($this->once())->method('beginTransaction');
$refundAdapterMock->expects($this->once())
->method('refund')
->with($creditMemoMock, $orderMock, false)
->willReturn($orderMock);
$orderRepositoryMock->expects($this->once())
->method('save')
->with($orderMock);
$creditMemoMock->expects($this->once())
->method('getInvoice')
->willReturn(null);
$adapterMock->expects($this->once())->method('commit');
$this->creditmemoRepositoryMock->expects($this->once())
->method('save');

$this->assertSame($creditMemoMock, $this->creditmemoService->refund($creditMemoMock, true));
}

/**
* @expectedExceptionMessage The most money available to refund is 1.
* @expectedException \Magento\Framework\Exception\LocalizedException
Expand Down

0 comments on commit e27445d

Please sign in to comment.