99
1010use Magento \Checkout \Api \Data \PaymentDetailsInterface ;
1111use Magento \Checkout \Api \Data \ShippingInformationInterface ;
12+ use Magento \Checkout \Model \AddressComparatorInterface ;
1213use Magento \Checkout \Model \PaymentDetailsFactory ;
1314use Magento \Checkout \Model \ShippingInformationManagement ;
1415use Magento \Framework \Exception \InputException ;
2728use Magento \Quote \Model \Quote ;
2829use Magento \Quote \Model \Quote \Address ;
2930use Magento \Quote \Model \QuoteAddressValidator ;
31+ use Magento \Quote \Model \QuoteAddressValidationService ;
3032use Magento \Quote \Model \Shipping ;
3133use Magento \Quote \Model \ShippingAssignment ;
3234use Magento \Quote \Model \ShippingAssignmentFactory ;
@@ -123,6 +125,16 @@ class ShippingInformationManagementTest extends TestCase
123125 */
124126 private $ addressValidatorMock ;
125127
128+ /**
129+ * @var QuoteAddressValidationService|MockObject
130+ */
131+ private $ quoteAddressValidationServiceMock ;
132+
133+ /**
134+ * @var AddressComparatorInterface|MockObject
135+ */
136+ private $ addressComparatorMock ;
137+
126138 /**
127139 * @inheritdoc
128140 */
@@ -185,6 +197,9 @@ protected function setUp(): void
185197 );
186198 $ this ->shippingFactoryMock = $ this ->createPartialMock (ShippingFactory::class, ['create ' ]);
187199 $ this ->addressValidatorMock = $ this ->createMock (QuoteAddressValidator::class);
200+ $ this ->quoteAddressValidationServiceMock = $ this ->createMock (QuoteAddressValidationService::class);
201+ $ this ->addressComparatorMock = $ this ->getMockForAbstractClass (AddressComparatorInterface::class);
202+ $ this ->addressComparatorMock ->method ('isEqual ' )->willReturn (false );
188203
189204 $ this ->model = $ this ->objectManager ->getObject (
190205 ShippingInformationManagement::class,
@@ -196,7 +211,9 @@ protected function setUp(): void
196211 'shippingAssignmentFactory ' => $ this ->shippingAssignmentFactoryMock ,
197212 'cartExtensionFactory ' => $ this ->cartExtensionFactoryMock ,
198213 'shippingFactory ' => $ this ->shippingFactoryMock ,
199- 'addressValidator ' => $ this ->addressValidatorMock
214+ 'addressValidator ' => $ this ->addressValidatorMock ,
215+ 'addressComparator ' => $ this ->addressComparatorMock ,
216+ 'quoteAddressValidationService ' => $ this ->quoteAddressValidationServiceMock
200217 ]
201218 );
202219 }
@@ -336,6 +353,9 @@ public function testSaveAddressInformationWithLocalizedException(): void
336353 $ this ->addressValidatorMock ->expects ($ this ->exactly (2 ))
337354 ->method ('validateForCart ' );
338355
356+ $ this ->quoteAddressValidationServiceMock ->expects ($ this ->once ())
357+ ->method ('validateAddressesWithRules ' );
358+
339359 $ this ->quoteRepositoryMock ->expects ($ this ->once ())
340360 ->method ('getActive ' )
341361 ->with ($ cartId )
@@ -355,6 +375,7 @@ public function testSaveAddressInformationWithLocalizedException(): void
355375 $ billingAddress ->expects ($ this ->once ())
356376 ->method ('getCustomerAddressId ' )
357377 ->willReturn (1 );
378+ $ billingAddress ->method ('setCustomerAddressId ' )->willReturnSelf ();
358379
359380 $ addressInformationMock ->expects ($ this ->once ())
360381 ->method ('getBillingAddress ' )
@@ -377,19 +398,23 @@ public function testSaveAddressInformationWithLocalizedException(): void
377398 ->method ('setBillingAddress ' )
378399 ->with ($ billingAddress )
379400 ->willReturnSelf ();
380- $ this ->quoteMock ->expects ($ this ->once ())
401+
402+ $ quoteBillingAddressMock = $ this ->getMockForAbstractClass (AddressInterface::class);
403+ $ quoteBillingAddressMock ->method ('getCustomerAddressId ' )->willReturn (null );
404+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ())
381405 ->method ('getBillingAddress ' )
382- ->willReturnSelf ();
383- $ this ->quoteMock ->expects ($ this ->once ())
406+ ->willReturn ($ quoteBillingAddressMock );
407+
408+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ())
384409 ->method ('getShippingAddress ' )
385- ->willReturnSelf ( );
410+ ->willReturn ( $ this -> shippingAddressMock );
386411
387412 $ this ->quoteRepositoryMock ->expects ($ this ->once ())
388413 ->method ('save ' )
389414 ->with ($ this ->quoteMock )
390415 ->willThrowException ($ exception );
391416
392- $ this ->expectException (LocalizedException ::class);
417+ $ this ->expectException (InputException ::class);
393418 $ this ->expectExceptionMessage (
394419 'The shipping information was unable to be saved. Error: " ' . $ errorMessage . '" '
395420 );
@@ -428,6 +453,8 @@ public function testSaveAddressInformationIfCanNotSaveQuote(): void
428453 ->willReturn ($ shippingMethod );
429454
430455 $ billingAddress = $ this ->getMockForAbstractClass (AddressInterface::class);
456+ $ billingAddress ->method ('getCustomerAddressId ' )->willReturn (null );
457+ $ billingAddress ->method ('setCustomerAddressId ' )->willReturnSelf ();
431458 $ addressInformationMock ->expects ($ this ->once ())
432459 ->method ('getBillingAddress ' )
433460 ->willReturn ($ billingAddress );
@@ -450,12 +477,13 @@ public function testSaveAddressInformationIfCanNotSaveQuote(): void
450477 ->willReturnSelf ();
451478
452479 $ quoteBillingAddress = $ this ->createMock (Address::class);
453- $ this ->quoteMock ->expects ($ this ->once ())
480+ $ quoteBillingAddress ->method ('getCustomerAddressId ' )->willReturn (null );
481+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ())
454482 ->method ('getBillingAddress ' )
455483 ->willReturn ($ quoteBillingAddress );
456484
457485 $ quoteShippingAddress = $ this ->createMock (Address::class);
458- $ this ->quoteMock ->expects ($ this ->once ())
486+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ())
459487 ->method ('getShippingAddress ' )
460488 ->willReturn ($ quoteShippingAddress );
461489
@@ -505,6 +533,7 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid(): void
505533 $ billingAddress ->expects ($ this ->once ())
506534 ->method ('getCustomerAddressId ' )
507535 ->willReturn (1 );
536+ $ billingAddress ->method ('setCustomerAddressId ' )->willReturnSelf ();
508537
509538 $ addressInformationMock ->expects ($ this ->once ())
510539 ->method ('getBillingAddress ' )
@@ -518,9 +547,12 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid(): void
518547 $ this ->quoteMock ->expects ($ this ->once ())
519548 ->method ('getItemsCount ' )
520549 ->willReturn (self ::STUB_ITEMS_COUNT );
521- $ this ->quoteMock ->expects ($ this ->once ())
550+
551+ $ quoteBillingAddressMock = $ this ->getMockForAbstractClass (AddressInterface::class);
552+ $ quoteBillingAddressMock ->method ('getCustomerAddressId ' )->willReturn (null );
553+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ())
522554 ->method ('getBillingAddress ' )
523- ->willReturnSelf ( );
555+ ->willReturn ( $ quoteBillingAddressMock );
524556
525557 $ this ->quoteMock ->expects ($ this ->once ())
526558 ->method ('setIsMultiShipping ' )
@@ -530,7 +562,8 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid(): void
530562 ->method ('setBillingAddress ' )
531563 ->with ($ billingAddress )
532564 ->willReturnSelf ();
533- $ this ->quoteMock ->expects ($ this ->exactly (2 ))
565+
566+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ())
534567 ->method ('getShippingAddress ' )
535568 ->willReturn ($ this ->shippingAddressMock );
536569
@@ -558,6 +591,7 @@ public function testSaveAddressInformationIfCarrierCodeIsInvalid(): void
558591 * Save address info test.
559592 *
560593 * @return void
594+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
561595 */
562596 public function testSaveAddressInformation (): void
563597 {
@@ -585,6 +619,8 @@ public function testSaveAddressInformation(): void
585619 ->willReturn ($ shippingMethod );
586620
587621 $ billingAddress = $ this ->getMockForAbstractClass (AddressInterface::class);
622+ $ billingAddress ->method ('getCustomerAddressId ' )->willReturn (null );
623+ $ billingAddress ->method ('setCustomerAddressId ' )->willReturnSelf ();
588624 $ addressInformationMock ->expects ($ this ->once ())
589625 ->method ('getBillingAddress ' )
590626 ->willReturn ($ billingAddress );
@@ -597,9 +633,12 @@ public function testSaveAddressInformation(): void
597633 $ this ->quoteMock ->expects ($ this ->once ())
598634 ->method ('getItemsCount ' )
599635 ->willReturn (self ::STUB_ITEMS_COUNT );
600- $ this ->quoteMock ->expects ($ this ->once ())
636+
637+ $ quoteBillingAddressMock = $ this ->getMockForAbstractClass (AddressInterface::class);
638+ $ quoteBillingAddressMock ->method ('getCustomerAddressId ' )->willReturn (null );
639+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ())
601640 ->method ('getBillingAddress ' )
602- ->willReturnSelf ( );
641+ ->willReturn ( $ quoteBillingAddressMock );
603642
604643 $ this ->quoteMock ->expects ($ this ->once ())
605644 ->method ('setIsMultiShipping ' )
@@ -609,7 +648,7 @@ public function testSaveAddressInformation(): void
609648 ->method ('setBillingAddress ' )
610649 ->with ($ billingAddress )
611650 ->willReturnSelf ();
612- $ this ->quoteMock ->expects ($ this ->exactly ( 2 ))
651+ $ this ->quoteMock ->expects ($ this ->atLeastOnce ( ))
613652 ->method ('getShippingAddress ' )
614653 ->willReturn ($ this ->shippingAddressMock );
615654
@@ -664,10 +703,11 @@ public function testSaveAddressInformation(): void
664703 private function getCartExtensionMock (): MockObject
665704 {
666705 $ mockBuilder = $ this ->getMockBuilder (CartExtension::class);
667- try {
706+
707+ if (method_exists (CartExtension::class, 'getShippingAssignments ' )) {
708+ $ mockBuilder ->onlyMethods (['getShippingAssignments ' , 'setShippingAssignments ' ]);
709+ } else {
668710 $ mockBuilder ->addMethods (['getShippingAssignments ' , 'setShippingAssignments ' ]);
669- } catch (RuntimeException $ e ) {
670- // CartExtension already generated.
671711 }
672712
673713 return $ mockBuilder ->getMock ();
0 commit comments