17
17
use Magento \Customer \Model \ResourceModel \CustomerRepository ;
18
18
use Magento \Framework \Data \Form \FormKey ;
19
19
use Magento \Framework \Api \SearchCriteriaBuilder ;
20
+ use Magento \Framework \Exception \LocalizedException ;
21
+ use Magento \Framework \Exception \NoSuchEntityException ;
22
+ use Magento \Quote \Api \Data \CartInterface ;
23
+ use Magento \Quote \Api \Data \CartItemInterface ;
20
24
use Magento \Quote \Model \Quote ;
21
25
use Magento \Quote \Api \CartRepositoryInterface ;
22
26
use Magento \Quote \Model \QuoteRepository ;
@@ -468,6 +472,10 @@ private function prepareRequest(string $method)
468
472
}
469
473
}
470
474
475
+ /**
476
+ * @throws NoSuchEntityException
477
+ * @throws LocalizedException
478
+ */
471
479
#[
472
480
DataFixture(ProductFixture::class, ['sku ' => 's1 ' , 'stock_item ' => ['is_in_stock ' => true ]], 'p1 ' ),
473
481
DataFixture(ProductFixture::class, ['sku ' => 's2 ' ,'stock_item ' => ['is_in_stock ' => true ]], 'p2 ' ),
@@ -483,7 +491,8 @@ private function prepareRequest(string $method)
483
491
'item2 '
484
492
)
485
493
]
486
- public function testUpdatePostActionWithMultipleProducts () {
494
+ public function testUpdatePostActionWithMultipleProducts ()
495
+ {
487
496
$ cartId = (int )$ this ->fixtures ->get ('cart ' )->getId ();
488
497
if (!$ cartId ) {
489
498
$ this ->fail ('quote fixture failed ' );
@@ -513,52 +522,82 @@ public function testUpdatePostActionWithMultipleProducts() {
513
522
$ updatedQuantity = 2 ;
514
523
515
524
$ this ->assertEquals (
516
- $ quote ->getItemsQty (),
517
525
$ originalQuantity + $ originalQuantity ,
518
- "Precondition failed: invalid quote item quantity "
526
+ $ quote ->getItemsQty (),
527
+ "Precondition failed: quote totals does not match. "
519
528
);
520
529
521
- /** @var FormKey $formKey */
522
- $ formKey = Bootstrap::getObjectManager ()->get (FormKey::class);
530
+ $ response = $ this ->updatePostRequest ($ quote , $ item1 , $ item2 , $ updatedQuantity , $ updatedQuantity , true );
523
531
524
- $ request = [
525
- 'form_key ' => $ formKey ->getFormKey (),
526
- 'cart ' => [
527
- $ item1 ->getId () => ['qty ' => $ updatedQuantity ],
528
- $ item2 ->getId () => ['qty ' => $ updatedQuantity ]
529
- ]
530
- ];
532
+ $ this ->assertContains (
533
+ '[{"error":"There are no source items with the in stock status","itemId": ' .$ item1 ->getId ().'}] ' ,
534
+ $ response
535
+ );
531
536
532
- $ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
533
- $ this ->getRequest ()->setPostValue ($ request );
534
- $ this ->dispatch ('checkout/cart/updateItemQty ' );
535
- $ response = $ this ->getResponse ()->getBody ();
536
- $ response = json_decode ($ response , true );
537
+ $ response = $ this ->updatePostRequest ($ quote , $ item1 , $ item2 , $ originalQuantity , $ updatedQuantity , false );
538
+
539
+ $ this ->assertContains (
540
+ '[{"error":"There are no source items with the in stock status","itemId": ' .$ item1 ->getId ().'}] ' ,
541
+ $ response
542
+ );
543
+ $ this ->assertEquals (
544
+ $ originalQuantity + $ updatedQuantity ,
545
+ $ quote ->getItemsQty (),
546
+ "Precondition failed: quote totals does not match. "
547
+ );
537
548
538
- $ this ->assertContains ('[{"error":"There are no source items with the in stock status","itemId": ' .$ item1 ->getId ().'}] ' , $ response );
549
+ $ response = $ this ->updatePostRequest ($ quote , $ item1 , $ item2 , $ updatedQuantity , $ updatedQuantity , false );
550
+
551
+ $ this ->assertContains (
552
+ '[{"error":"There are no source items with the in stock status","itemId": ' .$ item1 ->getId ().'}] ' ,
553
+ $ response
554
+ );
555
+ $ this ->assertEquals (
556
+ $ originalQuantity + $ updatedQuantity ,
557
+ $ quote ->getItemsQty (),
558
+ "Precondition failed: quote totals does not match. "
559
+ );
560
+ }
561
+
562
+ /**
563
+ * @param CartInterface $quote
564
+ * @param CartItemInterface $item1
565
+ * @param CartItemInterface $item2
566
+ * @param float $qty1
567
+ * @param float $qty2
568
+ * @param bool $updateQty
569
+ * @return mixed
570
+ * @throws LocalizedException
571
+ */
572
+ private function updatePostRequest (
573
+ CartInterface $ quote ,
574
+ CartItemInterface $ item1 ,
575
+ CartItemInterface $ item2 ,
576
+ float $ qty1 ,
577
+ float $ qty2 ,
578
+ bool $ updateQty = true
579
+ ): array {
580
+ /** @var FormKey $formKey */
581
+ $ formKey = Bootstrap::getObjectManager ()->get (FormKey::class);
539
582
540
583
$ request = [
541
584
'cart ' => [
542
- $ item1 ->getId () => ['qty ' => $ originalQuantity ],
543
- $ item2 ->getId () => ['qty ' => $ updatedQuantity ]
585
+ $ item1 ->getId () => ['qty ' => $ qty1 ],
586
+ $ item2 ->getId () => ['qty ' => $ qty2 ]
544
587
],
545
588
'update_cart_action ' => 'update_qty ' ,
546
589
'form_key ' => $ formKey ->getFormKey (),
547
590
];
548
591
$ this ->getRequest ()->setMethod (HttpRequest::METHOD_POST );
549
592
$ this ->getRequest ()->setPostValue ($ request );
550
- $ this ->dispatch ('checkout/cart/updatePost ' );
593
+ if ($ updateQty ) {
594
+ $ this ->dispatch ('checkout/cart/updateItemQty ' );
595
+ } else {
596
+ $ this ->dispatch ('checkout/cart/updatePost ' );
597
+ }
551
598
$ response = $ this ->getResponse ()->getBody ();
552
599
$ response = json_decode ($ response , true );
553
-
554
- $ this ->assertContains ('[{"error":"There are no source items with the in stock status","itemId": ' .$ item1 ->getId ().'}] ' , $ response );
555
-
556
600
$ quote ->collectTotals ();
557
-
558
- $ this ->assertEquals (
559
- $ quote ->getItemsQty (),
560
- $ originalQuantity + $ updatedQuantity ,
561
- "Precondition failed: invalid quote item quantity "
562
- );
601
+ return $ response ;
563
602
}
564
603
}
0 commit comments