7
7
8
8
namespace Magento \Quote \Test \Unit \Model ;
9
9
10
+ use Magento \Customer \Api \Data \CustomerInterface ;
11
+ use Magento \Customer \Model \Customer ;
10
12
use Magento \Framework \App \RequestInterface ;
11
- use Magento \Framework \Exception \NoSuchEntityException ;
12
13
13
14
use Magento \Framework \HTTP \PhpEnvironment \RemoteAddress ;
14
15
use Magento \Quote \Model \CustomerManagement ;
16
+ use Magento \Quote \Model \Quote ;
15
17
use Magento \Quote \Model \QuoteIdMaskFactory ;
16
18
use Magento \Sales \Api \Data \OrderAddressInterface ;
17
19
@@ -199,7 +201,7 @@ protected function setUp()
199
201
);
200
202
201
203
$ this ->quoteMock = $ this ->createPartialMock (
202
- \ Magento \ Quote \ Model \ Quote::class,
204
+ Quote::class,
203
205
[
204
206
'assignCustomer ' ,
205
207
'collectTotals ' ,
@@ -275,7 +277,7 @@ public function testCreateEmptyCartAnonymous()
275
277
$ storeId = 345 ;
276
278
$ quoteId = 2311 ;
277
279
278
- $ quoteMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
280
+ $ quoteMock = $ this ->createMock (Quote::class);
279
281
$ quoteAddress = $ this ->createPartialMock (
280
282
\Magento \Quote \Model \Quote \Address::class,
281
283
['setCollectShippingRates ' ]
@@ -306,14 +308,14 @@ public function testCreateEmptyCartForCustomer()
306
308
$ quoteId = 2311 ;
307
309
$ userId = 567 ;
308
310
309
- $ quoteMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
311
+ $ quoteMock = $ this ->createMock (Quote::class);
310
312
311
313
$ this ->quoteRepositoryMock
312
314
->expects ($ this ->once ())
313
315
->method ('getActiveForCustomer ' )
314
316
->with ($ userId )
315
- ->willThrowException (new NoSuchEntityException ());
316
- $ customer = $ this ->getMockBuilder (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class)
317
+ ->willThrowException (new \ Magento \ Framework \ Exception \ NoSuchEntityException ());
318
+ $ customer = $ this ->getMockBuilder (CustomerInterface::class)
317
319
->setMethods (['getDefaultBilling ' ])->disableOriginalConstructor ()->getMockForAbstractClass ();
318
320
$ quoteAddress = $ this ->createPartialMock (
319
321
\Magento \Quote \Model \Quote \Address::class,
@@ -342,14 +344,14 @@ public function testCreateEmptyCartForCustomerReturnExistsQuote()
342
344
$ storeId = 345 ;
343
345
$ userId = 567 ;
344
346
345
- $ quoteMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
347
+ $ quoteMock = $ this ->createMock (Quote::class);
346
348
347
349
$ this ->quoteRepositoryMock
348
350
->expects ($ this ->once ())
349
351
->method ('getActiveForCustomer ' )
350
352
->with ($ userId )->willReturn ($ quoteMock );
351
353
352
- $ customer = $ this ->getMockBuilder (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class)
354
+ $ customer = $ this ->getMockBuilder (CustomerInterface::class)
353
355
->setMethods (['getDefaultBilling ' ])->disableOriginalConstructor ()->getMockForAbstractClass ();
354
356
$ quoteAddress = $ this ->createPartialMock (
355
357
\Magento \Quote \Model \Quote \Address::class,
@@ -379,8 +381,8 @@ public function testAssignCustomerFromAnotherStore()
379
381
$ customerId = 455 ;
380
382
$ storeId = 5 ;
381
383
382
- $ quoteMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
383
- $ customerMock = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
384
+ $ quoteMock = $ this ->createMock (Quote::class);
385
+ $ customerMock = $ this ->createMock (CustomerInterface::class);
384
386
385
387
$ this ->quoteRepositoryMock
386
388
->expects ($ this ->once ())
@@ -395,7 +397,7 @@ public function testAssignCustomerFromAnotherStore()
395
397
->willReturn ($ customerMock );
396
398
397
399
$ customerModelMock = $ this ->createPartialMock (
398
- \ Magento \ Customer \ Model \ Customer::class,
400
+ Customer::class,
399
401
['load ' , 'getSharedStoreIds ' ]
400
402
);
401
403
$ this ->customerFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customerModelMock );
@@ -424,10 +426,10 @@ public function testAssignCustomerToNonanonymousCart()
424
426
$ storeId = 5 ;
425
427
426
428
$ quoteMock = $ this ->createPartialMock (
427
- \ Magento \ Quote \ Model \ Quote::class,
429
+ Quote::class,
428
430
['getCustomerId ' , 'setCustomer ' , 'setCustomerIsGuest ' ]
429
431
);
430
- $ customerMock = $ this ->createMock (\ Magento \ Customer \ Api \ Data \ CustomerInterface::class);
432
+ $ customerMock = $ this ->createMock (CustomerInterface::class);
431
433
432
434
$ this ->quoteRepositoryMock
433
435
->expects ($ this ->once ())
@@ -442,7 +444,7 @@ public function testAssignCustomerToNonanonymousCart()
442
444
->willReturn ($ customerMock );
443
445
444
446
$ customerModelMock = $ this ->createPartialMock (
445
- \ Magento \ Customer \ Model \ Customer::class,
447
+ Customer::class,
446
448
['load ' , 'getSharedStoreIds ' ]
447
449
);
448
450
$ this ->customerFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customerModelMock );
@@ -463,7 +465,7 @@ public function testAssignCustomerToNonanonymousCart()
463
465
}
464
466
465
467
/**
466
- * @expectedException \Magento\Framework\Exception\StateException
468
+ * @expectedException \Magento\Framework\Exception\NoSuchEntityException
467
469
*/
468
470
public function testAssignCustomerNoSuchCustomer ()
469
471
{
@@ -472,10 +474,51 @@ public function testAssignCustomerNoSuchCustomer()
472
474
$ storeId = 5 ;
473
475
474
476
$ quoteMock = $ this ->createPartialMock (
475
- \ Magento \ Quote \ Model \ Quote::class,
477
+ Quote::class,
476
478
['getCustomerId ' , 'setCustomer ' , 'setCustomerIsGuest ' ]
477
479
);
478
- $ customerMock = $ this ->createMock (\Magento \Customer \Api \Data \CustomerInterface::class);
480
+
481
+ $ this ->quoteRepositoryMock
482
+ ->expects ($ this ->once ())
483
+ ->method ('getActive ' )
484
+ ->with ($ cartId )
485
+ ->willReturn ($ quoteMock );
486
+
487
+ $ this ->customerRepositoryMock
488
+ ->expects ($ this ->once ())
489
+ ->method ('getById ' )
490
+ ->with ($ customerId )
491
+ ->willThrowException (new \Magento \Framework \Exception \NoSuchEntityException ());
492
+
493
+ $ this ->expectExceptionMessage (
494
+ "No such entity. "
495
+ );
496
+
497
+ $ this ->model ->assignCustomer ($ cartId , $ customerId , $ storeId );
498
+ }
499
+
500
+ public function testAssignCustomerWithActiveCart ()
501
+ {
502
+ $ cartId = 220 ;
503
+ $ customerId = 455 ;
504
+ $ storeId = 5 ;
505
+
506
+ $ this ->getPropertyValue ($ this ->model , 'quoteIdMaskFactory ' )
507
+ ->expects ($ this ->once ())
508
+ ->method ('create ' )
509
+ ->willReturn ($ this ->quoteIdMock );
510
+
511
+ $ quoteMock = $ this ->createPartialMock (
512
+ Quote::class,
513
+ ['getCustomerId ' , 'setCustomer ' , 'setCustomerIsGuest ' , 'setIsActive ' , 'getIsActive ' , 'merge ' ]
514
+ );
515
+
516
+ $ activeQuoteMock = $ this ->createPartialMock (
517
+ Quote::class,
518
+ ['getCustomerId ' , 'setCustomer ' , 'setCustomerIsGuest ' , 'setIsActive ' , 'getIsActive ' , 'merge ' ]
519
+ );
520
+
521
+ $ customerMock = $ this ->createMock (CustomerInterface::class);
479
522
480
523
$ this ->quoteRepositoryMock
481
524
->expects ($ this ->once ())
@@ -490,7 +533,7 @@ public function testAssignCustomerNoSuchCustomer()
490
533
->willReturn ($ customerMock );
491
534
492
535
$ customerModelMock = $ this ->createPartialMock (
493
- \ Magento \ Customer \ Model \ Customer::class,
536
+ Customer::class,
494
537
['load ' , 'getSharedStoreIds ' ]
495
538
);
496
539
$ this ->customerFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customerModelMock );
@@ -506,17 +549,26 @@ public function testAssignCustomerNoSuchCustomer()
506
549
->willReturn ([$ storeId , 'some store value ' ]);
507
550
508
551
$ quoteMock ->expects ($ this ->once ())->method ('getCustomerId ' )->willReturn (null );
509
-
510
552
$ this ->quoteRepositoryMock
511
553
->expects ($ this ->once ())
512
554
->method ('getForCustomer ' )
513
- ->with ($ customerId );
555
+ ->with ($ customerId )
556
+ ->willReturn ($ activeQuoteMock );
514
557
515
- $ this ->model ->assignCustomer ($ cartId , $ customerId , $ storeId );
558
+ $ quoteMock ->expects ($ this ->once ())->method ('merge ' )->with ($ activeQuoteMock )->willReturnSelf ();
559
+ $ activeQuoteMock ->expects ($ this ->once ())->method ('setIsActive ' )->with (0 );
560
+ $ this ->quoteRepositoryMock ->expects ($ this ->atLeastOnce ())->method ('save ' )->with ($ activeQuoteMock );
516
561
517
- $ this ->expectExceptionMessage (
518
- "The customer can't be assigned to the cart because the customer already has an active cart. "
519
- );
562
+ $ quoteMock ->expects ($ this ->once ())->method ('setCustomer ' )->with ($ customerMock );
563
+ $ quoteMock ->expects ($ this ->once ())->method ('setCustomerIsGuest ' )->with (0 );
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 ->atLeastOnce ())->method ('save ' )->with ($ quoteMock );
570
+
571
+ $ this ->model ->assignCustomer ($ cartId , $ customerId , $ storeId );
520
572
}
521
573
522
574
public function testAssignCustomer ()
@@ -529,15 +581,13 @@ public function testAssignCustomer()
529
581
->expects ($ this ->once ())
530
582
->method ('create ' )
531
583
->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 ' );
584
+
535
585
$ quoteMock = $ this ->createPartialMock (
536
- \ Magento \ Quote \ Model \ Quote::class,
537
- ['getCustomerId ' , 'setCustomer ' , 'setCustomerIsGuest ' ]
586
+ Quote::class,
587
+ ['getCustomerId ' , 'setCustomer ' , 'setCustomerIsGuest ' , ' setIsActive ' , ' getIsActive ' , ' merge ' ]
538
588
);
539
- $ customerMock = $ this ->createMock (\Magento \Customer \Api \Data \CustomerInterface::class);
540
589
590
+ $ customerMock = $ this ->createMock (CustomerInterface::class);
541
591
$ this ->quoteRepositoryMock
542
592
->expects ($ this ->once ())
543
593
->method ('getActive ' )
@@ -551,10 +601,12 @@ public function testAssignCustomer()
551
601
->willReturn ($ customerMock );
552
602
553
603
$ customerModelMock = $ this ->createPartialMock (
554
- \ Magento \ Customer \ Model \ Customer::class,
604
+ Customer::class,
555
605
['load ' , 'getSharedStoreIds ' ]
556
606
);
607
+
557
608
$ this ->customerFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ customerModelMock );
609
+
558
610
$ customerModelMock
559
611
->expects ($ this ->once ())
560
612
->method ('load ' )
@@ -572,11 +624,17 @@ public function testAssignCustomer()
572
624
->expects ($ this ->once ())
573
625
->method ('getForCustomer ' )
574
626
->with ($ customerId )
575
- ->willThrowException (new NoSuchEntityException ());
627
+ ->willThrowException (new \Magento \Framework \Exception \NoSuchEntityException ());
628
+
629
+ $ quoteMock ->expects ($ this ->never ())->method ('merge ' );
576
630
577
631
$ quoteMock ->expects ($ this ->once ())->method ('setCustomer ' )->with ($ customerMock );
578
632
$ quoteMock ->expects ($ this ->once ())->method ('setCustomerIsGuest ' )->with (0 );
633
+ $ quoteMock ->expects ($ this ->once ())->method ('setIsActive ' )->with (1 );
579
634
635
+ $ this ->quoteIdMock ->expects ($ this ->once ())->method ('load ' )->with ($ cartId , 'quote_id ' )->willReturnSelf ();
636
+ $ this ->quoteIdMock ->expects ($ this ->once ())->method ('getId ' )->willReturn (10 );
637
+ $ this ->quoteIdMock ->expects ($ this ->once ())->method ('delete ' );
580
638
$ this ->quoteRepositoryMock ->expects ($ this ->once ())->method ('save ' )->with ($ quoteMock );
581
639
582
640
$ this ->model ->assignCustomer ($ cartId , $ customerId , $ storeId );
@@ -881,7 +939,7 @@ protected function getQuote(
881
939
\Magento \Quote \Model \Quote \Address $ shippingAddress = null
882
940
) {
883
941
$ quote = $ this ->createPartialMock (
884
- \ Magento \ Quote \ Model \ Quote::class,
942
+ Quote::class,
885
943
[
886
944
'setIsActive ' ,
887
945
'getCustomerEmail ' ,
@@ -928,7 +986,7 @@ protected function getQuote(
928
986
->willReturn ($ payment );
929
987
930
988
$ customer = $ this ->createPartialMock (
931
- \ Magento \ Customer \ Model \ Customer::class,
989
+ Customer::class,
932
990
['getDefaultBilling ' , 'getId ' ]
933
991
);
934
992
$ quote ->expects ($ this ->any ())->method ('getCustomerId ' )->willReturn ($ customerId );
@@ -1016,12 +1074,12 @@ protected function prepareOrderFactory(
1016
1074
}
1017
1075
1018
1076
/**
1019
- * @throws NoSuchEntityException
1077
+ * @throws \Magento\Framework\Exception\ NoSuchEntityException
1020
1078
*/
1021
1079
public function testGetCartForCustomer ()
1022
1080
{
1023
1081
$ customerId = 100 ;
1024
- $ cartMock = $ this ->createMock (\ Magento \ Quote \ Model \ Quote::class);
1082
+ $ cartMock = $ this ->createMock (Quote::class);
1025
1083
$ this ->quoteRepositoryMock ->expects ($ this ->once ())
1026
1084
->method ('getActiveForCustomer ' )
1027
1085
->with ($ customerId )
0 commit comments