Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
QuoteManagementTest.php
Go to the documentation of this file.
1 <?php
8 
10 
13 
18 class QuoteManagementTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $model;
24 
28  protected $quoteValidator;
29 
33  protected $eventManager;
34 
38  protected $orderFactory;
39 
44 
49 
54 
59 
63  protected $orderManagement;
64 
69 
74 
78  protected $userContextMock;
79 
84 
89 
94 
98  protected $storeManagerMock;
99 
104 
109 
114 
119 
123  protected $quoteMock;
124 
128  private $quoteIdMock;
129 
133  private $addressRepositoryMock;
134 
138  private $quoteFactoryMock;
139 
143  protected function setUp()
144  {
145  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
146 
147  $this->quoteValidator = $this->createMock(\Magento\Quote\Model\QuoteValidator::class);
148  $this->eventManager = $this->getMockForAbstractClass(\Magento\Framework\Event\ManagerInterface::class);
149  $this->orderFactory = $this->createPartialMock(
150  \Magento\Sales\Api\Data\OrderInterfaceFactory::class,
151  ['create', 'populate']
152  );
153  $this->quoteAddressToOrder = $this->createMock(\Magento\Quote\Model\Quote\Address\ToOrder::class);
154  $this->quotePaymentToOrderPayment = $this->createMock(\Magento\Quote\Model\Quote\Payment\ToOrderPayment::class);
155  $this->quoteAddressToOrderAddress = $this->createMock(\Magento\Quote\Model\Quote\Address\ToOrderAddress::class);
156  $this->quoteItemToOrderItem = $this->createMock(\Magento\Quote\Model\Quote\Item\ToOrderItem::class);
157  $this->orderManagement = $this->createMock(\Magento\Sales\Api\OrderManagementInterface::class);
158  $this->customerManagement = $this->createMock(\Magento\Quote\Model\CustomerManagement::class);
159  $this->quoteRepositoryMock = $this->createMock(\Magento\Quote\Api\CartRepositoryInterface::class);
160 
161  $this->userContextMock = $this->createMock(\Magento\Authorization\Model\UserContextInterface::class);
162  $this->customerRepositoryMock = $this->createPartialMock(
163  \Magento\Customer\Api\CustomerRepositoryInterface::class,
164  ['create', 'save', 'get', 'getById', 'getList', 'delete', 'deleteById']
165  );
166  $this->customerFactoryMock = $this->createPartialMock(
167  \Magento\Customer\Model\CustomerFactory::class,
168  ['create']
169  );
170  $this->storeManagerMock = $this->getMockForAbstractClass(
171  \Magento\Store\Model\StoreManagerInterface::class,
172  [],
173  '',
174  false,
175  true,
176  true,
177  ['getStore', 'getStoreId']
178  );
179 
180  $this->quoteMock = $this->createPartialMock(\Magento\Quote\Model\Quote::class, [
181  'getId',
182  'getCheckoutMethod',
183  'setCheckoutMethod',
184  'setCustomerId',
185  'setCustomerEmail',
186  'getBillingAddress',
187  'setCustomerIsGuest',
188  'setCustomerGroupId',
189  'assignCustomer',
190  'getPayment',
191  ]);
192 
193  $this->quoteAddressFactory = $this->createPartialMock(
194  \Magento\Quote\Model\Quote\AddressFactory::class,
195  ['create']
196  );
197 
198  $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
199  $this->checkoutSessionMock = $this->createPartialMock(
200  \Magento\Checkout\Model\Session::class,
201  ['setLastQuoteId', 'setLastSuccessQuoteId', 'setLastOrderId', 'setLastRealOrderId', 'setLastOrderStatus']
202  );
203  $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
204  $this->accountManagementMock = $this->createMock(\Magento\Customer\Api\AccountManagementInterface::class);
205 
206  $this->quoteFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteFactory::class, ['create']);
207  $this->addressRepositoryMock = $this->getMockBuilder(\Magento\Customer\Api\AddressRepositoryInterface::class)
208  ->getMockForAbstractClass();
209 
210  $this->model = $objectManager->getObject(
211  \Magento\Quote\Model\QuoteManagement::class,
212  [
213  'eventManager' => $this->eventManager,
214  'quoteValidator' => $this->quoteValidator,
215  'orderFactory' => $this->orderFactory,
216  'orderManagement' => $this->orderManagement,
217  'customerManagement' => $this->customerManagement,
218  'quoteAddressToOrder' => $this->quoteAddressToOrder,
219  'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
220  'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
221  'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
222  'userContext' => $this->userContextMock,
223  'quoteRepository' => $this->quoteRepositoryMock,
224  'customerRepository' => $this->customerRepositoryMock,
225  'customerModelFactory' => $this->customerFactoryMock,
226  'quoteAddressFactory' => $this->quoteAddressFactory,
227  'dataObjectHelper' => $this->dataObjectHelperMock,
228  'storeManager' => $this->storeManagerMock,
229  'checkoutSession' => $this->checkoutSessionMock,
230  'customerSession' => $this->customerSessionMock,
231  'accountManagement' => $this->accountManagementMock,
232  'quoteFactory' => $this->quoteFactoryMock,
233  'addressRepository' => $this->addressRepositoryMock
234  ]
235  );
236 
237  // Set the new dependency
238  $this->quoteIdMock = $this->createMock(\Magento\Quote\Model\QuoteIdMask::class);
239  $quoteIdFactoryMock = $this->createPartialMock(\Magento\Quote\Model\QuoteIdMaskFactory::class, ['create']);
240  $this->setPropertyValue($this->model, 'quoteIdMaskFactory', $quoteIdFactoryMock);
241  }
242 
244  {
245  $storeId = 345;
246  $quoteId = 2311;
247 
248  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
249  $quoteAddress = $this->createPartialMock(
250  \Magento\Quote\Model\Quote\Address::class,
251  ['setCollectShippingRates']
252  );
253  $quoteAddress->expects($this->once())->method('setCollectShippingRates')->with(true);
254 
255  $quoteMock->expects($this->any())->method('setBillingAddress')->with($quoteAddress)->willReturnSelf();
256  $quoteMock->expects($this->any())->method('setShippingAddress')->with($quoteAddress)->willReturnSelf();
257  $quoteMock->expects($this->any())->method('getShippingAddress')->willReturn($quoteAddress);
258 
259  $this->quoteAddressFactory->expects($this->any())->method('create')->willReturn($quoteAddress);
260 
261  $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($quoteMock);
262  $quoteMock->expects($this->any())->method('setStoreId')->with($storeId);
263 
264  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
265  $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
266 
267  $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
268  $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
269 
270  $this->assertEquals($quoteId, $this->model->createEmptyCart());
271  }
272 
274  {
275  $storeId = 345;
276  $quoteId = 2311;
277  $userId = 567;
278 
279  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
280 
281  $this->quoteRepositoryMock
282  ->expects($this->once())
283  ->method('getActiveForCustomer')
284  ->with($userId)
285  ->willThrowException(new NoSuchEntityException());
286 
287  $this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($quoteMock);
288  $quoteMock->expects($this->any())->method('setStoreId')->with($storeId);
289  $quoteMock->expects($this->any())->method('setCustomerIsGuest')->with(0);
290 
291  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
292  $quoteMock->expects($this->once())->method('getId')->willReturn($quoteId);
293 
294  $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
295  $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
296 
297  $this->assertEquals($quoteId, $this->model->createEmptyCartForCustomer($userId));
298  }
299 
301  {
302  $storeId = 345;
303  $userId = 567;
304 
305  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
306 
307  $this->quoteRepositoryMock
308  ->expects($this->once())
309  ->method('getActiveForCustomer')
310  ->with($userId)->willReturn($quoteMock);
311 
312  $this->quoteFactoryMock->expects($this->never())->method('create')->willReturn($quoteMock);
313  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
314 
315  $this->storeManagerMock->expects($this->once())->method('getStore')->willReturnSelf();
316  $this->storeManagerMock->expects($this->once())->method('getStoreId')->willReturn($storeId);
317 
318  $this->model->createEmptyCartForCustomer($userId);
319  }
320 
326  {
327  $cartId = 220;
328  $customerId = 455;
329  $storeId = 5;
330 
331  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
332  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
333 
334  $this->quoteRepositoryMock
335  ->expects($this->once())
336  ->method('getActive')
337  ->with($cartId)
338  ->willReturn($quoteMock);
339 
340  $this->customerRepositoryMock
341  ->expects($this->once())
342  ->method('getById')
343  ->with($customerId)
344  ->willReturn($customerMock);
345 
346  $customerModelMock = $this->createPartialMock(
347  \Magento\Customer\Model\Customer::class,
348  ['load', 'getSharedStoreIds']
349  );
350  $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
351  $customerModelMock
352  ->expects($this->once())
353  ->method('load')
354  ->with($customerId)
355  ->willReturnSelf();
356 
357  $customerModelMock
358  ->expects($this->once())
359  ->method('getSharedStoreIds')
360  ->willReturn([]);
361 
362  $this->model->assignCustomer($cartId, $customerId, $storeId);
363  }
364 
370  {
371  $cartId = 220;
372  $customerId = 455;
373  $storeId = 5;
374 
375  $quoteMock = $this->createPartialMock(
376  \Magento\Quote\Model\Quote::class,
377  ['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
378  );
379  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
380 
381  $this->quoteRepositoryMock
382  ->expects($this->once())
383  ->method('getActive')
384  ->with($cartId)
385  ->willReturn($quoteMock);
386 
387  $this->customerRepositoryMock
388  ->expects($this->once())
389  ->method('getById')
390  ->with($customerId)
391  ->willReturn($customerMock);
392 
393  $customerModelMock = $this->createPartialMock(
394  \Magento\Customer\Model\Customer::class,
395  ['load', 'getSharedStoreIds']
396  );
397  $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
398  $customerModelMock
399  ->expects($this->once())
400  ->method('load')
401  ->with($customerId)
402  ->willReturnSelf();
403 
404  $customerModelMock
405  ->expects($this->once())
406  ->method('getSharedStoreIds')
407  ->willReturn([$storeId, 'some store value']);
408 
409  $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(753);
410 
411  $this->model->assignCustomer($cartId, $customerId, $storeId);
412  }
413 
418  {
419  $cartId = 220;
420  $customerId = 455;
421  $storeId = 5;
422 
423  $quoteMock = $this->createPartialMock(
424  \Magento\Quote\Model\Quote::class,
425  ['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
426  );
427  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
428 
429  $this->quoteRepositoryMock
430  ->expects($this->once())
431  ->method('getActive')
432  ->with($cartId)
433  ->willReturn($quoteMock);
434 
435  $this->customerRepositoryMock
436  ->expects($this->once())
437  ->method('getById')
438  ->with($customerId)
439  ->willReturn($customerMock);
440 
441  $customerModelMock = $this->createPartialMock(
442  \Magento\Customer\Model\Customer::class,
443  ['load', 'getSharedStoreIds']
444  );
445  $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
446  $customerModelMock
447  ->expects($this->once())
448  ->method('load')
449  ->with($customerId)
450  ->willReturnSelf();
451 
452  $customerModelMock
453  ->expects($this->once())
454  ->method('getSharedStoreIds')
455  ->willReturn([$storeId, 'some store value']);
456 
457  $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
458 
459  $this->quoteRepositoryMock
460  ->expects($this->once())
461  ->method('getForCustomer')
462  ->with($customerId);
463 
464  $this->model->assignCustomer($cartId, $customerId, $storeId);
465 
466  $this->expectExceptionMessage(
467  "The customer can't be assigned to the cart because the customer already has an active cart."
468  );
469  }
470 
471  public function testAssignCustomer()
472  {
473  $cartId = 220;
474  $customerId = 455;
475  $storeId = 5;
476 
477  $this->getPropertyValue($this->model, 'quoteIdMaskFactory')
478  ->expects($this->once())
479  ->method('create')
480  ->willReturn($this->quoteIdMock);
481  $this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();
482  $this->quoteIdMock->expects($this->once())->method('getId')->willReturn(10);
483  $this->quoteIdMock->expects($this->once())->method('delete');
484  $quoteMock = $this->createPartialMock(
485  \Magento\Quote\Model\Quote::class,
486  ['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
487  );
488  $customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
489 
490  $this->quoteRepositoryMock
491  ->expects($this->once())
492  ->method('getActive')
493  ->with($cartId)
494  ->willReturn($quoteMock);
495 
496  $this->customerRepositoryMock
497  ->expects($this->once())
498  ->method('getById')
499  ->with($customerId)
500  ->willReturn($customerMock);
501 
502  $customerModelMock = $this->createPartialMock(
503  \Magento\Customer\Model\Customer::class,
504  ['load', 'getSharedStoreIds']
505  );
506  $this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
507  $customerModelMock
508  ->expects($this->once())
509  ->method('load')
510  ->with($customerId)
511  ->willReturnSelf();
512 
513  $customerModelMock
514  ->expects($this->once())
515  ->method('getSharedStoreIds')
516  ->willReturn([$storeId, 'some store value']);
517 
518  $quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
519 
520  $this->quoteRepositoryMock
521  ->expects($this->once())
522  ->method('getForCustomer')
523  ->with($customerId)
524  ->willThrowException(new NoSuchEntityException());
525 
526  $quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
527  $quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
528 
529  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
530 
531  $this->model->assignCustomer($cartId, $customerId, $storeId);
532  }
533 
534  public function testSubmit()
535  {
536  $orderData = [];
537  $isGuest = true;
538  $isVirtual = false;
539  $customerId = 1;
540  $quoteId = 1;
541  $quoteItem = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
542  $billingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
543  $shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
544  $payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
545  $baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
546  $convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
547  $convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
548  $convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
549  $convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
550 
551  $addresses = [$convertedShipping, $convertedBilling];
552  $quoteItems = [$quoteItem];
553  $convertedItems = [$convertedQuoteItem];
554 
555  $quote = $this->getQuote(
556  $isGuest,
557  $isVirtual,
559  $payment,
560  $customerId,
561  $quoteId,
562  $quoteItems,
564  );
565 
566  $this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
567  $this->quoteAddressToOrder->expects($this->once())
568  ->method('convert')
569  ->with($shippingAddress, $orderData)
570  ->willReturn($baseOrder);
571  $this->quoteAddressToOrderAddress->expects($this->at(0))
572  ->method('convert')
573  ->with(
575  [
576  'address_type' => 'shipping',
577  'email' => 'customer@example.com'
578  ]
579  )
580  ->willReturn($convertedShipping);
581  $this->quoteAddressToOrderAddress->expects($this->at(1))
582  ->method('convert')
583  ->with(
585  [
586  'address_type' => 'billing',
587  'email' => 'customer@example.com'
588  ]
589  )
590  ->willReturn($convertedBilling);
591  $billingAddress->expects($this->once())->method('getId')->willReturn(4);
592  $convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
593 
594  $this->quoteItemToOrderItem->expects($this->once())->method('convert')
595  ->with($quoteItem, ['parent_item' => null])
596  ->willReturn($convertedQuoteItem);
597  $this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
598  ->willReturn($convertedPayment);
599  $shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
600  $shippingAddress->expects($this->once())->method('getId')->willReturn(5);
601  $convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
602  $order = $this->prepareOrderFactory(
603  $baseOrder,
604  $convertedBilling,
605  $addresses,
606  $convertedPayment,
607  $convertedItems,
608  $quoteId,
609  $convertedShipping
610  );
611 
612  $this->orderManagement->expects($this->once())
613  ->method('place')
614  ->with($order)
615  ->willReturn($order);
616  $this->eventManager->expects($this->at(0))
617  ->method('dispatch')
618  ->with('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]);
619  $this->eventManager->expects($this->at(1))
620  ->method('dispatch')
621  ->with('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]);
622  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
623  $this->assertEquals($order, $this->model->submit($quote, $orderData));
624  }
625 
626  public function testPlaceOrderIfCustomerIsGuest()
627  {
628  $cartId = 100;
629  $orderId = 332;
630  $orderIncrementId = 100003332;
631  $orderStatus = 'status1';
632  $email = 'email@mail.com';
633 
634  $this->quoteRepositoryMock->expects($this->once())
635  ->method('getActive')
636  ->with($cartId)
637  ->willReturn($this->quoteMock);
638 
639  $this->quoteMock->expects($this->once())
640  ->method('getCheckoutMethod')
641  ->willReturn(\Magento\Checkout\Model\Type\Onepage::METHOD_GUEST);
642  $this->quoteMock->expects($this->once())->method('setCustomerId')->with(null)->willReturnSelf();
643  $this->quoteMock->expects($this->once())->method('setCustomerEmail')->with($email)->willReturnSelf();
644 
645  $addressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, ['getEmail']);
646  $addressMock->expects($this->once())->method('getEmail')->willReturn($email);
647  $this->quoteMock->expects($this->once())->method('getBillingAddress')->with()->willReturn($addressMock);
648 
649  $this->quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(true)->willReturnSelf();
650  $this->quoteMock->expects($this->once())
651  ->method('setCustomerGroupId')
652  ->with(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
653 
655  $service = $this->getMockBuilder(\Magento\Quote\Model\QuoteManagement::class)
656  ->setMethods(['submit'])
657  ->setConstructorArgs(
658  [
659  'eventManager' => $this->eventManager,
660  'quoteValidator' => $this->quoteValidator,
661  'orderFactory' => $this->orderFactory,
662  'orderManagement' => $this->orderManagement,
663  'customerManagement' => $this->customerManagement,
664  'quoteAddressToOrder' => $this->quoteAddressToOrder,
665  'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
666  'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
667  'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
668  'userContext' => $this->userContextMock,
669  'quoteRepository' => $this->quoteRepositoryMock,
670  'customerRepository' => $this->customerRepositoryMock,
671  'customerModelFactory' => $this->customerFactoryMock,
672  'quoteAddressFactory' => $this->quoteAddressFactory,
673  'dataObjectHelper' => $this->dataObjectHelperMock,
674  'storeManager' => $this->storeManagerMock,
675  'checkoutSession' => $this->checkoutSessionMock,
676  'customerSession' => $this->customerSessionMock,
677  'accountManagement' => $this->accountManagementMock,
678  'quoteFactory' => $this->quoteFactoryMock
679  ]
680  )
681  ->getMock();
682 
683  $orderMock = $this->createPartialMock(
684  \Magento\Sales\Model\Order::class,
685  ['getId', 'getIncrementId', 'getStatus']
686  );
687  $service->expects($this->once())->method('submit')->willReturn($orderMock);
688 
689  $this->quoteMock->expects($this->atLeastOnce())->method('getId')->willReturn($cartId);
690 
691  $orderMock->expects($this->atLeastOnce())->method('getId')->willReturn($orderId);
692  $orderMock->expects($this->atLeastOnce())->method('getIncrementId')->willReturn($orderIncrementId);
693  $orderMock->expects($this->atLeastOnce())->method('getStatus')->willReturn($orderStatus);
694 
695  $this->checkoutSessionMock->expects($this->once())->method('setLastQuoteId')->with($cartId);
696  $this->checkoutSessionMock->expects($this->once())->method('setLastSuccessQuoteId')->with($cartId);
697  $this->checkoutSessionMock->expects($this->once())->method('setLastOrderId')->with($orderId);
698  $this->checkoutSessionMock->expects($this->once())->method('setLastRealOrderId')->with($orderIncrementId);
699  $this->checkoutSessionMock->expects($this->once())->method('setLastOrderStatus')->with($orderStatus);
700 
701  $this->assertEquals($orderId, $service->placeOrder($cartId));
702  }
703 
704  public function testPlaceOrder()
705  {
706  $cartId = 323;
707  $orderId = 332;
708  $orderIncrementId = 100003332;
709  $orderStatus = 'status1';
710 
712  $service = $this->getMockBuilder(\Magento\Quote\Model\QuoteManagement::class)
713  ->setMethods(['submit'])
714  ->setConstructorArgs(
715  [
716  'eventManager' => $this->eventManager,
717  'quoteValidator' => $this->quoteValidator,
718  'orderFactory' => $this->orderFactory,
719  'orderManagement' => $this->orderManagement,
720  'customerManagement' => $this->customerManagement,
721  'quoteAddressToOrder' => $this->quoteAddressToOrder,
722  'quoteAddressToOrderAddress' => $this->quoteAddressToOrderAddress,
723  'quoteItemToOrderItem' => $this->quoteItemToOrderItem,
724  'quotePaymentToOrderPayment' => $this->quotePaymentToOrderPayment,
725  'userContext' => $this->userContextMock,
726  'quoteRepository' => $this->quoteRepositoryMock,
727  'customerRepository' => $this->customerRepositoryMock,
728  'customerModelFactory' => $this->customerFactoryMock,
729  'quoteAddressFactory' => $this->quoteAddressFactory,
730  'dataObjectHelper' => $this->dataObjectHelperMock,
731  'storeManager' => $this->storeManagerMock,
732  'checkoutSession' => $this->checkoutSessionMock,
733  'customerSession' => $this->customerSessionMock,
734  'accountManagement' => $this->accountManagementMock,
735  'quoteFactory' => $this->quoteFactoryMock
736  ]
737  )
738  ->getMock();
739 
740  $orderMock = $this->createMock(\Magento\Sales\Model\Order::class);
741 
742  $this->quoteRepositoryMock->expects($this->once())
743  ->method('getActive')
744  ->with($cartId)
745  ->willReturn($this->quoteMock);
746 
747  $quotePayment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
748  $quotePayment->expects($this->once())
749  ->method('setQuote');
750  $quotePayment->expects($this->once())
751  ->method('importData');
752  $this->quoteMock->expects($this->atLeastOnce())
753  ->method('getPayment')
754  ->willReturn($quotePayment);
755 
756  $this->quoteMock->expects($this->once())
757  ->method('getCheckoutMethod')
758  ->willReturn(\Magento\Checkout\Model\Type\Onepage::METHOD_CUSTOMER);
759  $this->quoteMock->expects($this->never())
760  ->method('setCustomerIsGuest')
761  ->with(true);
762 
763  $service->expects($this->once())->method('submit')->willReturn($orderMock);
764 
765  $this->quoteMock->expects($this->atLeastOnce())->method('getId')->willReturn($cartId);
766 
767  $orderMock->expects($this->atLeastOnce())->method('getId')->willReturn($orderId);
768  $orderMock->expects($this->atLeastOnce())->method('getIncrementId')->willReturn($orderIncrementId);
769  $orderMock->expects($this->atLeastOnce())->method('getStatus')->willReturn($orderStatus);
770 
771  $this->checkoutSessionMock->expects($this->once())->method('setLastQuoteId')->with($cartId);
772  $this->checkoutSessionMock->expects($this->once())->method('setLastSuccessQuoteId')->with($cartId);
773  $this->checkoutSessionMock->expects($this->once())->method('setLastOrderId')->with($orderId);
774  $this->checkoutSessionMock->expects($this->once())->method('setLastRealOrderId')->with($orderIncrementId);
775  $this->checkoutSessionMock->expects($this->once())->method('setLastOrderStatus')->with($orderStatus);
776 
777  $paymentMethod = $this->createPartialMock(\Magento\Quote\Model\Quote\Payment::class, ['setChecks', 'getData']);
778  $paymentMethod->expects($this->once())->method('setChecks');
779  $paymentMethod->expects($this->once())->method('getData')->willReturn(['additional_data' => []]);
780 
781  $this->assertEquals($orderId, $service->placeOrder($cartId, $paymentMethod));
782  }
783 
795  protected function getQuote(
796  $isGuest,
797  $isVirtual,
800  $customerId,
801  $id,
802  array $quoteItems,
804  ) {
805  $quote = $this->createPartialMock(
806  \Magento\Quote\Model\Quote::class,
807  [
808  'setIsActive',
809  'getCustomerEmail',
810  'getAllVisibleItems',
811  'getCustomerIsGuest',
812  'isVirtual',
813  'getBillingAddress',
814  'getShippingAddress',
815  'getId',
816  'getCustomer',
817  'getAllItems',
818  'getPayment',
819  'reserveOrderId',
820  'getCustomerId',
821  'addCustomerAddress'
822  ]
823  );
824  $quote->expects($this->once())
825  ->method('setIsActive')
826  ->with(false);
827  $quote->expects($this->any())
828  ->method('getAllVisibleItems')
829  ->willReturn($quoteItems);
830  $quote->expects($this->once())
831  ->method('getAllItems')
832  ->willReturn($quoteItems);
833  $quote->expects($this->once())
834  ->method('getCustomerIsGuest')
835  ->willReturn($isGuest);
836  $quote->expects($this->any())
837  ->method('isVirtual')
838  ->willReturn($isVirtual);
839  if ($shippingAddress) {
840  $quote->expects($this->any())
841  ->method('getShippingAddress')
842  ->willReturn($shippingAddress);
843  }
844  $quote->expects($this->any())
845  ->method('getBillingAddress')
846  ->willReturn($billingAddress);
847  $quote->expects($this->once())
848  ->method('getPayment')
849  ->willReturn($payment);
850 
851  $customer = $this->createPartialMock(
852  \Magento\Customer\Model\Customer::class,
853  ['getDefaultBilling', 'getId']
854  );
855  $quote->expects($this->any())->method('getCustomerId')->willReturn($customerId);
856 
857  $customer->expects($this->once())
858  ->method('getId')
859  ->willReturn($customerId);
860  $quote->expects($this->atLeastOnce())
861  ->method('getCustomerEmail')
862  ->willReturn('customer@example.com');
863  $quote->expects($this->any())
864  ->method('getCustomer')
865  ->willReturn($customer);
866  $quote->expects($this->once())
867  ->method('getId')
868  ->willReturn($id);
869  $this->customerRepositoryMock->expects($this->any())->method('getById')->willReturn($customer);
870 
871  $customer->expects($this->any())->method('getDefaultBilling')->willReturn(1);
872  return $quote;
873  }
874 
885  protected function prepareOrderFactory(
886  \Magento\Sales\Api\Data\OrderInterface $baseOrder,
888  array $addresses,
889  $payment,
890  array $items,
891  $quoteId,
893  $customerId = null
894  ) {
895  $order = $this->createPartialMock(
896  \Magento\Sales\Model\Order::class,
897  [
898  'setShippingAddress',
899  'getAddressesCollection',
900  'getAddresses',
901  'getBillingAddress',
902  'addAddresses',
903  'setBillingAddress',
904  'setAddresses',
905  'setPayment',
906  'setItems',
907  'setQuoteId'
908  ]
909  );
910 
911  $this->orderFactory->expects($this->once())
912  ->method('create')
913  ->willReturn($order);
914  $this->orderFactory->expects($this->never())
915  ->method('populate')
916  ->with($baseOrder);
917 
918  if ($shippingAddress) {
919  $order->expects($this->once())->method('setShippingAddress')->with($shippingAddress);
920  }
921  if ($customerId) {
922  $this->orderFactory->expects($this->once())
923  ->method('setCustomerId')
924  ->with($customerId);
925  }
926  $order->expects($this->any())->method('getAddressesCollection');
927  $order->expects($this->any())->method('getAddresses');
928  $order->expects($this->any())->method('getBillingAddress')->willReturn(false);
929  $order->expects($this->any())->method('addAddresses')->withAnyParameters()->willReturnSelf();
930  $order->expects($this->once())->method('setBillingAddress')->with($billingAddress);
931  $order->expects($this->once())->method('setAddresses')->with($addresses);
932  $order->expects($this->once())->method('setPayment')->with($payment);
933  $order->expects($this->once())->method('setItems')->with($items);
934  $order->expects($this->once())->method('setQuoteId')->with($quoteId);
935 
936  return $order;
937  }
938 
939  public function testGetCartForCustomer()
940  {
941  $customerId = 100;
942  $cartMock = $this->createMock(\Magento\Quote\Model\Quote::class);
943  $this->quoteRepositoryMock->expects($this->once())
944  ->method('getActiveForCustomer')
945  ->with($customerId)
946  ->willReturn($cartMock);
947  $this->assertEquals($cartMock, $this->model->getCartForCustomer($customerId));
948  }
949 
957  protected function getPropertyValue($object, $property)
958  {
959  $reflection = new \ReflectionClass(get_class($object));
960  $reflectionProperty = $reflection->getProperty($property);
961  $reflectionProperty->setAccessible(true);
962 
963  return $reflectionProperty->getValue($object);
964  }
965 
973  protected function setPropertyValue(&$object, $property, $value)
974  {
975  $reflection = new \ReflectionClass(get_class($object));
976  $reflectionProperty = $reflection->getProperty($property);
977  $reflectionProperty->setAccessible(true);
978  $reflectionProperty->setValue($object, $value);
979 
980  return $object;
981  }
982 
983  public function testSubmitForCustomer()
984  {
985  $orderData = [];
986  $isGuest = false;
987  $isVirtual = false;
988  $customerId = 1;
989  $quoteId = 1;
990  $quoteItem = $this->createMock(\Magento\Quote\Model\Quote\Item::class);
991  $billingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
992  $shippingAddress = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
993  $payment = $this->createMock(\Magento\Quote\Model\Quote\Payment::class);
994  $baseOrder = $this->createMock(\Magento\Sales\Api\Data\OrderInterface::class);
995  $convertedBilling = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
996  $convertedShipping = $this->createPartialMockForAbstractClass(OrderAddressInterface::class, ['setData']);
997  $convertedPayment = $this->createMock(\Magento\Sales\Api\Data\OrderPaymentInterface::class);
998  $convertedQuoteItem = $this->createMock(\Magento\Sales\Api\Data\OrderItemInterface::class);
999 
1000  $addresses = [$convertedShipping, $convertedBilling];
1001  $quoteItems = [$quoteItem];
1002  $convertedItems = [$convertedQuoteItem];
1003 
1004  $quote = $this->getQuote(
1005  $isGuest,
1006  $isVirtual,
1008  $payment,
1009  $customerId,
1010  $quoteId,
1011  $quoteItems,
1013  );
1014 
1015  $this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
1016  $this->quoteAddressToOrder->expects($this->once())
1017  ->method('convert')
1018  ->with($shippingAddress, $orderData)
1019  ->willReturn($baseOrder);
1020  $this->quoteAddressToOrderAddress->expects($this->at(0))
1021  ->method('convert')
1022  ->with(
1024  [
1025  'address_type' => 'shipping',
1026  'email' => 'customer@example.com'
1027  ]
1028  )
1029  ->willReturn($convertedShipping);
1030  $this->quoteAddressToOrderAddress->expects($this->at(1))
1031  ->method('convert')
1032  ->with(
1034  [
1035  'address_type' => 'billing',
1036  'email' => 'customer@example.com'
1037  ]
1038  )
1039  ->willReturn($convertedBilling);
1040  $this->quoteItemToOrderItem->expects($this->once())->method('convert')
1041  ->with($quoteItem, ['parent_item' => null])
1042  ->willReturn($convertedQuoteItem);
1043  $this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)
1044  ->willReturn($convertedPayment);
1045  $shippingAddress->expects($this->once())->method('getShippingMethod')->willReturn('free');
1046  $shippingAddress->expects($this->once())->method('getId')->willReturn(5);
1047  $convertedShipping->expects($this->once())->method('setData')->with('quote_address_id', 5);
1048  $order = $this->prepareOrderFactory(
1049  $baseOrder,
1050  $convertedBilling,
1051  $addresses,
1052  $convertedPayment,
1053  $convertedItems,
1054  $quoteId,
1055  $convertedShipping
1056  );
1057  $customerAddressMock = $this->getMockBuilder(\Magento\Customer\Api\Data\AddressInterface::class)
1058  ->getMockForAbstractClass();
1059  $shippingAddress->expects($this->once())->method('exportCustomerAddress')->willReturn($customerAddressMock);
1060  $this->addressRepositoryMock->expects($this->once())->method('save')->with($customerAddressMock);
1061  $quote->expects($this->any())->method('addCustomerAddress')->with($customerAddressMock);
1062  $billingAddress->expects($this->once())->method('getCustomerId')->willReturn(2);
1063  $billingAddress->expects($this->once())->method('getSaveInAddressBook')->willReturn(false);
1064  $billingAddress->expects($this->once())->method('getId')->willReturn(4);
1065  $convertedBilling->expects($this->once())->method('setData')->with('quote_address_id', 4);
1066  $this->orderManagement->expects($this->once())
1067  ->method('place')
1068  ->with($order)
1069  ->willReturn($order);
1070  $this->eventManager->expects($this->at(0))
1071  ->method('dispatch')
1072  ->with('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]);
1073  $this->eventManager->expects($this->at(1))
1074  ->method('dispatch')
1075  ->with('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]);
1076  $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
1077  $this->assertEquals($order, $this->model->submit($quote, $orderData));
1078  }
1079 
1088  private function createPartialMockForAbstractClass($className, $methods = [])
1089  {
1090  return $this->getMockForAbstractClass(
1091  $className,
1092  [],
1093  '',
1094  true,
1095  true,
1096  true,
1097  $methods
1098  );
1099  }
1100 }
$billingAddress
Definition: order.php:25
$objectManager
Definition: bootstrap.php:17
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
$id
Definition: fieldset.phtml:14
$addresses
Definition: address_list.php:7
$quote
$shippingAddress
Definition: order.php:40
$order
Definition: order.php:55
prepareOrderFactory(\Magento\Sales\Api\Data\OrderInterface $baseOrder, OrderAddressInterface $billingAddress, array $addresses, $payment, array $items, $quoteId, OrderAddressInterface $shippingAddress=null, $customerId=null)
getQuote( $isGuest, $isVirtual, \Magento\Quote\Model\Quote\Address $billingAddress, \Magento\Quote\Model\Quote\Payment $payment, $customerId, $id, array $quoteItems, \Magento\Quote\Model\Quote\Address $shippingAddress=null)
$payment
Definition: order.php:17
$methods
Definition: billing.phtml:71
$orderStatus
Definition: order_status.php:9
$value
Definition: gender.phtml:16
$cartId
Definition: quote.php:22
$quoteItem
Definition: quote.php:38
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31
$items