7
7
8
8
use Magento \Framework \App \ResourceConnection ;
9
9
use Magento \Framework \DB \Adapter \AdapterInterface ;
10
+ use Magento \Quote \Model \Quote ;
11
+ use Magento \Quote \Model \Quote \Address ;
12
+ use Magento \Quote \Model \QuoteIdMask ;
10
13
11
14
/**
12
15
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -95,6 +98,7 @@ public function testSavePaymentInformationAndPlaceOrder()
95
98
$ email = 'email@magento.com ' ;
96
99
$ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
97
100
$ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
101
+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
98
102
99
103
$ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
100
104
@@ -118,10 +122,6 @@ public function testSavePaymentInformationAndPlaceOrder()
118
122
->willReturn ($ adapterMockForCheckout );
119
123
$ adapterMockForCheckout ->expects ($ this ->once ())->method ('beginTransaction ' );
120
124
$ adapterMockForCheckout ->expects ($ this ->once ())->method ('commit ' );
121
-
122
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
123
- ->method ('assign ' )
124
- ->with ($ cartId , $ billingAddressMock );
125
125
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
126
126
$ this ->cartManagementMock ->expects ($ this ->once ())->method ('placeOrder ' )->with ($ cartId )->willReturn ($ orderId );
127
127
@@ -142,6 +142,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
142
142
$ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
143
143
$ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
144
144
145
+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
145
146
$ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
146
147
147
148
$ adapterMockForSales = $ this ->getMockBuilder (AdapterInterface::class)
@@ -164,10 +165,7 @@ public function testSavePaymentInformationAndPlaceOrderException()
164
165
->willReturn ($ adapterMockForCheckout );
165
166
$ adapterMockForCheckout ->expects ($ this ->once ())->method ('beginTransaction ' );
166
167
$ adapterMockForCheckout ->expects ($ this ->once ())->method ('rollback ' );
167
-
168
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
169
- ->method ('assign ' )
170
- ->with ($ cartId , $ billingAddressMock );
168
+
171
169
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
172
170
$ exception = new \Exception (__ ('DB exception ' ));
173
171
$ this ->cartManagementMock ->expects ($ this ->once ())->method ('placeOrder ' )->willThrowException ($ exception );
@@ -181,11 +179,9 @@ public function testSavePaymentInformation()
181
179
$ email = 'email@magento.com ' ;
182
180
$ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
183
181
$ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
182
+ $ this ->getMockForAssignBillingAddress ($ cartId , $ billingAddressMock );
184
183
$ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
185
184
186
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
187
- ->method ('assign ' )
188
- ->with ($ cartId , $ billingAddressMock );
189
185
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
190
186
191
187
$ this ->assertTrue ($ this ->model ->savePaymentInformation ($ cartId , $ email , $ paymentMock , $ billingAddressMock ));
@@ -197,13 +193,13 @@ public function testSavePaymentInformationWithoutBillingAddress()
197
193
$ email = 'email@magento.com ' ;
198
194
$ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
199
195
$ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
200
- $ quoteMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
196
+ $ quoteMock = $ this ->createMock (Quote::class);
201
197
202
198
$ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
203
199
204
200
$ this ->billingAddressManagementMock ->expects ($ this ->never ())->method ('assign ' );
205
201
$ 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 ' ]);
207
203
$ this ->quoteIdMaskFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ quoteIdMaskMock );
208
204
$ quoteIdMaskMock ->expects ($ this ->once ())->method ('load ' )->with ($ cartId , 'masked_id ' )->willReturnSelf ();
209
205
$ quoteIdMaskMock ->expects ($ this ->once ())->method ('getQuoteId ' )->willReturn ($ cartId );
@@ -224,6 +220,15 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
224
220
$ paymentMock = $ this ->createMock (\Magento \Quote \Api \Data \PaymentInterface::class);
225
221
$ billingAddressMock = $ this ->createMock (\Magento \Quote \Api \Data \AddressInterface::class);
226
222
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
+
227
232
$ billingAddressMock ->expects ($ this ->once ())->method ('setEmail ' )->with ($ email )->willReturnSelf ();
228
233
229
234
$ adapterMockForSales = $ this ->getMockBuilder (AdapterInterface::class)
@@ -246,10 +251,7 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
246
251
->willReturn ($ adapterMockForCheckout );
247
252
$ adapterMockForCheckout ->expects ($ this ->once ())->method ('beginTransaction ' );
248
253
$ adapterMockForCheckout ->expects ($ this ->once ())->method ('rollback ' );
249
-
250
- $ this ->billingAddressManagementMock ->expects ($ this ->once ())
251
- ->method ('assign ' )
252
- ->with ($ cartId , $ billingAddressMock );
254
+
253
255
$ this ->paymentMethodManagementMock ->expects ($ this ->once ())->method ('set ' )->with ($ cartId , $ paymentMock );
254
256
$ phrase = new \Magento \Framework \Phrase (__ ('DB exception ' ));
255
257
$ exception = new \Magento \Framework \Exception \LocalizedException ($ phrase );
@@ -258,4 +260,55 @@ public function testSavePaymentInformationAndPlaceOrderWithLocolizedException()
258
260
259
261
$ this ->model ->savePaymentInformationAndPlaceOrder ($ cartId , $ email , $ paymentMock , $ billingAddressMock );
260
262
}
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
+ }
261
314
}
0 commit comments