Skip to content

Commit e5095fb

Browse files
committed
Updates for code according to comments. Related updates for unit test.
1 parent fe12c41 commit e5095fb

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

app/code/Magento/Quote/Api/CartManagementInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public function getCartForCustomer($customerId);
5252
* @param int $customerId The customer ID.
5353
* @param int $storeId
5454
* @return boolean
55+
* @throws \Magento\Framework\Exception\LocalizedException
56+
* @throws \Magento\Framework\Exception\StateException
57+
* @throws \Magento\Framework\Exception\NoSuchEntityException
5558
*/
5659
public function assignCustomer($cartId, $customerId, $storeId);
5760

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,22 @@ public function assignCustomer($cartId, $customerId, $storeId)
305305
}
306306

307307
if ($customerActiveQuote) {
308-
/** Merge carts */
309-
$quote->merge($customerActiveQuote);
310-
$this->quoteRepository->delete($customerActiveQuote);
308+
try {
309+
/** Merge carts */
310+
$quote->merge($customerActiveQuote);
311+
$customerActiveQuote->setIsActive(0);
312+
$this->quoteRepository->save($customerActiveQuote);
313+
} catch (\Exception $e) {
314+
$message = sprintf(
315+
"The customer can't be assigned to the cart. Error on cart merging: %s",
316+
$e->getMessage()
317+
);
318+
throw new StateException($message);
319+
}
320+
311321
}
312322
$quote->setCustomer($customer);
313323
$quote->setCustomerIsGuest(0);
314-
$quote->setStoreId($storeId);
315324
$quote->setIsActive(1);
316325

317326
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,12 @@ public function testAssignCustomerWithActiveCart()
510510

511511
$quoteMock = $this->createPartialMock(
512512
Quote::class,
513-
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setStoreId', 'setIsActive', 'getIsActive', 'merge']
513+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setIsActive', 'getIsActive', 'merge']
514514
);
515515

516516
$activeQuoteMock = $this->createPartialMock(
517517
Quote::class,
518-
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setStoreId', 'setIsActive', 'getIsActive', 'merge']
518+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setIsActive', 'getIsActive', 'merge']
519519
);
520520

521521
$customerMock = $this->createMock(CustomerInterface::class);
@@ -556,17 +556,17 @@ public function testAssignCustomerWithActiveCart()
556556
->willReturn($activeQuoteMock);
557557

558558
$quoteMock->expects($this->once())->method('merge')->with($activeQuoteMock)->willReturnSelf();
559-
$this->quoteRepositoryMock->expects($this->once())->method('delete')->with($activeQuoteMock);
559+
$activeQuoteMock->expects($this->once())->method('setIsActive')->with(0);
560+
$this->quoteRepositoryMock->expects($this->atLeastOnce())->method('save')->with($activeQuoteMock);
560561

561562
$quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
562563
$quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
563-
$quoteMock->expects($this->once())->method('setStoreId')->with($storeId);
564564
$quoteMock->expects($this->once())->method('setIsActive')->with(1);
565565

566566
$this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();
567567
$this->quoteIdMock->expects($this->once())->method('getId')->willReturn(10);
568568
$this->quoteIdMock->expects($this->once())->method('delete');
569-
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
569+
$this->quoteRepositoryMock->expects($this->atLeastOnce())->method('save')->with($quoteMock);
570570

571571
$this->model->assignCustomer($cartId, $customerId, $storeId);
572572
}
@@ -585,7 +585,7 @@ public function testAssignCustomer()
585585

586586
$quoteMock = $this->createPartialMock(
587587
Quote::class,
588-
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setStoreId', 'setIsActive', 'getIsActive', 'merge']
588+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setIsActive', 'getIsActive', 'merge']
589589
);
590590

591591
$customerMock = $this->createMock(CustomerInterface::class);
@@ -629,11 +629,9 @@ public function testAssignCustomer()
629629

630630
$this->assertEquals(false, $activeQuoteMock);
631631
$quoteMock->expects($this->never())->method('merge');
632-
$this->quoteRepositoryMock->expects($this->never())->method('delete');
633632

634633
$quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
635634
$quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
636-
$quoteMock->expects($this->once())->method('setStoreId')->with($storeId);
637635
$quoteMock->expects($this->once())->method('setIsActive')->with(1);
638636

639637
$this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();

0 commit comments

Comments
 (0)