creditmemoRepositoryMock = $this->getMockForAbstractClass( \Magento\Sales\Api\CreditmemoRepositoryInterface::class, ['get'], '', false ); $this->creditmemoCommentRepositoryMock = $this->getMockForAbstractClass( \Magento\Sales\Api\CreditmemoCommentRepositoryInterface::class, [], '', false ); $this->searchCriteriaBuilderMock = $this->createPartialMock( \Magento\Framework\Api\SearchCriteriaBuilder::class, ['create', 'addFilters'] ); $this->filterBuilderMock = $this->createPartialMock( \Magento\Framework\Api\FilterBuilder::class, ['setField', 'setValue', 'setConditionType', 'create'] ); $this->creditmemoNotifierMock = $this->createMock(\Magento\Sales\Model\Order\CreditmemoNotifier::class); $this->priceCurrency = $this->getMockBuilder(\Magento\Framework\Pricing\PriceCurrencyInterface::class) ->getMockForAbstractClass(); $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->creditmemoService = $this->objectManagerHelper->getObject( \Magento\Sales\Model\Service\CreditmemoService::class, [ 'creditmemoRepository' => $this->creditmemoRepositoryMock, 'creditmemoCommentRepository' => $this->creditmemoCommentRepositoryMock, 'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock, 'filterBuilder' => $this->filterBuilderMock, 'creditmemoNotifier' => $this->creditmemoNotifierMock, 'priceCurrency' => $this->priceCurrency, ] ); } /** * Run test cancel method * @expectedExceptionMessage You can not cancel Credit Memo * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testCancel() { $this->assertTrue($this->creditmemoService->cancel(1)); } /** * Run test getCommentsList method */ public function testGetCommentsList() { $id = 25; $returnValue = 'return-value'; $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class); $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class); $this->filterBuilderMock->expects($this->once()) ->method('setField') ->with('parent_id') ->will($this->returnSelf()); $this->filterBuilderMock->expects($this->once()) ->method('setValue') ->with($id) ->will($this->returnSelf()); $this->filterBuilderMock->expects($this->once()) ->method('setConditionType') ->with('eq') ->will($this->returnSelf()); $this->filterBuilderMock->expects($this->once()) ->method('create') ->will($this->returnValue($filterMock)); $this->searchCriteriaBuilderMock->expects($this->once()) ->method('addFilters') ->with([$filterMock]); $this->searchCriteriaBuilderMock->expects($this->once()) ->method('create') ->will($this->returnValue($searchCriteriaMock)); $this->creditmemoCommentRepositoryMock->expects($this->once()) ->method('getList') ->with($searchCriteriaMock) ->will($this->returnValue($returnValue)); $this->assertEquals($returnValue, $this->creditmemoService->getCommentsList($id)); } /** * Run test notify method */ public function testNotify() { $id = 123; $returnValue = 'return-value'; $modelMock = $this->getMockForAbstractClass( \Magento\Sales\Model\AbstractModel::class, [], '', false ); $this->creditmemoRepositoryMock->expects($this->once()) ->method('get') ->with($id) ->will($this->returnValue($modelMock)); $this->creditmemoNotifierMock->expects($this->once()) ->method('notify') ->with($modelMock) ->will($this->returnValue($returnValue)); $this->assertEquals($returnValue, $this->creditmemoService->notify($id)); } public function testRefund() { $creditMemoMock = $this->getMockBuilder(\Magento\Sales\Api\Data\CreditmemoInterface::class) ->setMethods(['getId', 'getOrder', 'getInvoice']) ->disableOriginalConstructor() ->getMockForAbstractClass(); $creditMemoMock->expects($this->once())->method('getId')->willReturn(null); $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->priceCurrency->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)); } 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->priceCurrency->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 */ public function testRefundExpectsMoneyAvailableToReturn() { $baseGrandTotal = 10; $baseTotalRefunded = 9; $baseTotalPaid = 10; /** @var CreditmemoInterface|MockObject $creditMemo */ $creditMemo = $this->getMockBuilder(CreditmemoInterface::class) ->setMethods(['getId', 'getOrder']) ->getMockForAbstractClass(); $creditMemo->method('getId') ->willReturn(null); /** @var Order|MockObject $order */ $order = $this->getMockBuilder(Order::class) ->disableOriginalConstructor() ->getMock(); $creditMemo->method('getOrder') ->willReturn($order); $creditMemo->method('getBaseGrandTotal') ->willReturn($baseGrandTotal); $order->method('getBaseTotalRefunded') ->willReturn($baseTotalRefunded); $this->priceCurrency->method('round') ->withConsecutive([$baseTotalRefunded + $baseGrandTotal], [$baseTotalPaid]) ->willReturnOnConsecutiveCalls($baseTotalRefunded + $baseGrandTotal, $baseTotalPaid); $order->method('getBaseTotalPaid') ->willReturn($baseTotalPaid); $baseAvailableRefund = $baseTotalPaid - $baseTotalRefunded; $order->method('formatPriceTxt') ->with($baseAvailableRefund) ->willReturn($baseAvailableRefund); $this->creditmemoService->refund($creditMemo, true); } /** * @expectedExceptionMessage We cannot register an existing credit memo. * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testRefundDoNotExpectsId() { $creditMemoMock = $this->getMockBuilder(\Magento\Sales\Api\Data\CreditmemoInterface::class) ->setMethods(['getId']) ->getMockForAbstractClass(); $creditMemoMock->expects($this->once())->method('getId')->willReturn(444); $this->creditmemoService->refund($creditMemoMock, true); } }