Skip to content

Commit dc460c8

Browse files
committed
Update for PHPUnit test scenario according to new update for merging guest cart and customer cart.
1 parent 2a6dc66 commit dc460c8

File tree

1 file changed

+42
-57
lines changed

1 file changed

+42
-57
lines changed

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

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
namespace Magento\Quote\Test\Unit\Model;
99

10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\Customer;
1012
use Magento\Framework\App\RequestInterface;
1113
use Magento\Framework\Exception\NoSuchEntityException;
1214

1315
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
1416
use Magento\Quote\Model\CustomerManagement;
17+
use Magento\Quote\Model\Quote;
1518
use Magento\Quote\Model\QuoteIdMaskFactory;
1619
use Magento\Sales\Api\Data\OrderAddressInterface;
1720

@@ -199,7 +202,7 @@ protected function setUp()
199202
);
200203

201204
$this->quoteMock = $this->createPartialMock(
202-
\Magento\Quote\Model\Quote::class,
205+
Quote::class,
203206
[
204207
'assignCustomer',
205208
'collectTotals',
@@ -275,7 +278,7 @@ public function testCreateEmptyCartAnonymous()
275278
$storeId = 345;
276279
$quoteId = 2311;
277280

278-
$quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
281+
$quoteMock = $this->createMock(Quote::class);
279282
$quoteAddress = $this->createPartialMock(
280283
\Magento\Quote\Model\Quote\Address::class,
281284
['setCollectShippingRates']
@@ -306,14 +309,14 @@ public function testCreateEmptyCartForCustomer()
306309
$quoteId = 2311;
307310
$userId = 567;
308311

309-
$quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
312+
$quoteMock = $this->createMock(Quote::class);
310313

311314
$this->quoteRepositoryMock
312315
->expects($this->once())
313316
->method('getActiveForCustomer')
314317
->with($userId)
315318
->willThrowException(new NoSuchEntityException());
316-
$customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
319+
$customer = $this->getMockBuilder(CustomerInterface::class)
317320
->setMethods(['getDefaultBilling'])->disableOriginalConstructor()->getMockForAbstractClass();
318321
$quoteAddress = $this->createPartialMock(
319322
\Magento\Quote\Model\Quote\Address::class,
@@ -342,14 +345,14 @@ public function testCreateEmptyCartForCustomerReturnExistsQuote()
342345
$storeId = 345;
343346
$userId = 567;
344347

345-
$quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
348+
$quoteMock = $this->createMock(Quote::class);
346349

347350
$this->quoteRepositoryMock
348351
->expects($this->once())
349352
->method('getActiveForCustomer')
350353
->with($userId)->willReturn($quoteMock);
351354

352-
$customer = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
355+
$customer = $this->getMockBuilder(CustomerInterface::class)
353356
->setMethods(['getDefaultBilling'])->disableOriginalConstructor()->getMockForAbstractClass();
354357
$quoteAddress = $this->createPartialMock(
355358
\Magento\Quote\Model\Quote\Address::class,
@@ -379,8 +382,8 @@ public function testAssignCustomerFromAnotherStore()
379382
$customerId = 455;
380383
$storeId = 5;
381384

382-
$quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
383-
$customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
385+
$quoteMock = $this->createMock(Quote::class);
386+
$customerMock = $this->createMock(CustomerInterface::class);
384387

385388
$this->quoteRepositoryMock
386389
->expects($this->once())
@@ -395,7 +398,7 @@ public function testAssignCustomerFromAnotherStore()
395398
->willReturn($customerMock);
396399

397400
$customerModelMock = $this->createPartialMock(
398-
\Magento\Customer\Model\Customer::class,
401+
Customer::class,
399402
['load', 'getSharedStoreIds']
400403
);
401404
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
@@ -424,10 +427,10 @@ public function testAssignCustomerToNonanonymousCart()
424427
$storeId = 5;
425428

426429
$quoteMock = $this->createPartialMock(
427-
\Magento\Quote\Model\Quote::class,
430+
Quote::class,
428431
['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
429432
);
430-
$customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
433+
$customerMock = $this->createMock(CustomerInterface::class);
431434

432435
$this->quoteRepositoryMock
433436
->expects($this->once())
@@ -442,7 +445,7 @@ public function testAssignCustomerToNonanonymousCart()
442445
->willReturn($customerMock);
443446

444447
$customerModelMock = $this->createPartialMock(
445-
\Magento\Customer\Model\Customer::class,
448+
Customer::class,
446449
['load', 'getSharedStoreIds']
447450
);
448451
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
@@ -463,7 +466,7 @@ public function testAssignCustomerToNonanonymousCart()
463466
}
464467

465468
/**
466-
* @expectedException \Magento\Framework\Exception\StateException
469+
* @expectedException NoSuchEntityException
467470
*/
468471
public function testAssignCustomerNoSuchCustomer()
469472
{
@@ -472,10 +475,9 @@ public function testAssignCustomerNoSuchCustomer()
472475
$storeId = 5;
473476

474477
$quoteMock = $this->createPartialMock(
475-
\Magento\Quote\Model\Quote::class,
478+
Quote::class,
476479
['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
477480
);
478-
$customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
479481

480482
$this->quoteRepositoryMock
481483
->expects($this->once())
@@ -487,36 +489,14 @@ public function testAssignCustomerNoSuchCustomer()
487489
->expects($this->once())
488490
->method('getById')
489491
->with($customerId)
490-
->willReturn($customerMock);
492+
->willThrowException(new NoSuchEntityException());
491493

492-
$customerModelMock = $this->createPartialMock(
493-
\Magento\Customer\Model\Customer::class,
494-
['load', 'getSharedStoreIds']
494+
$this->expectExceptionMessage(
495+
"No such entity."
495496
);
496-
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
497-
$customerModelMock
498-
->expects($this->once())
499-
->method('load')
500-
->with($customerId)
501-
->willReturnSelf();
502-
503-
$customerModelMock
504-
->expects($this->once())
505-
->method('getSharedStoreIds')
506-
->willReturn([$storeId, 'some store value']);
507-
508-
$quoteMock->expects($this->once())->method('getCustomerId')->willReturn(null);
509-
510-
$this->quoteRepositoryMock
511-
->expects($this->once())
512-
->method('getForCustomer')
513-
->with($customerId);
514497

515498
$this->model->assignCustomer($cartId, $customerId, $storeId);
516499

517-
$this->expectExceptionMessage(
518-
"The customer can't be assigned to the cart because the customer already has an active cart."
519-
);
520500
}
521501

522502
public function testAssignCustomer()
@@ -525,18 +505,11 @@ public function testAssignCustomer()
525505
$customerId = 455;
526506
$storeId = 5;
527507

528-
$this->getPropertyValue($this->model, 'quoteIdMaskFactory')
529-
->expects($this->once())
530-
->method('create')
531-
->willReturn($this->quoteIdMock);
532-
$this->quoteIdMock->expects($this->once())->method('load')->with($cartId, 'quote_id')->willReturnSelf();
533-
$this->quoteIdMock->expects($this->once())->method('getId')->willReturn(10);
534-
$this->quoteIdMock->expects($this->once())->method('delete');
535508
$quoteMock = $this->createPartialMock(
536-
\Magento\Quote\Model\Quote::class,
537-
['getCustomerId', 'setCustomer', 'setCustomerIsGuest']
509+
Quote::class,
510+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'merge']
538511
);
539-
$customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
512+
$customerMock = $this->createMock(CustomerInterface::class);
540513

541514
$this->quoteRepositoryMock
542515
->expects($this->once())
@@ -551,7 +524,7 @@ public function testAssignCustomer()
551524
->willReturn($customerMock);
552525

553526
$customerModelMock = $this->createPartialMock(
554-
\Magento\Customer\Model\Customer::class,
527+
Customer::class,
555528
['load', 'getSharedStoreIds']
556529
);
557530
$this->customerFactoryMock->expects($this->once())->method('create')->willReturn($customerModelMock);
@@ -574,10 +547,22 @@ public function testAssignCustomer()
574547
->with($customerId)
575548
->willThrowException(new NoSuchEntityException());
576549

577-
$quoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
578-
$quoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
550+
$activeQuoteMock = $this->createPartialMock(
551+
Quote::class,
552+
['getCustomerId', 'setCustomer', 'setCustomerIsGuest', 'setStoreId', 'setIsActive', 'getIsActive', 'merge']
553+
);
579554

580-
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
555+
$this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($activeQuoteMock);
556+
$activeQuoteMock->expects($this->once())->method('setCustomer')->with($customerMock);
557+
$activeQuoteMock->expects($this->once())->method('setCustomerIsGuest')->with(0);
558+
$activeQuoteMock->expects($this->once())->method('setStoreId')->with($storeId);
559+
$activeQuoteMock->expects($this->once())->method('setIsActive')->with(1);
560+
561+
$activeQuoteMock->expects($this->once())->method('getIsActive')->willReturn(1);
562+
$activeQuoteMock->expects($this->once())->method('merge')->with($quoteMock)->willReturnSelf();
563+
$this->quoteRepositoryMock->expects($this->once())->method('delete')->with($quoteMock);
564+
565+
$this->quoteRepositoryMock->expects($this->once())->method('save')->with($activeQuoteMock);
581566

582567
$this->model->assignCustomer($cartId, $customerId, $storeId);
583568
}
@@ -881,7 +866,7 @@ protected function getQuote(
881866
\Magento\Quote\Model\Quote\Address $shippingAddress = null
882867
) {
883868
$quote = $this->createPartialMock(
884-
\Magento\Quote\Model\Quote::class,
869+
Quote::class,
885870
[
886871
'setIsActive',
887872
'getCustomerEmail',
@@ -928,7 +913,7 @@ protected function getQuote(
928913
->willReturn($payment);
929914

930915
$customer = $this->createPartialMock(
931-
\Magento\Customer\Model\Customer::class,
916+
Customer::class,
932917
['getDefaultBilling', 'getId']
933918
);
934919
$quote->expects($this->any())->method('getCustomerId')->willReturn($customerId);
@@ -1021,7 +1006,7 @@ protected function prepareOrderFactory(
10211006
public function testGetCartForCustomer()
10221007
{
10231008
$customerId = 100;
1024-
$cartMock = $this->createMock(\Magento\Quote\Model\Quote::class);
1009+
$cartMock = $this->createMock(Quote::class);
10251010
$this->quoteRepositoryMock->expects($this->once())
10261011
->method('getActiveForCustomer')
10271012
->with($customerId)

0 commit comments

Comments
 (0)