|
34 | 34 | use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection; |
35 | 35 | use Magento\Store\Api\Data\StoreInterface; |
36 | 36 | use PHPUnit\Framework\MockObject\MockObject; |
| 37 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
37 | 38 | use PHPUnit\Framework\TestCase; |
38 | 39 |
|
39 | 40 | /** |
@@ -638,4 +639,95 @@ public static function setShippingAsBillingDataProvider(): array |
638 | 639 | ] |
639 | 640 | ]; |
640 | 641 | } |
| 642 | + |
| 643 | + public function testGetQuoteAssignsCustomerWhenCustomerIdPresent(): void |
| 644 | + { |
| 645 | + $quote = $this->getMockBuilder(Quote::class) |
| 646 | + ->disableOriginalConstructor() |
| 647 | + ->onlyMethods(['assignCustomer']) |
| 648 | + ->addMethods(['getCustomerId']) |
| 649 | + ->getMock(); |
| 650 | + |
| 651 | + $quote->expects($this->once()) |
| 652 | + ->method('getCustomerId') |
| 653 | + ->willReturn(self::CUSTOMER_ID); |
| 654 | + |
| 655 | + $customerData = $this->getMockBuilder(CustomerInterface::class) |
| 656 | + ->getMockForAbstractClass(); |
| 657 | + |
| 658 | + $customerRepository = $this->getMockBuilder(CustomerRepositoryInterface::class) |
| 659 | + ->getMockForAbstractClass(); |
| 660 | + $customerRepository->expects($this->once()) |
| 661 | + ->method('getById') |
| 662 | + ->with(self::CUSTOMER_ID) |
| 663 | + ->willReturn($customerData); |
| 664 | + |
| 665 | + $quote->expects($this->once()) |
| 666 | + ->method('assignCustomer') |
| 667 | + ->with($customerData); |
| 668 | + |
| 669 | + $this->sessionQuote->expects($this->once()) |
| 670 | + ->method('getQuote') |
| 671 | + ->willReturn($quote); |
| 672 | + |
| 673 | + $subject = $this->createAdminOrderCreateWithCustomerRepository($customerRepository); |
| 674 | + |
| 675 | + $result = $subject->getQuote(); |
| 676 | + |
| 677 | + $this->assertSame($quote, $result); |
| 678 | + } |
| 679 | + |
| 680 | + public function testGetQuoteSkipsAssignWhenNoCustomerId(): void |
| 681 | + { |
| 682 | + $quote = $this->getMockBuilder(Quote::class) |
| 683 | + ->disableOriginalConstructor() |
| 684 | + ->onlyMethods(['assignCustomer']) |
| 685 | + ->addMethods(['getCustomerId']) |
| 686 | + ->getMock(); |
| 687 | + |
| 688 | + $quote->expects($this->once()) |
| 689 | + ->method('getCustomerId') |
| 690 | + ->willReturn(0); |
| 691 | + |
| 692 | + $quote->expects($this->never()) |
| 693 | + ->method('assignCustomer'); |
| 694 | + |
| 695 | + $customerRepository = $this->getMockBuilder(CustomerRepositoryInterface::class) |
| 696 | + ->getMockForAbstractClass(); |
| 697 | + $customerRepository->expects($this->never()) |
| 698 | + ->method('getById'); |
| 699 | + |
| 700 | + $this->sessionQuote->expects($this->once()) |
| 701 | + ->method('getQuote') |
| 702 | + ->willReturn($quote); |
| 703 | + |
| 704 | + $subject = $this->createAdminOrderCreateWithCustomerRepository($customerRepository); |
| 705 | + |
| 706 | + $result = $subject->getQuote(); |
| 707 | + |
| 708 | + $this->assertSame($quote, $result); |
| 709 | + } |
| 710 | + |
| 711 | + private function createAdminOrderCreateWithCustomerRepository( |
| 712 | + CustomerRepositoryInterface $customerRepository |
| 713 | + ): Create { |
| 714 | + $objectManagerHelper = new ObjectManagerHelper($this); |
| 715 | + return $objectManagerHelper->getObject( |
| 716 | + Create::class, |
| 717 | + [ |
| 718 | + '_objectManager' => $this->objectManager, |
| 719 | + 'messageManager' => $this->messageManager, |
| 720 | + 'quoteSession' => $this->sessionQuote, |
| 721 | + 'metadataFormFactory' => $this->formFactory, |
| 722 | + 'customerFactory' => $this->customerFactory, |
| 723 | + 'groupRepository' => $this->groupRepository, |
| 724 | + 'quoteItemUpdater' => $this->itemUpdater, |
| 725 | + 'customerMapper' => $this->customerMapper, |
| 726 | + 'dataObjectHelper' => $this->dataObjectHelper, |
| 727 | + 'quoteRepository' => $this->quoteRepository, |
| 728 | + 'quoteFactory' => $this->quoteFactory, |
| 729 | + 'customerRepository' => $customerRepository, |
| 730 | + ] |
| 731 | + ); |
| 732 | + } |
641 | 733 | } |
0 commit comments