|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\LoginAsCustomerQuote\Test\Unit\Plugin\LoginAsCustomerApi; |
| 9 | + |
| 10 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 11 | +use Magento\Customer\Model\Session as CustomerSession; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\LoginAsCustomerApi\Api\AuthenticateCustomerBySecretInterface; |
| 14 | +use Magento\LoginAsCustomerQuote\Plugin\LoginAsCustomerApi\ProcessShoppingCartPlugin; |
| 15 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 16 | +use Magento\Quote\Model\Quote; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Unit tests for ProcessShoppingCartPlugin |
| 22 | + */ |
| 23 | +class ProcessShoppingCartPluginTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var CustomerSession|MockObject |
| 27 | + */ |
| 28 | + private $customerSession; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var CheckoutSession|MockObject |
| 32 | + */ |
| 33 | + private $checkoutSession; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var CartRepositoryInterface|MockObject |
| 37 | + */ |
| 38 | + private $quoteRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Quote|MockObject |
| 42 | + */ |
| 43 | + private $quote; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var ProcessShoppingCartPlugin |
| 47 | + */ |
| 48 | + private $plugin; |
| 49 | + |
| 50 | + /** |
| 51 | + * Set up test environment |
| 52 | + */ |
| 53 | + protected function setUp(): void |
| 54 | + { |
| 55 | + $this->customerSession = $this->getMockBuilder(CustomerSession::class)->disableOriginalConstructor()->getMock(); |
| 56 | + $this->checkoutSession = $this->getMockBuilder(CheckoutSession::class)->disableOriginalConstructor()->getMock(); |
| 57 | + $this->quoteRepository = $this->getMockBuilder(CartRepositoryInterface::class) |
| 58 | + ->disableOriginalConstructor() |
| 59 | + ->getMockForAbstractClass(); |
| 60 | + $this->quote = $this->getMockBuilder(Quote::class)->disableOriginalConstructor()->getMock(); |
| 61 | + $this->plugin = new ProcessShoppingCartPlugin( |
| 62 | + $this->customerSession, |
| 63 | + $this->checkoutSession, |
| 64 | + $this->quoteRepository |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test beforeExecute when if condition is true |
| 70 | + */ |
| 71 | + public function testBeforeExecuteWithNoCustomerIdAndQuoteId(): void |
| 72 | + { |
| 73 | + $this->customerSession->expects($this->once())->method('getId')->willReturn(null); |
| 74 | + $this->checkoutSession->expects($this->exactly(2))->method('getQuote')->willReturn($this->quote); |
| 75 | + $this->quote->expects($this->once())->method('getId')->willReturn(123); |
| 76 | + $this->quote->expects($this->once())->method('removeAllItems')->willReturnSelf(); |
| 77 | + $this->quote->expects($this->once())->method('setCustomerIsGuest')->with(0)->willReturnSelf(); |
| 78 | + $this->quoteRepository->expects($this->once())->method('save')->with($this->quote); |
| 79 | + $subject = $this->createMock(AuthenticateCustomerBySecretInterface::class); |
| 80 | + $secret = 'test-secret'; |
| 81 | + $result = $this->plugin->beforeExecute($subject, $secret); |
| 82 | + $this->assertNull($result); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test beforeExecute when if condition is false |
| 87 | + * |
| 88 | + * @throws LocalizedException |
| 89 | + */ |
| 90 | + public function testBeforeExecuteWithCustomerId(): void |
| 91 | + { |
| 92 | + $this->customerSession->expects($this->once())->method('getId')->willReturn(456); |
| 93 | + $this->checkoutSession->expects($this->never())->method('getQuote'); |
| 94 | + $this->quote->expects($this->never())->method('getId'); |
| 95 | + $this->noGuestCart(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Test beforeExecute when if condition is false (no quote ID) |
| 100 | + * |
| 101 | + * @throws LocalizedException |
| 102 | + */ |
| 103 | + public function testBeforeExecuteWithNoQuoteId(): void |
| 104 | + { |
| 105 | + $this->customerSession->expects($this->once())->method('getId')->willReturn(null); |
| 106 | + $this->checkoutSession->expects($this->once())->method('getQuote')->willReturn($this->quote); |
| 107 | + $this->quote->expects($this->once())->method('getId')->willReturn(null); |
| 108 | + $this->noGuestCart(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Expected that no guest cart exist |
| 113 | + * |
| 114 | + * @return void |
| 115 | + * @throws LocalizedException |
| 116 | + */ |
| 117 | + private function noGuestCart(): void |
| 118 | + { |
| 119 | + $this->quote->expects($this->never())->method('removeAllItems'); |
| 120 | + $this->quote->expects($this->never())->method('setCustomerIsGuest'); |
| 121 | + $this->quoteRepository->expects($this->never())->method('save'); |
| 122 | + $subject = $this->getMockBuilder(AuthenticateCustomerBySecretInterface::class) |
| 123 | + ->disableOriginalConstructor() |
| 124 | + ->getMockForAbstractClass(); |
| 125 | + $secret = 'test-secret'; |
| 126 | + $result = $this->plugin->beforeExecute($subject, $secret); |
| 127 | + $this->assertNull($result); |
| 128 | + } |
| 129 | +} |
0 commit comments