|
| 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\Quote\Test\Unit\Model; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Framework\Exception\InputException; |
| 13 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 14 | +use Magento\Quote\Api\CartRepositoryInterface; |
| 15 | +use Magento\Quote\Api\Data\AddressInterface; |
| 16 | +use Magento\Quote\Model\Quote; |
| 17 | +use Magento\Quote\Model\Quote\TotalsCollector; |
| 18 | +use Magento\Quote\Model\QuoteAddressValidator; |
| 19 | +use Magento\Quote\Model\ShippingAddressManagement; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | + |
| 23 | +class ShippingAddressManagementTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ShippingAddressManagement |
| 27 | + */ |
| 28 | + private ShippingAddressManagement $model; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var CartRepositoryInterface |
| 32 | + */ |
| 33 | + private $quoteRepositoryMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var QuoteAddressValidator |
| 37 | + */ |
| 38 | + private $addressValidatorMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var LoggerInterface |
| 42 | + */ |
| 43 | + private $loggerMock; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var AddressRepositoryInterface |
| 47 | + */ |
| 48 | + private $addressRepositoryMock; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ScopeConfigInterface |
| 52 | + */ |
| 53 | + private $scopeConfigMock; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var TotalsCollector |
| 57 | + */ |
| 58 | + private $totalsCollectorMock; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var Quote |
| 62 | + */ |
| 63 | + private $quoteMock; |
| 64 | + |
| 65 | + /** |
| 66 | + * @inheritdoc |
| 67 | + */ |
| 68 | + protected function setUp(): void |
| 69 | + { |
| 70 | + $this->quoteRepositoryMock = $this->createMock(CartRepositoryInterface::class); |
| 71 | + $this->addressValidatorMock = $this->createMock(QuoteAddressValidator::class); |
| 72 | + $this->loggerMock = $this->createMock(LoggerInterface::class); |
| 73 | + $this->addressRepositoryMock = $this->createMock(AddressRepositoryInterface::class); |
| 74 | + $this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class); |
| 75 | + $this->totalsCollectorMock = $this->createMock(TotalsCollector::class); |
| 76 | + $this->quoteMock = $this->createMock(Quote::class); |
| 77 | + $this->model = new ShippingAddressManagement( |
| 78 | + $this->quoteRepositoryMock, |
| 79 | + $this->addressValidatorMock, |
| 80 | + $this->loggerMock, |
| 81 | + $this->addressRepositoryMock, |
| 82 | + $this->scopeConfigMock, |
| 83 | + $this->totalsCollectorMock |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @throws InputException |
| 89 | + * @throws NoSuchEntityException |
| 90 | + * @dataProvider assignDataProvider |
| 91 | + */ |
| 92 | + public function testAssign(bool $saveInAddressBook, bool $showCompany): void |
| 93 | + { |
| 94 | + $cartId = $customerId = 123; |
| 95 | + $addressMock = $this->getMockBuilder(AddressInterface::class) |
| 96 | + ->addMethods(['setCollectShippingRates', 'save', 'importCustomerAddressData']) |
| 97 | + ->disableOriginalConstructor() |
| 98 | + ->getMockForAbstractClass(); |
| 99 | + $this->quoteMock |
| 100 | + ->expects($this->once()) |
| 101 | + ->method('isVirtual') |
| 102 | + ->willReturn(false); |
| 103 | + $this->quoteRepositoryMock |
| 104 | + ->expects($this->once()) |
| 105 | + ->method('getActive') |
| 106 | + ->with($cartId) |
| 107 | + ->willReturn($this->quoteMock); |
| 108 | + $addressMock |
| 109 | + ->expects($this->once()) |
| 110 | + ->method('getSaveInAddressBook') |
| 111 | + ->willReturn($saveInAddressBook); |
| 112 | + $addressMock |
| 113 | + ->expects($this->once()) |
| 114 | + ->method('getSameAsBilling') |
| 115 | + ->willReturn(true); |
| 116 | + $addressMock |
| 117 | + ->expects($this->once()) |
| 118 | + ->method('getCustomerAddressId') |
| 119 | + ->willReturn($customerId); |
| 120 | + $addressMock |
| 121 | + ->expects($saveInAddressBook && !$showCompany ? $this->once() : $this->never()) |
| 122 | + ->method('setCompany') |
| 123 | + ->with(null); |
| 124 | + $addressMock |
| 125 | + ->expects($this->once()) |
| 126 | + ->method('importCustomerAddressData') |
| 127 | + ->willReturn($addressMock); |
| 128 | + $addressMock |
| 129 | + ->expects($this->once()) |
| 130 | + ->method('setSameAsBilling') |
| 131 | + ->with(true); |
| 132 | + $addressMock |
| 133 | + ->expects($this->once()) |
| 134 | + ->method('setSaveInAddressBook') |
| 135 | + ->with($saveInAddressBook); |
| 136 | + $addressMock->method('setCollectShippingRates'); |
| 137 | + $addressMock->method('save'); |
| 138 | + $this->scopeConfigMock |
| 139 | + ->expects($saveInAddressBook ? $this->once() : $this->never()) |
| 140 | + ->method('getValue') |
| 141 | + ->willReturn($showCompany); |
| 142 | + $this->addressValidatorMock |
| 143 | + ->expects($this->once()) |
| 144 | + ->method('validateForCart'); |
| 145 | + $this->quoteMock |
| 146 | + ->expects($this->once()) |
| 147 | + ->method('setShippingAddress') |
| 148 | + ->with($addressMock); |
| 149 | + $this->quoteMock |
| 150 | + ->method('getShippingAddress') |
| 151 | + ->willReturn($addressMock); |
| 152 | + $this->model->assign($cartId, $addressMock); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @return array |
| 157 | + */ |
| 158 | + public function assignDataProvider(): array |
| 159 | + { |
| 160 | + return [ |
| 161 | + [true, true], |
| 162 | + [true, false], |
| 163 | + [false, true], |
| 164 | + [false, false], |
| 165 | + ]; |
| 166 | + } |
| 167 | +} |
0 commit comments