44 private $priceCurrencyMock;
54 private $objectManagerHelper;
61 $this->creditmemoRepositoryMock = $this->getMockForAbstractClass(
62 \
Magento\Sales\Api\CreditmemoRepositoryInterface::class,
67 $this->creditmemoCommentRepositoryMock = $this->getMockForAbstractClass(
68 \
Magento\Sales\Api\CreditmemoCommentRepositoryInterface::class,
73 $this->searchCriteriaBuilderMock = $this->createPartialMock(
74 \
Magento\Framework\Api\SearchCriteriaBuilder::class,
75 [
'create',
'addFilters']
77 $this->filterBuilderMock = $this->createPartialMock(
78 \
Magento\Framework\Api\FilterBuilder::class,
79 [
'setField',
'setValue',
'setConditionType',
'create']
81 $this->creditmemoNotifierMock = $this->createMock(\
Magento\Sales\Model\
Order\CreditmemoNotifier::class);
82 $this->priceCurrencyMock = $this->getMockBuilder(\
Magento\Framework\Pricing\PriceCurrencyInterface::class)
83 ->getMockForAbstractClass();
84 $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
86 $this->creditmemoService = $this->objectManagerHelper->getObject(
87 \
Magento\Sales\Model\Service\CreditmemoService::class,
89 'creditmemoRepository' => $this->creditmemoRepositoryMock,
90 'creditmemoCommentRepository' => $this->creditmemoCommentRepositoryMock,
91 'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
92 'filterBuilder' => $this->filterBuilderMock,
93 'creditmemoNotifier' => $this->creditmemoNotifierMock,
94 'priceCurrency' => $this->priceCurrencyMock,
106 $this->assertTrue($this->creditmemoService->cancel(1));
115 $returnValue =
'return-value';
117 $filterMock = $this->createMock(\
Magento\Framework\Api\Filter::class);
118 $searchCriteriaMock = $this->createMock(\
Magento\Framework\Api\SearchCriteria::class);
120 $this->filterBuilderMock->expects($this->once())
123 ->will($this->returnSelf());
124 $this->filterBuilderMock->expects($this->once())
127 ->will($this->returnSelf());
128 $this->filterBuilderMock->expects($this->once())
129 ->method(
'setConditionType')
131 ->will($this->returnSelf());
132 $this->filterBuilderMock->expects($this->once())
134 ->will($this->returnValue($filterMock));
135 $this->searchCriteriaBuilderMock->expects($this->once())
136 ->method(
'addFilters')
137 ->with([$filterMock]);
138 $this->searchCriteriaBuilderMock->expects($this->once())
140 ->will($this->returnValue($searchCriteriaMock));
141 $this->creditmemoCommentRepositoryMock->expects($this->once())
143 ->with($searchCriteriaMock)
144 ->will($this->returnValue($returnValue));
146 $this->assertEquals($returnValue, $this->creditmemoService->getCommentsList(
$id));
155 $returnValue =
'return-value';
157 $modelMock = $this->getMockForAbstractClass(
158 \
Magento\Sales\Model\AbstractModel::class,
164 $this->creditmemoRepositoryMock->expects($this->once())
167 ->will($this->returnValue($modelMock));
168 $this->creditmemoNotifierMock->expects($this->once())
171 ->will($this->returnValue($returnValue));
173 $this->assertEquals($returnValue, $this->creditmemoService->notify(
$id));
178 $creditMemoMock = $this->getMockBuilder(\
Magento\Sales\Api\
Data\CreditmemoInterface::class)
179 ->setMethods([
'getId',
'getOrder',
'getInvoice'])
180 ->disableOriginalConstructor()
181 ->getMockForAbstractClass();
182 $creditMemoMock->expects($this->once())->method(
'getId')->willReturn(
null);
183 $orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
185 $creditMemoMock->expects($this->atLeastOnce())->method(
'getOrder')->willReturn($orderMock);
186 $orderMock->expects($this->once())->method(
'getBaseTotalRefunded')->willReturn(0);
187 $orderMock->expects($this->once())->method(
'getBaseTotalPaid')->willReturn(10);
188 $creditMemoMock->expects($this->once())->method(
'getBaseGrandTotal')->willReturn(10);
190 $this->priceCurrencyMock->expects($this->any())
192 ->willReturnArgument(0);
195 $refundAdapterMock = $this->getMockBuilder(\
Magento\Sales\Model\
Order\RefundAdapterInterface::class)
196 ->disableOriginalConstructor()
197 ->getMockForAbstractClass();
198 $this->objectManagerHelper->setBackwardCompatibleProperty(
199 $this->creditmemoService,
205 $resourceMock = $this->getMockBuilder(\
Magento\Framework\
App\ResourceConnection::class)
206 ->disableOriginalConstructor()
208 $this->objectManagerHelper->setBackwardCompatibleProperty(
209 $this->creditmemoService,
215 $orderRepositoryMock = $this->getMockBuilder(\
Magento\Sales\Api\OrderRepositoryInterface::class)
216 ->disableOriginalConstructor()
217 ->getMockForAbstractClass();
218 $this->objectManagerHelper->setBackwardCompatibleProperty(
219 $this->creditmemoService,
224 $adapterMock = $this->getMockBuilder(\
Magento\Framework\DB\Adapter\AdapterInterface::class)
225 ->disableOriginalConstructor()
226 ->getMockForAbstractClass();
227 $resourceMock->expects($this->once())->method(
'getConnection')->with(
'sales')->willReturn($adapterMock);
228 $adapterMock->expects($this->once())->method(
'beginTransaction');
229 $refundAdapterMock->expects($this->once())
231 ->with($creditMemoMock, $orderMock,
false)
232 ->willReturn($orderMock);
233 $orderRepositoryMock->expects($this->once())
236 $creditMemoMock->expects($this->once())
237 ->method(
'getInvoice')
239 $adapterMock->expects($this->once())->method(
'commit');
240 $this->creditmemoRepositoryMock->expects($this->once())
243 $this->assertSame($creditMemoMock, $this->creditmemoService->refund($creditMemoMock,
true));
248 $creditMemoMock = $this->getMockBuilder(\
Magento\Sales\Api\
Data\CreditmemoInterface::class)
249 ->setMethods([
'getId',
'getOrder',
'getState',
'getInvoice'])
250 ->disableOriginalConstructor()
251 ->getMockForAbstractClass();
252 $creditMemoMock->expects($this->once())->method(
'getId')->willReturn(444);
253 $creditMemoMock->expects($this->once())->method(
'getState')
254 ->willReturn(\
Magento\Sales\Model\
Order\Creditmemo::STATE_OPEN);
255 $orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
257 $creditMemoMock->expects($this->atLeastOnce())->method(
'getOrder')->willReturn($orderMock);
258 $orderMock->expects($this->once())->method(
'getBaseTotalRefunded')->willReturn(0);
259 $orderMock->expects($this->once())->method(
'getBaseTotalPaid')->willReturn(10);
260 $creditMemoMock->expects($this->once())->method(
'getBaseGrandTotal')->willReturn(10);
262 $this->priceCurrencyMock->expects($this->any())
264 ->willReturnArgument(0);
267 $refundAdapterMock = $this->getMockBuilder(\
Magento\Sales\Model\
Order\RefundAdapterInterface::class)
268 ->disableOriginalConstructor()
269 ->getMockForAbstractClass();
270 $this->objectManagerHelper->setBackwardCompatibleProperty(
271 $this->creditmemoService,
277 $resourceMock = $this->getMockBuilder(\
Magento\Framework\
App\ResourceConnection::class)
278 ->disableOriginalConstructor()
280 $this->objectManagerHelper->setBackwardCompatibleProperty(
281 $this->creditmemoService,
287 $orderRepositoryMock = $this->getMockBuilder(\
Magento\Sales\Api\OrderRepositoryInterface::class)
288 ->disableOriginalConstructor()
289 ->getMockForAbstractClass();
290 $this->objectManagerHelper->setBackwardCompatibleProperty(
291 $this->creditmemoService,
296 $adapterMock = $this->getMockBuilder(\
Magento\Framework\DB\Adapter\AdapterInterface::class)
297 ->disableOriginalConstructor()
298 ->getMockForAbstractClass();
299 $resourceMock->expects($this->once())->method(
'getConnection')->with(
'sales')->willReturn($adapterMock);
300 $adapterMock->expects($this->once())->method(
'beginTransaction');
301 $refundAdapterMock->expects($this->once())
303 ->with($creditMemoMock, $orderMock,
false)
304 ->willReturn($orderMock);
305 $orderRepositoryMock->expects($this->once())
308 $creditMemoMock->expects($this->once())
309 ->method(
'getInvoice')
311 $adapterMock->expects($this->once())->method(
'commit');
312 $this->creditmemoRepositoryMock->expects($this->once())
315 $this->assertSame($creditMemoMock, $this->creditmemoService->refund($creditMemoMock,
true));
324 $baseGrandTotal = 10;
325 $baseTotalRefunded = 9;
327 $creditMemoMock = $this->getMockBuilder(\
Magento\Sales\Api\
Data\CreditmemoInterface::class)
328 ->setMethods([
'getId',
'getOrder',
'formatBasePrice'])
329 ->getMockForAbstractClass();
330 $creditMemoMock->expects($this->once())->method(
'getId')->willReturn(
null);
331 $orderMock = $this->getMockBuilder(Order::class)->disableOriginalConstructor()->getMock();
332 $creditMemoMock->expects($this->atLeastOnce())->method(
'getOrder')->willReturn($orderMock);
333 $creditMemoMock->expects($this->once())->method(
'getBaseGrandTotal')->willReturn($baseGrandTotal);
334 $orderMock->expects($this->atLeastOnce())->method(
'getBaseTotalRefunded')->willReturn($baseTotalRefunded);
335 $this->priceCurrencyMock->expects($this->exactly(2))->method(
'round')->withConsecutive(
336 [$baseTotalRefunded + $baseGrandTotal],
338 )->willReturnOnConsecutiveCalls(
339 $baseTotalRefunded + $baseGrandTotal,
342 $orderMock->expects($this->atLeastOnce())->method(
'getBaseTotalPaid')->willReturn($baseTotalPaid);
343 $baseAvailableRefund = $baseTotalPaid - $baseTotalRefunded;
344 $orderMock->expects($this->once())->method(
'formatBasePrice')->with(
346 )->willReturn($baseAvailableRefund);
347 $this->creditmemoService->refund($creditMemoMock,
true);
356 $creditMemoMock = $this->getMockBuilder(\
Magento\Sales\Api\
Data\CreditmemoInterface::class)
357 ->setMethods([
'getId'])
358 ->getMockForAbstractClass();
359 $creditMemoMock->expects($this->once())->method(
'getId')->willReturn(444);
360 $this->creditmemoService->refund($creditMemoMock,
true);
$creditmemoRepositoryMock
testRefundDoNotExpectsId()
testRefundExpectsMoneyAvailableToReturn()
$searchCriteriaBuilderMock
$creditmemoCommentRepositoryMock
testRefundPendingCreditMemo()