77
88use Magento \Framework \App \ResourceConnection ;
99use Magento \Framework \DB \Adapter \AdapterInterface ;
10+ use Magento \Quote \Model \Quote ;
11+ use Magento \Quote \Model \Quote \Address ;
12+ use Magento \Quote \Model \QuoteIdMask ;
1013
1114/**
1215 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -95,6 +98,7 @@ public function testSavePaymentInformationAndPlaceOrder()
95989699 $ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
97100 $ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
101+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
98102
99103 $ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
100104
@@ -118,10 +122,6 @@ public function testSavePaymentInformationAndPlaceOrder()
118122 ->willReturn ($ adapterMockForCheckout );
119123 $ adapterMockForCheckout ->expects ($ this ->once ())->method ('beginTransaction ' );
120124 $ adapterMockForCheckout ->expects ($ this ->once ())->method ('commit ' );
121-
122- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
123- ->method ('assign ' )
124- ->with ($ cartId , $ billingAddressMock );
125125 $ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
126126 $ this ->cartManagementMock ->expects ($ this ->once ())->method ('placeOrder ' )->with ($ cartId )->willReturn ($ orderId );
127127
@@ -142,6 +142,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
142142 $ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
143143 $ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
144144
145+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
145146 $ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
146147
147148 $ adapterMockForSales = $ this ->getMockBuilder (AdapterInterface::class)
@@ -164,10 +165,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
164165 ->willReturn ($ adapterMockForCheckout );
165166 $ adapterMockForCheckout ->expects ($ this ->once ())->method ('beginTransaction ' );
166167 $ adapterMockForCheckout ->expects ($ this ->once ())->method ('rollback ' );
167-
168- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
169- ->method ('assign ' )
170- ->with ($ cartId , $ billingAddressMock );
168+
171169 $ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
172170 $ exception = new \Exception (__ ('DB exception ' ));
173171 $ this ->cartManagementMock ->expects ($ this ->once ())->method ('placeOrder ' )->willThrowException ($ exception );
@@ -181,11 +179,9 @@ public function testSavePaymentInformation()
181179182180 $ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
183181 $ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
182+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
184183 $ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
185184
186- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
187- ->method ('assign ' )
188- ->with ($ cartId , $ billingAddressMock );
189185 $ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
190186
191187 $ this ->assertTrue ($ this ->model ->savePaymentInformation ($ cartId , $ email , $ paymentMock , $ billingAddressMock ));
@@ -197,13 +193,13 @@ public function testSavePaymentInformationWithoutBillingAddress()
197193198194 $ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
199195 $ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
200- $ quoteMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
196+ $ quoteMock = $ this ->createMock (Quote::class);
201197
202198 $ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
203199
204200 $ this ->billingAddressManagementMock ->expects ($ this ->never ())->method ('assign ' );
205201 $ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
206- $ quoteIdMaskMock = $ this ->createPartialMock (\ Magento \ Quote \ Model \ QuoteIdMask::class, ['getQuoteId ' , 'load ' ]);
202+ $ quoteIdMaskMock = $ this ->createPartialMock (QuoteIdMask::class, ['getQuoteId ' , 'load ' ]);
207203 $ this ->quoteIdMaskFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ quoteIdMaskMock );
208204 $ quoteIdMaskMock ->expects ($ this ->once ())->method ('load ' )->with ($ cartId , 'masked_id ' )->willReturnSelf ();
209205 $ quoteIdMaskMock ->expects ($ this ->once ())->method ('getQuoteId ' )->willReturn ($ cartId );
@@ -224,6 +220,15 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
224220 $ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
225221 $ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
226222
223+ $ quoteMock = $ this ->createMock (Quote::class);
224+ $ quoteMock ->method ('getBillingAddress ' )->willReturn ($ billingAddressMock );
225+ $ this ->cartRepositoryMock ->method ('getActive ' )->with ($ cartId )->willReturn ($ quoteMock );
226+
227+ $ quoteIdMask = $ this ->createPartialMock (QuoteIdMask::class, ['getQuoteId ' , 'load ' ]);
228+ $ this ->quoteIdMaskFactoryMock ->method ('create ' )->willReturn ($ quoteIdMask );
229+ $ quoteIdMask ->method ('load ' )->with ($ cartId , 'masked_id ' )->willReturnSelf ();
230+ $ quoteIdMask ->method ('getQuoteId ' )->willReturn ($ cartId );
231+
227232 $ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
228233
229234 $ adapterMockForSales = $ this ->getMockBuilder (AdapterInterface::class)
@@ -246,10 +251,7 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
246251 ->willReturn ($ adapterMockForCheckout );
247252 $ adapterMockForCheckout ->expects ($ this ->once ())->method ('beginTransaction ' );
248253 $ adapterMockForCheckout ->expects ($ this ->once ())->method ('rollback ' );
249-
250- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
251- ->method ('assign ' )
252- ->with ($ cartId , $ billingAddressMock );
254+
253255 $ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
254256 $ phrase = new \Magento \Framework \Phrase (__ ('DB exception ' ));
255257 $ exception = new \Magento \Framework \Exception \LocalizedException ($ phrase );
@@ -258,4 +260,55 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
258260
259261 $ this ->model ->savePaymentInformationAndPlaceOrder ($ cartId , $ email , $ paymentMock , $ billingAddressMock );
260262 }
263+
264+ /**
265+ * @param int $cartId
266+ * @param \PHPUnit_Framework_MockObject_MockObject $billingAddressMock
267+ * @return void
268+ */
269+ private function getMockForAssignBillingAddress ($ cartId , $ billingAddressMock )
270+ {
271+ $ quoteIdMask = $ this ->createPartialMock (QuoteIdMask::class, ['getQuoteId ' , 'load ' ]);
272+ $ this ->quoteIdMaskFactoryMock ->method ('create ' )
273+ ->willReturn ($ quoteIdMask );
274+ $ quoteIdMask ->method ('load ' )
275+ ->with ($ cartId , 'masked_id ' )
276+ ->willReturnSelf ();
277+ $ quoteIdMask ->method ('getQuoteId ' )
278+ ->willReturn ($ cartId );
279+
280+ $ billingAddressId = 1 ;
281+ $ quote = $ this ->createMock (Quote::class);
282+ $ quoteBillingAddress = $ this ->createMock (Address::class);
283+ $ quoteShippingAddress = $ this ->createPartialMock (
284+ Address::class,
285+ ['setLimitCarrier ' , 'getShippingMethod ' ]
286+ );
287+ $ this ->cartRepositoryMock ->method ('getActive ' )
288+ ->with ($ cartId )
289+ ->willReturn ($ quote );
290+ $ quote ->expects ($ this ->once ())
291+ ->method ('getBillingAddress ' )
292+ ->willReturn ($ quoteBillingAddress );
293+ $ quote ->expects ($ this ->once ())
294+ ->method ('getShippingAddress ' )
295+ ->willReturn ($ quoteShippingAddress );
296+ $ quoteBillingAddress ->expects ($ this ->once ())
297+ ->method ('getId ' )
298+ ->willReturn ($ billingAddressId );
299+ $ quote ->expects ($ this ->once ())
300+ ->method ('removeAddress ' )
301+ ->with ($ billingAddressId );
302+ $ quote ->expects ($ this ->once ())
303+ ->method ('setBillingAddress ' )
304+ ->with ($ billingAddressMock );
305+ $ quote ->expects ($ this ->once ())
306+ ->method ('setDataChanges ' )
307+ ->willReturnSelf ();
308+ $ quoteShippingAddress ->method ('getShippingMethod ' )
309+ ->willReturn ('flatrate_flatrate ' );
310+ $ quoteShippingAddress ->expects ($ this ->once ())
311+ ->method ('setLimitCarrier ' )
312+ ->with ('flatrate ' );
313+ }
261314}
0 commit comments