@@ -63,6 +63,21 @@ class ShippingMethodManagementTest extends \PHPUnit_Framework_TestCase
6363 */
6464 private $ shippingAddress ;
6565
66+ /**
67+ * @var \Magento\Framework\Reflection\DataObjectProcessor|MockObject
68+ */
69+ private $ dataProcessor ;
70+
71+ /**
72+ * @var \Magento\Customer\Api\Data\AddressInterfaceFactory|MockObject
73+ */
74+ private $ addressFactory ;
75+
76+ /**
77+ * @var \Magento\Customer\Api\AddressRepositoryInterface|MockObject
78+ */
79+ private $ addressRepository ;
80+
6681 /**
6782 * @var TotalsCollector|MockObject
6883 */
@@ -72,16 +87,17 @@ protected function setUp()
7287 {
7388 $ this ->objectManager = new ObjectManager ($ this );
7489 $ this ->quoteRepository = $ this ->getMock (\Magento \Quote \Api \CartRepositoryInterface::class);
75- $ this ->methodDataFactoryMock = $ this ->getMock (
76- \Magento \Quote \Api \Data \ShippingMethodInterfaceFactory::class,
77- [
78- 'create '
79- ],
80- [],
81- '' ,
82- false
83- );
90+ $ this ->addressRepository = $ this ->getMock (\Magento \Customer \Api \AddressRepositoryInterface::class);
91+
92+ $ className = \Magento \Quote \Api \Data \ShippingMethodInterfaceFactory::class;
93+ $ this ->methodDataFactoryMock = $ this ->getMock ($ className , ['create ' ], [], '' , false );
8494
95+ $ className = \Magento \Customer \Api \Data \AddressInterfaceFactory::class;
96+ $ this ->addressFactory = $ this ->getMock ($ className , ['create ' ], [], '' , false );
97+
98+ $ className = \Magento \Framework \Reflection \DataObjectProcessor::class;
99+ $ this ->dataProcessor = $ this ->getMock ($ className , [], [], '' , false );
100+
85101 $ this ->storeMock = $ this ->getMock (\Magento \Store \Model \Store::class, [], [], '' , false );
86102 $ this ->quote = $ this ->getMockBuilder (Quote::class)
87103 ->disableOriginalConstructor ()
@@ -132,9 +148,22 @@ protected function setUp()
132148 'quoteRepository ' => $ this ->quoteRepository ,
133149 'methodDataFactory ' => $ this ->methodDataFactoryMock ,
134150 'converter ' => $ this ->converter ,
135- 'totalsCollector ' => $ this ->totalsCollector
151+ 'totalsCollector ' => $ this ->totalsCollector ,
152+ 'addressRepository ' => $ this ->addressRepository
136153 ]
137154 );
155+
156+ $ this ->objectManager ->setBackwardCompatibleProperty (
157+ $ this ->model ,
158+ 'addressFactory ' ,
159+ $ this ->addressFactory
160+ );
161+
162+ $ this ->objectManager ->setBackwardCompatibleProperty (
163+ $ this ->model ,
164+ 'dataProcessor ' ,
165+ $ this ->dataProcessor
166+ );
138167 }
139168
140169 /**
@@ -457,11 +486,17 @@ public function testEstimateByExtendedAddress()
457486 ];
458487 $ currencyCode = 'UAH ' ;
459488
489+ /**
490+ * @var \Magento\Quote\Api\Data\AddressInterface|MockObject $address
491+ */
460492 $ address = $ this ->getMockBuilder (Address::class)
461493 ->disableOriginalConstructor ()
462- ->setMethods (['getData ' ])
463494 ->getMock ();
464495
496+ $ this ->addressFactory ->expects ($ this ->any ())
497+ ->method ('create ' )
498+ ->will ($ this ->returnValue ($ address ));
499+
465500 $ this ->quoteRepository ->expects (static ::once ())
466501 ->method ('getActive ' )
467502 ->with ($ cartId )
@@ -474,18 +509,98 @@ public function testEstimateByExtendedAddress()
474509 ->method ('getItemsCount ' )
475510 ->willReturn (1 );
476511
477- $ address ->expects (static ::once ())
478- ->method ('getData ' )
479- ->willReturn ($ addressData );
480-
481512 $ this ->quote ->expects (static ::once ())
482513 ->method ('getShippingAddress ' )
483514 ->willReturn ($ this ->shippingAddress );
484515
516+ $ this ->dataProcessor ->expects (static ::any ())
517+ ->method ('buildOutputDataArray ' )
518+ ->willReturn ($ addressData );
519+
485520 $ this ->shippingAddress ->expects (static ::once ())
486- ->method ('addData ' )
487- ->with ($ addressData )
521+ ->method ('setCollectShippingRates ' )
522+ ->with (true )
523+ ->willReturnSelf ();
524+
525+ $ this ->totalsCollector ->expects (static ::once ())
526+ ->method ('collectAddressTotals ' )
527+ ->with ($ this ->quote , $ this ->shippingAddress )
488528 ->willReturnSelf ();
529+
530+ $ rate = $ this ->getMockBuilder (Rate::class)
531+ ->disableOriginalConstructor ()
532+ ->setMethods ([])
533+ ->getMock ();
534+ $ methodObject = $ this ->getMockForAbstractClass (ShippingMethodInterface::class);
535+ $ expectedRates = [$ methodObject ];
536+
537+ $ this ->shippingAddress ->expects (static ::once ())
538+ ->method ('getGroupedAllShippingRates ' )
539+ ->willReturn ([[$ rate ]]);
540+
541+ $ this ->quote ->expects (static ::once ())
542+ ->method ('getQuoteCurrencyCode ' )
543+ ->willReturn ($ currencyCode );
544+
545+ $ this ->converter ->expects (static ::once ())
546+ ->method ('modelToDataObject ' )
547+ ->with ($ rate , $ currencyCode )
548+ ->willReturn ($ methodObject );
549+
550+ $ carriersRates = $ this ->model ->estimateByExtendedAddress ($ cartId , $ address );
551+ static ::assertEquals ($ expectedRates , $ carriersRates );
552+ }
553+
554+ /**
555+ * @covers \Magento\Quote\Model\ShippingMethodManagement::estimateByAddressId
556+ */
557+ public function testEstimateByAddressId ()
558+ {
559+ $ cartId = 1 ;
560+
561+ $ addressData = [
562+ 'region ' => 'California ' ,
563+ 'region_id ' => 23 ,
564+ 'country_id ' => 1 ,
565+ 'postcode ' => 90200
566+ ];
567+ $ currencyCode = 'UAH ' ;
568+
569+ /**
570+ * @var \Magento\Customer\Api\Data\AddressInterface|MockObject $address
571+ */
572+ $ address = $ this ->getMockBuilder (\Magento \Customer \Api \Data \AddressInterface::class)
573+ ->disableOriginalConstructor ()
574+ ->getMock ();
575+
576+ $ this ->addressRepository ->expects ($ this ->any ())
577+ ->method ('getById ' )
578+ ->will ($ this ->returnValue ($ address ));
579+
580+ $ this ->addressFactory ->expects ($ this ->any ())
581+ ->method ('create ' )
582+ ->will ($ this ->returnValue ($ address ));
583+
584+ $ this ->quoteRepository ->expects (static ::once ())
585+ ->method ('getActive ' )
586+ ->with ($ cartId )
587+ ->willReturn ($ this ->quote );
588+
589+ $ this ->quote ->expects (static ::once ())
590+ ->method ('isVirtual ' )
591+ ->willReturn (false );
592+ $ this ->quote ->expects (static ::once ())
593+ ->method ('getItemsCount ' )
594+ ->willReturn (1 );
595+
596+ $ this ->quote ->expects (static ::once ())
597+ ->method ('getShippingAddress ' )
598+ ->willReturn ($ this ->shippingAddress );
599+
600+ $ this ->dataProcessor ->expects (static ::any ())
601+ ->method ('buildOutputDataArray ' )
602+ ->willReturn ($ addressData );
603+
489604 $ this ->shippingAddress ->expects (static ::once ())
490605 ->method ('setCollectShippingRates ' )
491606 ->with (true )
@@ -516,7 +631,7 @@ public function testEstimateByExtendedAddress()
516631 ->with ($ rate , $ currencyCode )
517632 ->willReturn ($ methodObject );
518633
519- $ carriersRates = $ this ->model ->estimateByExtendedAddress ($ cartId , $ address );
634+ $ carriersRates = $ this ->model ->estimateByAddressId ($ cartId , $ address );
520635 static ::assertEquals ($ expectedRates , $ carriersRates );
521636 }
522637}
0 commit comments