|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Multishipping\Test\Unit\Model\Cart\Controller; |
| 9 | + |
| 10 | +use Magento\Checkout\Controller\Sidebar\UpdateItemQty; |
| 11 | +use Magento\Checkout\Model\Session; |
| 12 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 13 | +use Magento\Customer\Api\Data\AddressInterface; |
| 14 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 15 | +use Magento\Framework\App\RequestInterface; |
| 16 | +use Magento\Framework\Exception\LocalizedException; |
| 17 | +use Magento\Multishipping\Model\Cart\Controller\MiniCartPlugin; |
| 18 | +use Magento\Multishipping\Model\DisableMultishipping; |
| 19 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 20 | +use Magento\Quote\Model\Quote; |
| 21 | +use Magento\Quote\Model\Quote\Address; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +/** |
| 26 | + * Test shipping addresses and item assignments after MultiShipping flow |
| 27 | + * |
| 28 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 29 | + */ |
| 30 | +class MiniCartPluginTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var MiniCartPlugin |
| 34 | + */ |
| 35 | + private $model; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var MockObject |
| 39 | + */ |
| 40 | + private $cartRepositoryMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var MockObject |
| 44 | + */ |
| 45 | + private $checkoutSessionMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var MockObject |
| 49 | + */ |
| 50 | + private $addressRepositoryMock; |
| 51 | + |
| 52 | + protected function setUp(): void |
| 53 | + { |
| 54 | + $this->cartRepositoryMock = $this->getMockForAbstractClass(CartRepositoryInterface::class); |
| 55 | + $this->checkoutSessionMock = $this->createMock(Session::class); |
| 56 | + $this->addressRepositoryMock = $this->getMockForAbstractClass(AddressRepositoryInterface::class); |
| 57 | + $disableMultishippingMock = $this->createMock(DisableMultishipping::class); |
| 58 | + $this->model = new MiniCartPlugin( |
| 59 | + $this->cartRepositoryMock, |
| 60 | + $this->checkoutSessionMock, |
| 61 | + $this->addressRepositoryMock, |
| 62 | + $disableMultishippingMock |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test cart plugin |
| 68 | + * |
| 69 | + * @param string $actionName |
| 70 | + * @param int $addressId |
| 71 | + * @param int $customerAddressId |
| 72 | + * @param bool $isMultiShippingAddresses |
| 73 | + * @throws LocalizedException |
| 74 | + * @dataProvider getDataDataProvider |
| 75 | + */ |
| 76 | + public function testBeforeDispatch( |
| 77 | + string $actionName, |
| 78 | + int $addressId, |
| 79 | + int $customerAddressId, |
| 80 | + bool $isMultiShippingAddresses |
| 81 | + ): void { |
| 82 | + $requestMock = $this->getMockForAbstractClass(RequestInterface::class); |
| 83 | + $quoteMock = $this->createPartialMock(Quote::class, [ |
| 84 | + 'isMultipleShippingAddresses', |
| 85 | + 'getAllShippingAddresses', |
| 86 | + 'removeAddress', |
| 87 | + 'getShippingAddress', |
| 88 | + 'getCustomer' |
| 89 | + ]); |
| 90 | + $requestMock->method('getActionName') |
| 91 | + ->willReturn($actionName); |
| 92 | + $this->checkoutSessionMock->method('getQuote') |
| 93 | + ->willReturn($quoteMock); |
| 94 | + |
| 95 | + $addressMock = $this->createMock(Address::class); |
| 96 | + $addressMock->method('getId') |
| 97 | + ->willReturn($addressId); |
| 98 | + |
| 99 | + $quoteMock->method('isMultipleShippingAddresses') |
| 100 | + ->willReturn($isMultiShippingAddresses); |
| 101 | + $quoteMock->method('getAllShippingAddresses') |
| 102 | + ->willReturn([$addressMock]); |
| 103 | + $quoteMock->method('removeAddress') |
| 104 | + ->with($addressId)->willReturnSelf(); |
| 105 | + |
| 106 | + $shippingAddressMock = $this->createMock(Address::class); |
| 107 | + $quoteMock->method('getShippingAddress') |
| 108 | + ->willReturn($shippingAddressMock); |
| 109 | + $customerMock = $this->getMockForAbstractClass(CustomerInterface::class); |
| 110 | + $quoteMock->method('getCustomer') |
| 111 | + ->willReturn($customerMock); |
| 112 | + $customerMock->method('getDefaultShipping') |
| 113 | + ->willReturn($customerAddressId); |
| 114 | + |
| 115 | + $customerAddressMock = $this->getMockForAbstractClass(AddressInterface::class); |
| 116 | + $this->addressRepositoryMock->method('getById') |
| 117 | + ->with($customerAddressId) |
| 118 | + ->willReturn($customerAddressMock); |
| 119 | + |
| 120 | + $shippingAddressMock->method('importCustomerAddressData') |
| 121 | + ->with($customerAddressMock) |
| 122 | + ->willReturnSelf(); |
| 123 | + |
| 124 | + $this->cartRepositoryMock->expects($this->any()) |
| 125 | + ->method('save') |
| 126 | + ->with($quoteMock); |
| 127 | + |
| 128 | + $this->model->beforeDispatch( |
| 129 | + $this->createMock(UpdateItemQty::class), |
| 130 | + $requestMock |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @return array |
| 136 | + */ |
| 137 | + public function getDataDataProvider() |
| 138 | + { |
| 139 | + return [ |
| 140 | + 'test with `add` action and multi shipping address enabled' => ['add', 100, 200, true], |
| 141 | + 'test with `add` action and multi shipping address disabled' => ['add', 100, 200, false], |
| 142 | + 'test with `edit` action and multi shipping address disabled' => ['add', 110, 200, false] |
| 143 | + ]; |
| 144 | + } |
| 145 | +} |
0 commit comments