11
11
use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
12
12
use Magento \Sales \Api \Data \OrderInterface ;
13
13
use Magento \Sales \Model \Order ;
14
+ use Magento \Sales \Model \ResourceModel \Order \Item \Collection ;
14
15
use Magento \Sales \Model \ResourceModel \Order \Status \History \CollectionFactory as HistoryCollectionFactory ;
16
+ use Magento \Sales \Api \OrderItemRepositoryInterface ;
17
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
18
+ use Magento \Framework \Api \SearchCriteria ;
19
+ use Magento \Sales \Api \Data \OrderItemSearchResultInterface ;
15
20
16
21
/**
17
22
* Test class for \Magento\Sales\Model\Order
@@ -87,6 +92,16 @@ class OrderTest extends \PHPUnit\Framework\TestCase
87
92
*/
88
93
private $ timezone ;
89
94
95
+ /**
96
+ * @var OrderItemRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
97
+ */
98
+ private $ itemRepository ;
99
+
100
+ /**
101
+ * @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
102
+ */
103
+ private $ searchCriteriaBuilder ;
104
+
90
105
protected function setUp ()
91
106
{
92
107
$ helper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
@@ -144,6 +159,15 @@ protected function setUp()
144
159
$ this ->eventManager = $ this ->createMock (\Magento \Framework \Event \Manager::class);
145
160
$ context = $ this ->createPartialMock (\Magento \Framework \Model \Context::class, ['getEventDispatcher ' ]);
146
161
$ context ->expects ($ this ->any ())->method ('getEventDispatcher ' )->willReturn ($ this ->eventManager );
162
+
163
+ $ this ->itemRepository = $ this ->getMockBuilder (OrderItemRepositoryInterface::class)
164
+ ->setMethods (['getList ' ])
165
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
166
+
167
+ $ this ->searchCriteriaBuilder = $ this ->getMockBuilder (SearchCriteriaBuilder::class)
168
+ ->setMethods (['addFilter ' , 'create ' ])
169
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
170
+
147
171
$ this ->order = $ helper ->getObject (
148
172
\Magento \Sales \Model \Order::class,
149
173
[
@@ -157,37 +181,80 @@ protected function setUp()
157
181
'productListFactory ' => $ this ->productCollectionFactoryMock ,
158
182
'localeResolver ' => $ this ->localeResolver ,
159
183
'timezone ' => $ this ->timezone ,
184
+ 'itemRepository ' => $ this ->itemRepository ,
185
+ 'searchCriteriaBuilder ' => $ this ->searchCriteriaBuilder
160
186
]
161
187
);
162
188
}
163
189
164
- public function testGetItemById ()
190
+ /**
191
+ * Test testGetItems method.
192
+ */
193
+ public function testGetItems ()
165
194
{
166
- $ realOrderItemId = 1 ;
167
- $ fakeOrderItemId = 2 ;
195
+ $ orderItems = [$ this ->item ];
196
+
197
+ $ this ->searchCriteriaBuilder ->expects ($ this ->once ())->method ('addFilter ' )->willReturnSelf ();
198
+
199
+ $ searchCriteria = $ this ->getMockBuilder (SearchCriteria::class)
200
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
201
+ $ this ->searchCriteriaBuilder ->expects ($ this ->once ())->method ('create ' )->willReturn ($ searchCriteria );
168
202
169
- $ orderItem = $ this ->createMock (\Magento \Sales \Model \Order \Item::class);
203
+ $ itemsCollection = $ this ->getMockBuilder (OrderItemSearchResultInterface::class)
204
+ ->setMethods (['getItems ' ])
205
+ ->disableOriginalConstructor ()->getMockForAbstractClass ();
206
+ $ itemsCollection ->expects ($ this ->once ())->method ('getItems ' )->willReturn ($ orderItems );
207
+ $ this ->itemRepository ->expects ($ this ->once ())->method ('getList ' )->willReturn ($ itemsCollection );
170
208
209
+ $ this ->assertEquals ($ orderItems , $ this ->order ->getItems ());
210
+ }
211
+
212
+ /**
213
+ * Prepare order item mock.
214
+ *
215
+ * @param int $orderId
216
+ * @return void
217
+ */
218
+ private function prepareOrderItem (int $ orderId = 0 )
219
+ {
171
220
$ this ->order ->setData (
172
221
\Magento \Sales \Api \Data \OrderInterface::ITEMS ,
173
222
[
174
- $ realOrderItemId => $ orderItem
223
+ $ orderId => $ this -> item
175
224
]
176
225
);
226
+ }
177
227
178
- $ this ->assertEquals ($ orderItem , $ this ->order ->getItemById ($ realOrderItemId ));
228
+ /**
229
+ * Test GetItemById method.
230
+ *
231
+ * @return void
232
+ */
233
+ public function testGetItemById ()
234
+ {
235
+ $ realOrderItemId = 1 ;
236
+ $ fakeOrderItemId = 2 ;
237
+
238
+ $ this ->prepareOrderItem ($ realOrderItemId );
239
+
240
+ $ this ->assertEquals ($ this ->item , $ this ->order ->getItemById ($ realOrderItemId ));
179
241
$ this ->assertEquals (null , $ this ->order ->getItemById ($ fakeOrderItemId ));
180
242
}
181
243
182
244
/**
245
+ * Test GetItemByQuoteItemId method.
246
+ *
183
247
* @param int|null $gettingQuoteItemId
184
248
* @param int|null $quoteItemId
185
249
* @param string|null $result
186
250
*
187
251
* @dataProvider dataProviderGetItemByQuoteItemId
252
+ * @return void
188
253
*/
189
254
public function testGetItemByQuoteItemId ($ gettingQuoteItemId , $ quoteItemId , $ result )
190
255
{
256
+ $ this ->prepareOrderItem ();
257
+
191
258
$ this ->item ->expects ($ this ->any ())
192
259
->method ('getQuoteItemId ' )
193
260
->willReturn ($ gettingQuoteItemId );
@@ -212,14 +279,19 @@ public function dataProviderGetItemByQuoteItemId()
212
279
}
213
280
214
281
/**
282
+ * Test getAllVisibleItems method.
283
+ *
215
284
* @param bool $isDeleted
216
285
* @param int|null $parentItemId
217
286
* @param array $result
218
287
*
219
288
* @dataProvider dataProviderGetAllVisibleItems
289
+ * @return void
220
290
*/
221
291
public function testGetAllVisibleItems ($ isDeleted , $ parentItemId , array $ result )
222
292
{
293
+ $ this ->prepareOrderItem ();
294
+
223
295
$ this ->item ->expects ($ this ->once ())
224
296
->method ('isDeleted ' )
225
297
->willReturn ($ isDeleted );
@@ -263,8 +335,15 @@ public function testCanCancelIsPaymentReview()
263
335
$ this ->assertFalse ($ this ->order ->canCancel ());
264
336
}
265
337
338
+ /**
339
+ * Test CanInvoice method.
340
+ *
341
+ * @return void
342
+ */
266
343
public function testCanInvoice ()
267
344
{
345
+ $ this ->prepareOrderItem ();
346
+
268
347
$ this ->item ->expects ($ this ->any ())
269
348
->method ('getQtyToInvoice ' )
270
349
->willReturn (42 );
@@ -304,8 +383,15 @@ public function testCanNotInvoiceWhenActionInvoiceFlagIsFalse()
304
383
$ this ->assertFalse ($ this ->order ->canInvoice ());
305
384
}
306
385
386
+ /**
387
+ * Test CanNotInvoice method when invoice is locked.
388
+ *
389
+ * @return void
390
+ */
307
391
public function testCanNotInvoiceWhenLockedInvoice ()
308
392
{
393
+ $ this ->prepareOrderItem ();
394
+
309
395
$ this ->item ->expects ($ this ->any ())
310
396
->method ('getQtyToInvoice ' )
311
397
->willReturn (42 );
@@ -315,8 +401,15 @@ public function testCanNotInvoiceWhenLockedInvoice()
315
401
$ this ->assertFalse ($ this ->order ->canInvoice ());
316
402
}
317
403
404
+ /**
405
+ * Test CanNotInvoice method when didn't have qty to invoice.
406
+ *
407
+ * @return void
408
+ */
318
409
public function testCanNotInvoiceWhenDidNotHaveQtyToInvoice ()
319
410
{
411
+ $ this ->prepareOrderItem ();
412
+
320
413
$ this ->item ->expects ($ this ->any ())
321
414
->method ('getQtyToInvoice ' )
322
415
->willReturn (0 );
@@ -329,6 +422,7 @@ public function testCanNotInvoiceWhenDidNotHaveQtyToInvoice()
329
422
public function testCanCreditMemo ()
330
423
{
331
424
$ totalPaid = 10 ;
425
+ $ this ->prepareOrderItem ();
332
426
$ this ->order ->setTotalPaid ($ totalPaid );
333
427
$ this ->priceCurrency ->expects ($ this ->once ())->method ('round ' )->with ($ totalPaid )->willReturnArgument (0 );
334
428
$ this ->assertTrue ($ this ->order ->canCreditmemo ());
@@ -344,6 +438,7 @@ public function testCanCreditMemoForZeroTotal()
344
438
$ grandTotal = 0 ;
345
439
$ totalPaid = 0 ;
346
440
$ totalRefunded = 0 ;
441
+ $ this ->prepareOrderItem ();
347
442
$ this ->order ->setGrandTotal ($ grandTotal );
348
443
$ this ->order ->setTotalPaid ($ totalPaid );
349
444
$ this ->assertFalse ($ this ->order ->canCreditmemoForZeroTotal ($ totalRefunded ));
@@ -352,6 +447,7 @@ public function testCanCreditMemoForZeroTotal()
352
447
public function testCanNotCreditMemoWithTotalNull ()
353
448
{
354
449
$ totalPaid = 0 ;
450
+ $ this ->prepareOrderItem ();
355
451
$ this ->order ->setTotalPaid ($ totalPaid );
356
452
$ this ->priceCurrency ->expects ($ this ->once ())->method ('round ' )->with ($ totalPaid )->willReturnArgument (0 );
357
453
$ this ->assertFalse ($ this ->order ->canCreditmemo ());
@@ -363,6 +459,7 @@ public function testCanNotCreditMemoWithAdjustmentNegative()
363
459
$ adjustmentNegative = 10 ;
364
460
$ totalRefunded = 90 ;
365
461
462
+ $ this ->prepareOrderItem ();
366
463
$ this ->order ->setTotalPaid ($ totalPaid );
367
464
$ this ->order ->setTotalRefunded ($ totalRefunded );
368
465
$ this ->order ->setAdjustmentNegative ($ adjustmentNegative );
@@ -377,6 +474,7 @@ public function testCanCreditMemoWithAdjustmentNegativeLowerThanTotalPaid()
377
474
$ adjustmentNegative = 9 ;
378
475
$ totalRefunded = 90 ;
379
476
477
+ $ this ->prepareOrderItem ();
380
478
$ this ->order ->setTotalPaid ($ totalPaid );
381
479
$ this ->order ->setTotalRefunded ($ totalRefunded );
382
480
$ this ->order ->setAdjustmentNegative ($ adjustmentNegative );
@@ -601,8 +699,15 @@ public function testCanCancelCanReviewPayment()
601
699
$ this ->assertFalse ($ this ->order ->canCancel ());
602
700
}
603
701
702
+ /**
703
+ * Test CanCancelAllInvoiced method.
704
+ *
705
+ * @return void
706
+ */
604
707
public function testCanCancelAllInvoiced ()
605
708
{
709
+ $ this ->prepareOrderItem ();
710
+
606
711
$ paymentMock = $ this ->getMockBuilder (\Magento \Sales \Model \ResourceModel \Order \Payment::class)
607
712
->disableOriginalConstructor ()
608
713
->setMethods (['isDeleted ' , 'canReviewPayment ' , 'canFetchTransactionInfo ' , '__wakeUp ' ])
@@ -662,11 +767,16 @@ public function testCanCancelState()
662
767
}
663
768
664
769
/**
770
+ * Test CanCancelActionFlag method.
771
+ *
665
772
* @param bool $cancelActionFlag
666
773
* @dataProvider dataProviderActionFlag
774
+ * @return void
667
775
*/
668
776
public function testCanCancelActionFlag ($ cancelActionFlag )
669
777
{
778
+ $ this ->prepareOrderItem ();
779
+
670
780
$ paymentMock = $ this ->getMockBuilder (\Magento \Sales \Model \ResourceModel \Order \Payment::class)
671
781
->disableOriginalConstructor ()
672
782
->setMethods (['isDeleted ' , 'canReviewPayment ' , 'canFetchTransactionInfo ' , '__wakeUp ' ])
0 commit comments