1111use Magento \Checkout \Api \Data \ShippingInformationInterface ;
1212use Magento \Checkout \Api \ShippingInformationManagementInterface ;
1313use Magento \Checkout \Model \GuestShippingInformationManagement ;
14- use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
14+ use Magento \Customer \Model \Address ;
15+ use Magento \Customer \Model \AddressFactory ;
16+ use Magento \Framework \Exception \InputException ;
17+ use Magento \Framework \Validator \Factory as ValidatorFactory ;
18+ use Magento \Framework \Validator \ValidatorInterface ;
19+ use Magento \Quote \Api \Data \AddressInterface ;
1520use Magento \Quote \Model \QuoteIdMask ;
1621use Magento \Quote \Model \QuoteIdMaskFactory ;
1722use PHPUnit \Framework \MockObject \MockObject ;
1823use PHPUnit \Framework \TestCase ;
1924
25+ /**
26+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27+ */
2028class GuestShippingInformationManagementTest extends TestCase
2129{
2230 /**
23- * @var MockObject
31+ * @var ShippingInformationManagementInterface| MockObject
2432 */
2533 protected $ shippingInformationManagementMock ;
2634
2735 /**
28- * @var MockObject
36+ * @var QuoteIdMaskFactory| MockObject
2937 */
3038 protected $ quoteIdMaskFactoryMock ;
3139
40+ /**
41+ * @var ValidatorFactory|MockObject
42+ */
43+ protected $ validatorFactoryMock ;
44+
45+ /**
46+ * @var AddressFactory|MockObject
47+ */
48+ protected $ addressFactoryMock ;
49+
3250 /**
3351 * @var GuestShippingInformationManagement
3452 */
3553 protected $ model ;
3654
3755 protected function setUp (): void
3856 {
39- $ objectManager = new ObjectManager ($ this );
4057 $ this ->quoteIdMaskFactoryMock = $ this ->createPartialMock (
4158 QuoteIdMaskFactory::class,
4259 ['create ' ]
4360 );
4461 $ this ->shippingInformationManagementMock = $ this ->createMock (
4562 ShippingInformationManagementInterface::class
4663 );
47-
48- $ this ->model = $ objectManager -> getObject (
49- GuestShippingInformationManagement::class,
50- [
51- ' quoteIdMaskFactory ' => $ this ->quoteIdMaskFactoryMock ,
52- ' shippingInformationManagement ' => $ this ->shippingInformationManagementMock
53- ]
64+ $ this -> validatorFactoryMock = $ this -> createMock (ValidatorFactory::class);
65+ $ this ->addressFactoryMock = $ this -> createMock (AddressFactory::class);
66+ $ this -> model = new GuestShippingInformationManagement (
67+ $ this -> quoteIdMaskFactoryMock ,
68+ $ this ->shippingInformationManagementMock ,
69+ $ this ->validatorFactoryMock ,
70+ $ this -> addressFactoryMock
5471 );
5572 }
5673
@@ -59,17 +76,34 @@ public function testSaveAddressInformation()
5976 $ cartId = 'masked_id ' ;
6077 $ quoteId = '100 ' ;
6178 $ addressInformationMock = $ this ->getMockForAbstractClass (ShippingInformationInterface::class);
62-
79+ $ shippingAddressMock = $ this ->getMockForAbstractClass (AddressInterface::class);
80+ $ addressInformationMock ->expects ($ this ->once ())
81+ ->method ('getShippingAddress ' )
82+ ->willReturn ($ shippingAddressMock );
83+ $ shippingAddressMock ->expects ($ this ->once ())
84+ ->method ('getExtensionAttributes ' )
85+ ->willReturn (null );
86+ $ customerAddressMock = $ this ->createMock (Address::class);
87+ $ this ->addressFactoryMock ->expects ($ this ->once ())
88+ ->method ('create ' )
89+ ->willReturn ($ customerAddressMock );
90+ $ validatorMock = $ this ->createMock (ValidatorInterface::class);
91+ $ this ->validatorFactoryMock ->expects ($ this ->once ())
92+ ->method ('createValidator ' )
93+ ->with ('customer_address ' , 'save ' )
94+ ->willReturn ($ validatorMock );
95+ $ validatorMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (true );
6396 $ quoteIdMaskMock = $ this ->getMockBuilder (QuoteIdMask::class)
6497 ->addMethods (['getQuoteId ' ])
6598 ->onlyMethods (['load ' ])
6699 ->disableOriginalConstructor ()
67100 ->getMock ();
68101 $ this ->quoteIdMaskFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ quoteIdMaskMock );
69-
70- $ quoteIdMaskMock ->expects ($ this ->once ())->method ('load ' )->with ($ cartId , 'masked_id ' )->willReturnSelf ();
102+ $ quoteIdMaskMock ->expects ($ this ->once ())
103+ ->method ('load ' )
104+ ->with ($ cartId , 'masked_id ' )
105+ ->willReturnSelf ();
71106 $ quoteIdMaskMock ->expects ($ this ->once ())->method ('getQuoteId ' )->willReturn ($ quoteId );
72-
73107 $ paymentInformationMock = $ this ->getMockForAbstractClass (PaymentDetailsInterface::class);
74108 $ this ->shippingInformationManagementMock ->expects ($ this ->once ())
75109 ->method ('saveAddressInformation ' )
@@ -78,7 +112,39 @@ public function testSaveAddressInformation()
78112 $ addressInformationMock
79113 )
80114 ->willReturn ($ paymentInformationMock );
115+ $ this ->model ->saveAddressInformation ($ cartId , $ addressInformationMock );
116+ }
81117
118+ /**
119+ * Validate save address information when it is invalid
120+ *
121+ * @return void
122+ * @throws \PHPUnit\Framework\MockObject\Exception
123+ */
124+ public function testSaveAddressInformationWithInvalidAddress ()
125+ {
126+ $ cartId = 'masked_id ' ;
127+ $ addressInformationMock = $ this ->getMockForAbstractClass (ShippingInformationInterface::class);
128+ $ shippingAddressMock = $ this ->getMockForAbstractClass (AddressInterface::class);
129+ $ addressInformationMock ->expects ($ this ->once ())
130+ ->method ('getShippingAddress ' )
131+ ->willReturn ($ shippingAddressMock );
132+ $ shippingAddressMock ->method ('getExtensionAttributes ' )->willReturn (null );
133+ $ customerAddressMock = $ this ->createMock (Address::class);
134+ $ this ->addressFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customerAddressMock );
135+ $ validatorMock = $ this ->createMock (ValidatorInterface::class);
136+ $ this ->validatorFactoryMock ->expects ($ this ->once ())
137+ ->method ('createValidator ' )
138+ ->with ('customer_address ' , 'save ' )
139+ ->willReturn ($ validatorMock );
140+ $ validatorMock ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
141+ $ validatorMock ->expects ($ this ->once ())
142+ ->method ('getMessages ' )
143+ ->willReturn (['First Name is not valid! ' , 'Last Name is not valid! ' ]);
144+ $ this ->expectException (InputException::class);
145+ $ this ->expectExceptionMessage (
146+ 'The shipping address contains invalid data: First Name is not valid!, Last Name is not valid! '
147+ );
82148 $ this ->model ->saveAddressInformation ($ cartId , $ addressInformationMock );
83149 }
84150}
0 commit comments