Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CreditmemoServiceTest.php
Go to the documentation of this file.
1 <?php
7 
9 
14 class CreditmemoServiceTest extends \PHPUnit\Framework\TestCase
15 {
20 
25 
30 
34  protected $filterBuilderMock;
35 
40 
44  private $priceCurrencyMock;
45 
49  protected $creditmemoService;
50 
54  private $objectManagerHelper;
55 
59  protected function setUp()
60  {
61  $this->creditmemoRepositoryMock = $this->getMockForAbstractClass(
62  \Magento\Sales\Api\CreditmemoRepositoryInterface::class,
63  ['get'],
64  '',
65  false
66  );
67  $this->creditmemoCommentRepositoryMock = $this->getMockForAbstractClass(
68  \Magento\Sales\Api\CreditmemoCommentRepositoryInterface::class,
69  [],
70  '',
71  false
72  );
73  $this->searchCriteriaBuilderMock = $this->createPartialMock(
74  \Magento\Framework\Api\SearchCriteriaBuilder::class,
75  ['create', 'addFilters']
76  );
77  $this->filterBuilderMock = $this->createPartialMock(
78  \Magento\Framework\Api\FilterBuilder::class,
79  ['setField', 'setValue', 'setConditionType', 'create']
80  );
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);
85 
86  $this->creditmemoService = $this->objectManagerHelper->getObject(
87  \Magento\Sales\Model\Service\CreditmemoService::class,
88  [
89  'creditmemoRepository' => $this->creditmemoRepositoryMock,
90  'creditmemoCommentRepository' => $this->creditmemoCommentRepositoryMock,
91  'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
92  'filterBuilder' => $this->filterBuilderMock,
93  'creditmemoNotifier' => $this->creditmemoNotifierMock,
94  'priceCurrency' => $this->priceCurrencyMock,
95  ]
96  );
97  }
98 
104  public function testCancel()
105  {
106  $this->assertTrue($this->creditmemoService->cancel(1));
107  }
108 
112  public function testGetCommentsList()
113  {
114  $id = 25;
115  $returnValue = 'return-value';
116 
117  $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
118  $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
119 
120  $this->filterBuilderMock->expects($this->once())
121  ->method('setField')
122  ->with('parent_id')
123  ->will($this->returnSelf());
124  $this->filterBuilderMock->expects($this->once())
125  ->method('setValue')
126  ->with($id)
127  ->will($this->returnSelf());
128  $this->filterBuilderMock->expects($this->once())
129  ->method('setConditionType')
130  ->with('eq')
131  ->will($this->returnSelf());
132  $this->filterBuilderMock->expects($this->once())
133  ->method('create')
134  ->will($this->returnValue($filterMock));
135  $this->searchCriteriaBuilderMock->expects($this->once())
136  ->method('addFilters')
137  ->with([$filterMock]);
138  $this->searchCriteriaBuilderMock->expects($this->once())
139  ->method('create')
140  ->will($this->returnValue($searchCriteriaMock));
141  $this->creditmemoCommentRepositoryMock->expects($this->once())
142  ->method('getList')
143  ->with($searchCriteriaMock)
144  ->will($this->returnValue($returnValue));
145 
146  $this->assertEquals($returnValue, $this->creditmemoService->getCommentsList($id));
147  }
148 
152  public function testNotify()
153  {
154  $id = 123;
155  $returnValue = 'return-value';
156 
157  $modelMock = $this->getMockForAbstractClass(
158  \Magento\Sales\Model\AbstractModel::class,
159  [],
160  '',
161  false
162  );
163 
164  $this->creditmemoRepositoryMock->expects($this->once())
165  ->method('get')
166  ->with($id)
167  ->will($this->returnValue($modelMock));
168  $this->creditmemoNotifierMock->expects($this->once())
169  ->method('notify')
170  ->with($modelMock)
171  ->will($this->returnValue($returnValue));
172 
173  $this->assertEquals($returnValue, $this->creditmemoService->notify($id));
174  }
175 
176  public function testRefund()
177  {
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();
184 
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);
189 
190  $this->priceCurrencyMock->expects($this->any())
191  ->method('round')
192  ->willReturnArgument(0);
193 
194  // Set payment adapter dependency
195  $refundAdapterMock = $this->getMockBuilder(\Magento\Sales\Model\Order\RefundAdapterInterface::class)
196  ->disableOriginalConstructor()
197  ->getMockForAbstractClass();
198  $this->objectManagerHelper->setBackwardCompatibleProperty(
199  $this->creditmemoService,
200  'refundAdapter',
201  $refundAdapterMock
202  );
203 
204  // Set resource dependency
205  $resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
206  ->disableOriginalConstructor()
207  ->getMock();
208  $this->objectManagerHelper->setBackwardCompatibleProperty(
209  $this->creditmemoService,
210  'resource',
211  $resourceMock
212  );
213 
214  // Set order repository dependency
215  $orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
216  ->disableOriginalConstructor()
217  ->getMockForAbstractClass();
218  $this->objectManagerHelper->setBackwardCompatibleProperty(
219  $this->creditmemoService,
220  'orderRepository',
221  $orderRepositoryMock
222  );
223 
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())
230  ->method('refund')
231  ->with($creditMemoMock, $orderMock, false)
232  ->willReturn($orderMock);
233  $orderRepositoryMock->expects($this->once())
234  ->method('save')
235  ->with($orderMock);
236  $creditMemoMock->expects($this->once())
237  ->method('getInvoice')
238  ->willReturn(null);
239  $adapterMock->expects($this->once())->method('commit');
240  $this->creditmemoRepositoryMock->expects($this->once())
241  ->method('save');
242 
243  $this->assertSame($creditMemoMock, $this->creditmemoService->refund($creditMemoMock, true));
244  }
245 
246  public function testRefundPendingCreditMemo()
247  {
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();
256 
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);
261 
262  $this->priceCurrencyMock->expects($this->any())
263  ->method('round')
264  ->willReturnArgument(0);
265 
266  // Set payment adapter dependency
267  $refundAdapterMock = $this->getMockBuilder(\Magento\Sales\Model\Order\RefundAdapterInterface::class)
268  ->disableOriginalConstructor()
269  ->getMockForAbstractClass();
270  $this->objectManagerHelper->setBackwardCompatibleProperty(
271  $this->creditmemoService,
272  'refundAdapter',
273  $refundAdapterMock
274  );
275 
276  // Set resource dependency
277  $resourceMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
278  ->disableOriginalConstructor()
279  ->getMock();
280  $this->objectManagerHelper->setBackwardCompatibleProperty(
281  $this->creditmemoService,
282  'resource',
283  $resourceMock
284  );
285 
286  // Set order repository dependency
287  $orderRepositoryMock = $this->getMockBuilder(\Magento\Sales\Api\OrderRepositoryInterface::class)
288  ->disableOriginalConstructor()
289  ->getMockForAbstractClass();
290  $this->objectManagerHelper->setBackwardCompatibleProperty(
291  $this->creditmemoService,
292  'orderRepository',
293  $orderRepositoryMock
294  );
295 
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())
302  ->method('refund')
303  ->with($creditMemoMock, $orderMock, false)
304  ->willReturn($orderMock);
305  $orderRepositoryMock->expects($this->once())
306  ->method('save')
307  ->with($orderMock);
308  $creditMemoMock->expects($this->once())
309  ->method('getInvoice')
310  ->willReturn(null);
311  $adapterMock->expects($this->once())->method('commit');
312  $this->creditmemoRepositoryMock->expects($this->once())
313  ->method('save');
314 
315  $this->assertSame($creditMemoMock, $this->creditmemoService->refund($creditMemoMock, true));
316  }
317 
323  {
324  $baseGrandTotal = 10;
325  $baseTotalRefunded = 9;
326  $baseTotalPaid = 10;
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],
337  [$baseTotalPaid]
338  )->willReturnOnConsecutiveCalls(
339  $baseTotalRefunded + $baseGrandTotal,
340  $baseTotalPaid
341  );
342  $orderMock->expects($this->atLeastOnce())->method('getBaseTotalPaid')->willReturn($baseTotalPaid);
343  $baseAvailableRefund = $baseTotalPaid - $baseTotalRefunded;
344  $orderMock->expects($this->once())->method('formatBasePrice')->with(
345  $baseAvailableRefund
346  )->willReturn($baseAvailableRefund);
347  $this->creditmemoService->refund($creditMemoMock, true);
348  }
349 
354  public function testRefundDoNotExpectsId()
355  {
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);
361  }
362 }
$id
Definition: fieldset.phtml:14