Skip to content

Commit d0e2ced

Browse files
committed
Fix assignCustomer for API test.
Additional unit test for assignCustomer when customer has active cart.
1 parent 6ff21a6 commit d0e2ced

File tree

2 files changed

+110
-28
lines changed

2 files changed

+110
-28
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -301,25 +301,28 @@ public function assignCustomer($cartId, $customerId, $storeId)
301301
$customerActiveQuote = $this->quoteRepository->getForCustomer($customerId);
302302
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
303303
/** This exception appear when customer have no active cart*/
304-
$customerActiveQuote = $this->quoteFactory->create();
305-
$customerActiveQuote->setCustomer($customer);
306-
$customerActiveQuote->setCustomerIsGuest(0);
307-
$customerActiveQuote->setStoreId($storeId);
308-
$customerActiveQuote->setIsActive(true);
304+
$customerActiveQuote = false;
309305
}
310306

311-
if ($customerActiveQuote->getIsActive()) {
307+
if ($customerActiveQuote) {
312308
/** Merge carts */
313-
$customerActiveQuote->merge($quote);
314-
$this->quoteRepository->delete($quote);
315-
$this->quoteRepository->save($customerActiveQuote);
309+
$quote->merge($customerActiveQuote);
310+
$this->quoteRepository->delete($customerActiveQuote);
311+
}
312+
$quote->setCustomer($customer);
313+
$quote->setCustomerIsGuest(0);
314+
$quote->setStoreId($storeId);
315+
$quote->setIsActive(1);
316316

317-
return true;
318-
} else {
319-
throw new \Magento\Framework\Exception\NoSuchEntityException(
320-
__("The customer can't be assigned to the cart. No active cart for customer.")
321-
);
317+
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
318+
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'quote_id');
319+
if ($quoteIdMask->getId()) {
320+
$quoteIdMask->delete();
322321
}
322+
323+
$this->quoteRepository->save($quote);
324+
325+
return true;
323326
}
324327

325328
/**

app/code/Magento/Quote/Test/Unit/Model/QuoteManagementTest.php

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -497,16 +497,27 @@ public function testAssignCustomerNoSuchCustomer()
497497
$this->model->assignCustomer($cartId, $customerId, $storeId);
498498
}
499499

500-
public function testAssignCustomer()
500+
public function testAssignCustomerWithActiveCart()
501501
{
502502
$cartId = 220;
503503
$customerId = 455;
504504
$storeId = 5;
505505

506+
$this->getPropertyValue($this->model, 'quoteIdMaskFactory')
507+
->expects($this->once())
508+
->method('create')
509+
->willReturn($this->quoteIdMock);
510+
506511
$quoteMock = $this->createPartialMock(
507512
Quote::class,
508-
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'merge']
513+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setStoreId', 'setIsActive', 'getIsActive', 'merge']
514+
);
515+
516+
$activeQuoteMock = $this->createPartialMock(
517+
Quote::class,
518+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setStoreId', 'setIsActive', 'getIsActive', 'merge']
509519
);
520+
510521
$customerMock = $this->createMock(CustomerInterface::class);
511522

512523
$this->quoteRepositoryMock
@@ -538,29 +549,97 @@ public function testAssignCustomer()
538549
->willReturn([$storeId, 'some store value']);
539550

540551
$quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
541-
542552
$this->quoteRepositoryMock
543553
->expects($this->once())
544554
->method('getForCustomer')
545555
->with($customerId)
546-
->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
556+
->willReturn($activeQuoteMock);
547557

548-
$activeQuoteMock = $this->createPartialMock(
558+
$quoteMock->expects($this->once())->method('merge')->with($activeQuoteMock)->willReturnSelf();
559+
$this->quoteRepositoryMock->expects($this->once())->method('delete')->with($activeQuoteMock);
560+
561+
$quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
562+
$quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
563+
$quoteMock->expects($this->once())->method('setStoreId')->with($storeId);
564+
$quoteMock->expects($this->once())->method('setIsActive')->with(1);
565+
566+
$this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();
567+
$this->quoteIdMock->expects($this->once())->method('getId')->willReturn(10);
568+
$this->quoteIdMock->expects($this->once())->method('delete');
569+
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
570+
571+
$this->model->assignCustomer($cartId, $customerId, $storeId);
572+
}
573+
574+
public function testAssignCustomer()
575+
{
576+
$cartId = 220;
577+
$customerId = 455;
578+
$storeId = 5;
579+
$activeQuoteMock = null;
580+
581+
$this->getPropertyValue($this->model, 'quoteIdMaskFactory')
582+
->expects($this->once())
583+
->method('create')
584+
->willReturn($this->quoteIdMock);
585+
586+
$quoteMock = $this->createPartialMock(
549587
Quote::class,
550588
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setStoreId', 'setIsActive', 'getIsActive', 'merge']
551589
);
552590

553-
$this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($activeQuoteMock);
554-
$activeQuoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
555-
$activeQuoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
556-
$activeQuoteMock->expects($this->once())->method('setStoreId')->with($storeId);
557-
$activeQuoteMock->expects($this->once())->method('setIsActive')->with(1);
591+
$customerMock = $this->createMock(CustomerInterface::class);
592+
$this->quoteRepositoryMock
593+
->expects($this->once())
594+
->method('getActive')
595+
->with($cartId)
596+
->willReturn($quoteMock);
597+
598+
$this->customerRepositoryMock
599+
->expects($this->once())
600+
->method('getById')
601+
->with($customerId)
602+
->willReturn($customerMock);
603+
604+
$customerModelMock = $this->createPartialMock(
605+
Customer::class,
606+
['load', 'getSharedStoreIds']
607+
);
558608

559-
$activeQuoteMock->expects($this->once())->method('getIsActive')->willReturn(1);
560-
$activeQuoteMock->expects($this->once())->method('merge')->with($quoteMock)->willReturnSelf();
561-
$this->quoteRepositoryMock->expects($this->once())->method('delete')->with($quoteMock);
609+
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
562610

563-
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($activeQuoteMock);
611+
$customerModelMock
612+
->expects($this->once())
613+
->method('load')
614+
->with($customerId)
615+
->willReturnSelf();
616+
617+
$customerModelMock
618+
->expects($this->once())
619+
->method('getSharedStoreIds')
620+
->willReturn([$storeId, 'some store value']);
621+
622+
$quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
623+
624+
$this->quoteRepositoryMock
625+
->expects($this->once())
626+
->method('getForCustomer')
627+
->with($customerId)
628+
->willThrowException(new \Magento\Framework\Exception\NoSuchEntityException());
629+
630+
$this->assertEquals(false, $activeQuoteMock);
631+
$quoteMock->expects($this->never())->method('merge');
632+
$this->quoteRepositoryMock->expects($this->never())->method('delete');
633+
634+
$quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
635+
$quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
636+
$quoteMock->expects($this->once())->method('setStoreId')->with($storeId);
637+
$quoteMock->expects($this->once())->method('setIsActive')->with(1);
638+
639+
$this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();
640+
$this->quoteIdMock->expects($this->once())->method('getId')->willReturn(10);
641+
$this->quoteIdMock->expects($this->once())->method('delete');
642+
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
564643

565644
$this->model->assignCustomer($cartId, $customerId, $storeId);
566645
}

0 commit comments

Comments
 (0)