Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingInformationManagementTest.php
Go to the documentation of this file.
1 <?php
8 
13 class ShippingInformationManagementTest extends \PHPUnit\Framework\TestCase
14 {
18  private $objectManager;
19 
24 
29 
34 
39 
44 
48  protected $quoteMock;
49 
53  protected $model;
54 
58  private $shippingAssignmentFactoryMock;
59 
63  private $cartExtensionFactoryMock;
64 
68  private $shippingFactoryMock;
69 
73  private $cartExtensionMock;
74 
78  private $shippingAssignmentMock;
79 
83  private $shippingMock;
84 
85  protected function setUp()
86  {
87  $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
88  $this->paymentMethodManagementMock = $this->createMock(
89  \Magento\Quote\Api\PaymentMethodManagementInterface::class
90  );
91  $this->paymentDetailsFactoryMock = $this->createPartialMock(
92  \Magento\Checkout\Model\PaymentDetailsFactory::class,
93  ['create']
94  );
95  $this->cartTotalsRepositoryMock = $this->createMock(\Magento\Quote\Api\CartTotalRepositoryInterface::class);
96  $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
97  $this->shippingAddressMock = $this->createPartialMock(
98  \Magento\Quote\Model\Quote\Address::class,
99  [
100  'getSaveInAddressBook',
101  'getSameAsBilling',
102  'getCustomerAddressId',
103  'setShippingAddress',
104  'getShippingAddress',
105  'setSaveInAddressBook',
106  'setSameAsBilling',
107  'setCollectShippingRates',
108  'getCountryId',
109  'importCustomerAddressData',
110  'save',
111  'getShippingRateByCode',
112  'getShippingMethod',
113  'setLimitCarrier'
114  ]
115  );
116 
117  $this->quoteMock = $this->createPartialMock(
118  \Magento\Quote\Model\Quote::class,
119  [
120  'isVirtual',
121  'getItemsCount',
122  'getIsMultiShipping',
123  'setIsMultiShipping',
124  'validateMinimumAmount',
125  'getStoreId',
126  'setShippingAddress',
127  'getShippingAddress',
128  'collectTotals',
129  'getExtensionAttributes',
130  'setExtensionAttributes',
131  'setBillingAddress'
132  ],
133  [],
134  '',
135  false
136  );
137 
138  $this->shippingAssignmentFactoryMock =
139  $this->createPartialMock(\Magento\Quote\Model\ShippingAssignmentFactory::class, ['create']);
140  $this->cartExtensionFactoryMock =
141  $this->createPartialMock(\Magento\Quote\Api\Data\CartExtensionFactory::class, ['create']);
142  $this->shippingFactoryMock =
143  $this->createPartialMock(\Magento\Quote\Model\ShippingFactory::class, ['create']);
144 
145  $this->model = $this->objectManager->getObject(
146  \Magento\Checkout\Model\ShippingInformationManagement::class,
147  [
148  'paymentMethodManagement' => $this->paymentMethodManagementMock,
149  'paymentDetailsFactory' => $this->paymentDetailsFactoryMock,
150  'cartTotalsRepository' => $this->cartTotalsRepositoryMock,
151  'quoteRepository' => $this->quoteRepositoryMock,
152  'shippingAssignmentFactory' => $this->shippingAssignmentFactoryMock,
153  'cartExtensionFactory' => $this->cartExtensionFactoryMock,
154  'shippingFactory' => $this->shippingFactoryMock
155  ]
156  );
157  }
158 
164  {
165  $cartId = 100;
166  $carrierCode = 'carrier_code';
167  $shippingMethod = 'shipping_method';
168  $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
169 
170  $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
171  $addressInformationMock->expects($this->once())
172  ->method('getShippingAddress')
173  ->willReturn($this->shippingAddressMock);
174  $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
175  $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
176  $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
177 
178  $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
179 
180  $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
181 
182  $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(0);
183  $this->quoteRepositoryMock->expects($this->once())
184  ->method('getActive')
185  ->with($cartId)
186  ->willReturn($this->quoteMock);
187 
188  $this->model->saveAddressInformation($cartId, $addressInformationMock);
189  }
190 
194  private function setShippingAssignmentsMocks($shippingMethod)
195  {
196  $this->quoteMock->expects($this->once())->method('getExtensionAttributes')->willReturn(null);
197  $this->shippingAddressMock->expects($this->once())->method('setLimitCarrier');
198  $this->cartExtensionMock = $this->createPartialMock(
199  \Magento\Quote\Api\Data\CartExtension::class,
200  ['getShippingAssignments', 'setShippingAssignments']
201  );
202  $this->cartExtensionFactoryMock->expects($this->once())
203  ->method('create')
204  ->willReturn($this->cartExtensionMock);
205  $this->cartExtensionMock->expects($this->once())->method('getShippingAssignments')->willReturn(null);
206 
207  $this->shippingAssignmentMock = $this->createMock(
208  \Magento\Quote\Model\ShippingAssignment::class
209  );
210  $this->shippingAssignmentFactoryMock->expects($this->once())
211  ->method('create')
212  ->willReturn($this->shippingAssignmentMock);
213  $this->shippingAssignmentMock->expects($this->once())->method('getShipping')->willReturn(null);
214 
215  $this->shippingMock = $this->createMock(\Magento\Quote\Model\Shipping::class);
216  $this->shippingFactoryMock->expects($this->once())->method('create')->willReturn($this->shippingMock);
217 
218  $this->shippingMock->expects($this->once())
219  ->method('setAddress')
220  ->with($this->shippingAddressMock)
221  ->willReturnSelf();
222  $this->shippingMock->expects($this->once())->method('setMethod')->with($shippingMethod)->willReturnSelf();
223 
224  $this->shippingAssignmentMock->expects($this->once())
225  ->method('setShipping')
226  ->with($this->shippingMock)
227  ->willReturnSelf();
228 
229  $this->cartExtensionMock->expects($this->once())
230  ->method('setShippingAssignments')
231  ->with([$this->shippingAssignmentMock])
232  ->willReturnSelf();
233 
234  $this->quoteMock->expects($this->once())
235  ->method('setExtensionAttributes')
236  ->with($this->cartExtensionMock)
237  ->willReturnSelf();
238  }
239 
245  {
246  $cartId = 100;
247  $carrierCode = 'carrier_code';
248  $shippingMethod = 'shipping_method';
249  $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
250 
251  $addressInformationMock->expects($this->once())
252  ->method('getShippingAddress')
253  ->willReturn($this->shippingAddressMock);
254  $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
255  $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
256 
257  $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
258  $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
259 
260  $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn(null);
261 
262  $this->model->saveAddressInformation($cartId, $addressInformationMock);
263  }
264 
270  {
271  $cartId = 100;
272  $carrierCode = 'carrier_code';
273  $shippingMethod = 'shipping_method';
274  $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
275 
276  $this->quoteRepositoryMock->expects($this->once())
277  ->method('getActive')
278  ->with($cartId)
279  ->willReturn($this->quoteMock);
280 
281  $addressInformationMock->expects($this->once())
282  ->method('getShippingAddress')
283  ->willReturn($this->shippingAddressMock);
284  $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
285  $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
286 
287  $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
288  $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
289 
290  $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
291 
292  $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
293 
294  $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
295  $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
296  $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
297 
298  $this->quoteRepositoryMock->expects($this->once())
299  ->method('save')
300  ->with($this->quoteMock)
301  ->willThrowException(new \Exception());
302 
303  $this->model->saveAddressInformation($cartId, $addressInformationMock);
304  }
305 
311  {
312  $cartId = 100;
313  $carrierCode = 'carrier_code';
314  $shippingMethod = 'shipping_method';
315  $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
316 
317  $this->quoteRepositoryMock->expects($this->once())
318  ->method('getActive')
319  ->with($cartId)
320  ->willReturn($this->quoteMock);
321  $addressInformationMock->expects($this->once())
322  ->method('getShippingAddress')
323  ->willReturn($this->shippingAddressMock);
324  $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
325  $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
326 
327  $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
328  $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
329  $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
330 
331  $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
332 
333  $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
334  $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
335  $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
336  $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->shippingAddressMock);
337 
338  $this->quoteRepositoryMock->expects($this->once())
339  ->method('save')
340  ->with($this->quoteMock);
341 
342  $this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
343  $this->shippingAddressMock->expects($this->once())
344  ->method('getShippingRateByCode')
345  ->with($shippingMethod)
346  ->willReturn(false);
347 
348  $this->model->saveAddressInformation($cartId, $addressInformationMock);
349  }
350 
351  public function testSaveAddressInformation()
352  {
353  $cartId = 100;
354  $carrierCode = 'carrier_code';
355  $shippingMethod = 'shipping_method';
356  $addressInformationMock = $this->createMock(\Magento\Checkout\Api\Data\ShippingInformationInterface::class);
357 
358  $this->quoteRepositoryMock->expects($this->once())
359  ->method('getActive')
360  ->with($cartId)
361  ->willReturn($this->quoteMock);
362  $addressInformationMock->expects($this->once())
363  ->method('getShippingAddress')
364  ->willReturn($this->shippingAddressMock);
365  $addressInformationMock->expects($this->once())->method('getShippingCarrierCode')->willReturn($carrierCode);
366  $addressInformationMock->expects($this->once())->method('getShippingMethodCode')->willReturn($shippingMethod);
367 
368  $billingAddress = $this->createMock(\Magento\Quote\Api\Data\AddressInterface::class);
369  $addressInformationMock->expects($this->once())->method('getBillingAddress')->willReturn($billingAddress);
370  $this->shippingAddressMock->expects($this->once())->method('getCountryId')->willReturn('USA');
371 
372  $this->setShippingAssignmentsMocks($carrierCode . '_' . $shippingMethod);
373 
374  $this->quoteMock->expects($this->once())->method('getItemsCount')->willReturn(100);
375  $this->quoteMock->expects($this->once())->method('setIsMultiShipping')->with(false)->willReturnSelf();
376  $this->quoteMock->expects($this->once())->method('setBillingAddress')->with($billingAddress)->willReturnSelf();
377  $this->quoteMock->expects($this->once())->method('getShippingAddress')->willReturn($this->shippingAddressMock);
378 
379  $this->quoteRepositoryMock->expects($this->once())
380  ->method('save')
381  ->with($this->quoteMock);
382 
383  $this->shippingAddressMock->expects($this->once())->method('getShippingMethod')->willReturn($shippingMethod);
384  $this->shippingAddressMock->expects($this->once())
385  ->method('getShippingRateByCode')
386  ->with($shippingMethod)
387  ->willReturn('rates');
388 
389  $paymentDetailsMock = $this->createMock(\Magento\Checkout\Api\Data\PaymentDetailsInterface::class);
390  $this->paymentDetailsFactoryMock->expects($this->once())->method('create')->willReturn($paymentDetailsMock);
391 
392  $paymentMethodMock = $this->createMock(\Magento\Quote\Api\Data\PaymentMethodInterface::class);
393  $this->paymentMethodManagementMock->expects($this->once())
394  ->method('getList')
395  ->with($cartId)
396  ->willReturn([$paymentMethodMock]);
397 
398  $cartTotalsMock = $this->createMock(\Magento\Quote\Api\Data\TotalsInterface::class);
399  $this->cartTotalsRepositoryMock->expects($this->once())
400  ->method('get')
401  ->with($cartId)
402  ->willReturn($cartTotalsMock);
403 
404  $paymentDetailsMock->expects($this->once())
405  ->method('setPaymentMethods')
406  ->with([$paymentMethodMock])
407  ->willReturnSelf();
408  $paymentDetailsMock->expects($this->once())->method('setTotals')->with()->willReturnSelf($cartTotalsMock);
409 
410  $this->assertEquals(
411  $paymentDetailsMock,
412  $this->model->saveAddressInformation($cartId, $addressInformationMock)
413  );
414  }
415 }
$billingAddress
Definition: order.php:25
$cartId
Definition: quote.php:22
$shippingMethod
Definition: popup.phtml:12