@@ -189,7 +189,7 @@ public function testSetLinkStatusPending($orderState, array $orderStateMapping)
189
189
]
190
190
);
191
191
192
- $ this ->itemsFactory ->expects ($ this ->once ())
192
+ $ this ->itemsFactory ->expects ($ this ->any ())
193
193
->method ('create ' )
194
194
->willReturn (
195
195
$ this ->createLinkItemCollection (
@@ -243,7 +243,7 @@ public function testSetLinkStatusClosed()
243
243
]
244
244
);
245
245
246
- $ this ->itemsFactory ->expects ($ this ->once ())
246
+ $ this ->itemsFactory ->expects ($ this ->any ())
247
247
->method ('create ' )
248
248
->willReturn (
249
249
$ this ->createLinkItemCollection (
@@ -308,7 +308,7 @@ public function testSetLinkStatusInvoiced()
308
308
]
309
309
);
310
310
311
- $ this ->itemsFactory ->expects ($ this ->once ())
311
+ $ this ->itemsFactory ->expects ($ this ->any ())
312
312
->method ('create ' )
313
313
->willReturn (
314
314
$ this ->createLinkItemCollection (
@@ -344,6 +344,137 @@ public function testSetLinkStatusEmptyOrder()
344
344
$ this ->assertInstanceOf (SetLinkStatusObserver::class, $ result );
345
345
}
346
346
347
+ public function testSetLinkStatusExpired ()
348
+ {
349
+ $ this ->scopeConfig ->expects ($ this ->once ())
350
+ ->method ('getValue ' )
351
+ ->with (
352
+ \Magento \Downloadable \Model \Link \Purchased \Item::XML_PATH_ORDER_ITEM_STATUS ,
353
+ ScopeInterface::SCOPE_STORE ,
354
+ 1
355
+ )
356
+ ->willReturn (Item::STATUS_PENDING );
357
+
358
+ $ this ->observerMock ->expects ($ this ->once ())
359
+ ->method ('getEvent ' )
360
+ ->willReturn ($ this ->eventMock );
361
+
362
+ $ this ->eventMock ->expects ($ this ->once ())
363
+ ->method ('getOrder ' )
364
+ ->willReturn ($ this ->orderMock );
365
+
366
+ $ this ->orderMock ->expects ($ this ->once ())
367
+ ->method ('getId ' )
368
+ ->willReturn (1 );
369
+
370
+ $ this ->orderMock ->expects ($ this ->once ())
371
+ ->method ('getStoreId ' )
372
+ ->willReturn (1 );
373
+
374
+ $ this ->orderMock ->expects ($ this ->atLeastOnce ())
375
+ ->method ('getState ' )
376
+ ->willReturn (Order::STATE_PROCESSING );
377
+
378
+ $ this ->orderMock ->expects ($ this ->any ())
379
+ ->method ('getAllItems ' )
380
+ ->willReturn (
381
+ [
382
+ $ this ->createRefundOrderItem (2 , 2 , 2 ),
383
+ $ this ->createRefundOrderItem (3 , 2 , 1 ),
384
+ $ this ->createRefundOrderItem (4 , 3 , 3 ),
385
+ ]
386
+ );
387
+
388
+ $ this ->itemsFactory ->expects ($ this ->any ())
389
+ ->method ('create ' )
390
+ ->willReturn (
391
+ $ this ->createLinkItemToExpireCollection (
392
+ [2 , 4 ],
393
+ [
394
+ $ this ->createLinkItem (
395
+ 'available ' ,
396
+ 2 ,
397
+ true ,
398
+ \Magento \Downloadable \Model \Link \Purchased \Item::LINK_STATUS_EXPIRED
399
+ ),
400
+ $ this ->createLinkItem (
401
+ 'pending_payment ' ,
402
+ 4 ,
403
+ true ,
404
+ \Magento \Downloadable \Model \Link \Purchased \Item::LINK_STATUS_EXPIRED
405
+ ),
406
+ ]
407
+ )
408
+ );
409
+
410
+ $ result = $ this ->setLinkStatusObserver ->execute ($ this ->observerMock );
411
+ $ this ->assertInstanceOf (SetLinkStatusObserver::class, $ result );
412
+ }
413
+
414
+ /**
415
+ * @param $id
416
+ * @param int $qtyOrdered
417
+ * @param int $qtyRefunded
418
+ * @param string $productType
419
+ * @param string $realProductType
420
+ * @return \Magento\Sales\Model\Order\Item|MockObject
421
+ */
422
+ private function createRefundOrderItem (
423
+ $ id ,
424
+ $ qtyOrdered ,
425
+ $ qtyRefunded ,
426
+ $ productType = DownloadableProductType::TYPE_DOWNLOADABLE ,
427
+ $ realProductType = DownloadableProductType::TYPE_DOWNLOADABLE
428
+ ) {
429
+ $ item = $ this ->getMockBuilder (Item::class)
430
+ ->disableOriginalConstructor ()
431
+ ->setMethods ([
432
+ 'getId ' ,
433
+ 'getQtyOrdered ' ,
434
+ 'getQtyRefunded ' ,
435
+ 'getProductType ' ,
436
+ 'getRealProductType '
437
+ ])->getMock ();
438
+ $ item ->expects ($ this ->any ())
439
+ ->method ('getId ' )
440
+ ->willReturn ($ id );
441
+ $ item ->expects ($ this ->any ())
442
+ ->method ('getQtyOrdered ' )
443
+ ->willReturn ($ qtyOrdered );
444
+ $ item ->expects ($ this ->any ())
445
+ ->method ('getQtyRefunded ' )
446
+ ->willReturn ($ qtyRefunded );
447
+ $ item ->expects ($ this ->any ())
448
+ ->method ('getProductType ' )
449
+ ->willReturn ($ productType );
450
+ $ item ->expects ($ this ->any ())
451
+ ->method ('getRealProductType ' )
452
+ ->willReturn ($ realProductType );
453
+
454
+ return $ item ;
455
+ }
456
+
457
+ /**
458
+ * @param array $expectedOrderItemIds
459
+ * @param array $items
460
+ * @return LinkItemCollection|MockObject
461
+ */
462
+ private function createLinkItemToExpireCollection (array $ expectedOrderItemIds , array $ items )
463
+ {
464
+ $ linkItemCollection = $ this ->getMockBuilder (
465
+ \Magento \Downloadable \Model \ResourceModel \Link \Purchased \Item \Collection::class
466
+ )
467
+ ->disableOriginalConstructor ()
468
+ ->setMethods (['addFieldToFilter ' ])
469
+ ->getMock ();
470
+ $ linkItemCollection ->expects ($ this ->any ())
471
+ ->method ('addFieldToFilter ' )
472
+ ->with ('order_item_id ' , ['in ' => $ expectedOrderItemIds ])
473
+ ->willReturn ($ items );
474
+
475
+ return $ linkItemCollection ;
476
+ }
477
+
347
478
/**
348
479
* @param $id
349
480
* @param int $statusId
@@ -359,7 +490,7 @@ private function createOrderItem(
359
490
) {
360
491
$ item = $ this ->getMockBuilder (Item::class)
361
492
->disableOriginalConstructor ()
362
- ->setMethods (['getId ' , 'getProductType ' , 'getRealProductType ' , 'getStatusId ' ])
493
+ ->setMethods (['getId ' , 'getProductType ' , 'getRealProductType ' , 'getStatusId ' , ' getQtyOrdered ' ])
363
494
->getMock ();
364
495
$ item ->expects ($ this ->any ())
365
496
->method ('getId ' )
@@ -373,6 +504,9 @@ private function createOrderItem(
373
504
$ item ->expects ($ this ->any ())
374
505
->method ('getStatusId ' )
375
506
->willReturn ($ statusId );
507
+ $ item ->expects ($ this ->any ())
508
+ ->method ('getQtyOrdered ' )
509
+ ->willReturn (1 );
376
510
377
511
return $ item ;
378
512
}
@@ -390,7 +524,7 @@ private function createLinkItemCollection(array $expectedOrderItemIds, array $it
390
524
->disableOriginalConstructor ()
391
525
->setMethods (['addFieldToFilter ' ])
392
526
->getMock ();
393
- $ linkItemCollection ->expects ($ this ->once ())
527
+ $ linkItemCollection ->expects ($ this ->any ())
394
528
->method ('addFieldToFilter ' )
395
529
->with ('order_item_id ' , ['in ' => $ expectedOrderItemIds ])
396
530
->willReturn ($ items );
@@ -415,11 +549,11 @@ private function createLinkItem($status, $orderItemId, $isSaved = false, $expect
415
549
->method ('getStatus ' )
416
550
->willReturn ($ status );
417
551
if ($ isSaved ) {
418
- $ linkItem ->expects ($ this ->once ())
552
+ $ linkItem ->expects ($ this ->any ())
419
553
->method ('setStatus ' )
420
554
->with ($ expectedStatus )
421
555
->willReturnSelf ();
422
- $ linkItem ->expects ($ this ->once ())
556
+ $ linkItem ->expects ($ this ->any ())
423
557
->method ('save ' )
424
558
->willReturnSelf ();
425
559
}
0 commit comments