|
| 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\Sales\Test\Unit\Block\Adminhtml\Order\Create\Form; |
| 9 | + |
| 10 | +use Magento\Backend\Model\Session\Quote as QuoteSession; |
| 11 | +use Magento\Store\Model\Store; |
| 12 | +use Magento\Directory\Helper\Data as DirectoryHelper; |
| 13 | +use Magento\Eav\Model\AttributeDataFactory; |
| 14 | +use Magento\Sales\Block\Adminhtml\Order\Create\Form\Address; |
| 15 | +use Magento\Customer\Api\AddressRepositoryInterface; |
| 16 | +use Magento\Customer\Api\Data\AddressSearchResultsInterface; |
| 17 | +use Magento\Customer\Api\Data\AddressInterface; |
| 18 | +use Magento\Customer\Model\Metadata\Form; |
| 19 | +use Magento\Customer\Model\Metadata\FormFactory; |
| 20 | +use Magento\Customer\Model\Address\Mapper; |
| 21 | +use Magento\Framework\Api\FilterBuilder; |
| 22 | +use Magento\Framework\Api\Filter; |
| 23 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 24 | +use Magento\Framework\Api\SearchCriteria; |
| 25 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 28 | + |
| 29 | +/** |
| 30 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 31 | + */ |
| 32 | +class AddressTest extends TestCase |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var QuoteSession|MockObject |
| 36 | + */ |
| 37 | + private $quoteSession; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var Store|MockObject |
| 41 | + */ |
| 42 | + private $store; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var DirectoryHelper|MockObject |
| 46 | + */ |
| 47 | + private $directoryHelper; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var int |
| 51 | + */ |
| 52 | + private $defaultCountryId; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var int |
| 56 | + */ |
| 57 | + private $customerId; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var int |
| 61 | + */ |
| 62 | + private $addressId; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var FormFactory|MockObject |
| 66 | + */ |
| 67 | + private $formFactory; |
| 68 | + |
| 69 | + /** |
| 70 | + * @var FilterBuilder|MockObject |
| 71 | + */ |
| 72 | + private $filterBuilder; |
| 73 | + |
| 74 | + /** |
| 75 | + * @var SearchCriteriaBuilder|MockObject |
| 76 | + */ |
| 77 | + private $criteriaBuilder; |
| 78 | + |
| 79 | + /** |
| 80 | + * @var AddressInterface|MockObject |
| 81 | + */ |
| 82 | + private $addressItem; |
| 83 | + |
| 84 | + /** |
| 85 | + * @var AddressRepositoryInterface|MockObject |
| 86 | + */ |
| 87 | + private $addressService; |
| 88 | + |
| 89 | + /** |
| 90 | + * @var Mapper|MockObject |
| 91 | + */ |
| 92 | + private $addressMapper; |
| 93 | + |
| 94 | + /** |
| 95 | + * @var Address |
| 96 | + */ |
| 97 | + private $address; |
| 98 | + |
| 99 | + /** |
| 100 | + * @var ObjectManager |
| 101 | + */ |
| 102 | + private $objectManager; |
| 103 | + |
| 104 | + /** |
| 105 | + * @inheritdoc |
| 106 | + */ |
| 107 | + protected function setUp() |
| 108 | + { |
| 109 | + $this->objectManager = new ObjectManager($this); |
| 110 | + |
| 111 | + $this->defaultCountryId = 1; |
| 112 | + $this->customerId = 10; |
| 113 | + $this->addressId = 100; |
| 114 | + |
| 115 | + $this->quoteSession = $this->getMockBuilder(QuoteSession::class) |
| 116 | + ->disableOriginalConstructor() |
| 117 | + ->setMethods(['getStore', 'getCustomerId']) |
| 118 | + ->getMock(); |
| 119 | + $this->store = $this->getMockBuilder(Store::class) |
| 120 | + ->disableOriginalConstructor() |
| 121 | + ->getMock(); |
| 122 | + $this->quoteSession->expects($this->any()) |
| 123 | + ->method('getStore') |
| 124 | + ->willReturn($this->store); |
| 125 | + $this->quoteSession->expects($this->any()) |
| 126 | + ->method('getCustomerId') |
| 127 | + ->willReturn($this->customerId); |
| 128 | + $this->directoryHelper = $this->getMockBuilder(DirectoryHelper::class) |
| 129 | + ->disableOriginalConstructor() |
| 130 | + ->setMethods(['getDefaultCountry']) |
| 131 | + ->getMock(); |
| 132 | + $this->directoryHelper->expects($this->any()) |
| 133 | + ->method('getDefaultCountry') |
| 134 | + ->willReturn($this->defaultCountryId); |
| 135 | + $this->formFactory = $this->getMockBuilder(FormFactory::class) |
| 136 | + ->disableOriginalConstructor() |
| 137 | + ->setMethods(['create']) |
| 138 | + ->getMock(); |
| 139 | + $this->filterBuilder = $this->getMockBuilder(FilterBuilder::class) |
| 140 | + ->disableOriginalConstructor() |
| 141 | + ->setMethods(['setField', 'setValue', 'setConditionType', 'create']) |
| 142 | + ->getMock(); |
| 143 | + $this->criteriaBuilder = $this->getMockBuilder(SearchCriteriaBuilder::class) |
| 144 | + ->disableOriginalConstructor() |
| 145 | + ->setMethods(['create', 'addFilters']) |
| 146 | + ->getMock(); |
| 147 | + $this->addressService = $this->getMockBuilder(AddressRepositoryInterface::class) |
| 148 | + ->setMethods(['getList']) |
| 149 | + ->getMockForAbstractClass(); |
| 150 | + $this->addressItem = $this->getMockBuilder(AddressInterface::class) |
| 151 | + ->setMethods(['getId']) |
| 152 | + ->getMockForAbstractClass(); |
| 153 | + $this->addressItem->expects($this->any()) |
| 154 | + ->method('getId') |
| 155 | + ->willReturn($this->addressId); |
| 156 | + $this->addressMapper = $this->getMockBuilder(Mapper::class) |
| 157 | + ->disableOriginalConstructor() |
| 158 | + ->setMethods(['toFlatArray']) |
| 159 | + ->getMock(); |
| 160 | + |
| 161 | + $this->address = $this->objectManager->getObject( |
| 162 | + Address::class, |
| 163 | + [ |
| 164 | + 'directoryHelper' => $this->directoryHelper, |
| 165 | + 'sessionQuote' => $this->quoteSession, |
| 166 | + 'customerFormFactory' => $this->formFactory, |
| 167 | + 'filterBuilder' => $this->filterBuilder, |
| 168 | + 'criteriaBuilder' => $this->criteriaBuilder, |
| 169 | + 'addressService' => $this->addressService, |
| 170 | + 'addressMapper' => $this->addressMapper |
| 171 | + ] |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + public function testGetAddressCollectionJson() |
| 176 | + { |
| 177 | + /** @var Form|MockObject $emptyForm */ |
| 178 | + $emptyForm = $this->getMockBuilder(Form::class) |
| 179 | + ->disableOriginalConstructor() |
| 180 | + ->setMethods(['outputData']) |
| 181 | + ->getMock(); |
| 182 | + $emptyForm->expects($this->once()) |
| 183 | + ->method('outputData') |
| 184 | + ->with(AttributeDataFactory::OUTPUT_FORMAT_JSON) |
| 185 | + ->willReturn('emptyFormData'); |
| 186 | + |
| 187 | + /** @var Filter|MockObject $filter */ |
| 188 | + $filter = $this->getMockBuilder(Filter::class) |
| 189 | + ->disableOriginalConstructor() |
| 190 | + ->getMock(); |
| 191 | + $this->filterBuilder->expects($this->once()) |
| 192 | + ->method('setField') |
| 193 | + ->with('parent_id') |
| 194 | + ->willReturnSelf(); |
| 195 | + $this->filterBuilder->expects($this->once()) |
| 196 | + ->method('setValue') |
| 197 | + ->with($this->customerId) |
| 198 | + ->willReturnSelf(); |
| 199 | + $this->filterBuilder->expects($this->once()) |
| 200 | + ->method('setConditionType') |
| 201 | + ->with('eq') |
| 202 | + ->willReturnSelf(); |
| 203 | + $this->filterBuilder->expects($this->once()) |
| 204 | + ->method('create') |
| 205 | + ->willReturn($filter); |
| 206 | + |
| 207 | + /** @var SearchCriteria|MockObject $searchCriteria */ |
| 208 | + $searchCriteria = $this->getMockBuilder(SearchCriteria::class) |
| 209 | + ->disableOriginalConstructor() |
| 210 | + ->getMock(); |
| 211 | + $this->criteriaBuilder->expects($this->once()) |
| 212 | + ->method('create') |
| 213 | + ->willReturn($searchCriteria); |
| 214 | + $this->criteriaBuilder->expects($this->once()) |
| 215 | + ->method('addFilters') |
| 216 | + ->with([$filter]); |
| 217 | + |
| 218 | + /** @var AddressSearchResultsInterface|MockObject $result */ |
| 219 | + $result = $this->getMockBuilder(AddressSearchResultsInterface::class) |
| 220 | + ->setMethods(['getList']) |
| 221 | + ->getMockForAbstractClass(); |
| 222 | + $result->expects($this->once()) |
| 223 | + ->method('getItems') |
| 224 | + ->willReturn([$this->addressItem]); |
| 225 | + $this->addressService->expects($this->once()) |
| 226 | + ->method('getList') |
| 227 | + ->with($searchCriteria) |
| 228 | + ->willReturn($result); |
| 229 | + |
| 230 | + /** @var Form|MockObject $emptyForm */ |
| 231 | + $addressForm = $this->getMockBuilder(Form::class) |
| 232 | + ->disableOriginalConstructor() |
| 233 | + ->setMethods(['outputData']) |
| 234 | + ->getMock(); |
| 235 | + $addressForm->expects($this->once()) |
| 236 | + ->method('outputData') |
| 237 | + ->with(AttributeDataFactory::OUTPUT_FORMAT_JSON) |
| 238 | + ->willReturn('addressFormData'); |
| 239 | + $this->addressMapper->expects($this->once()) |
| 240 | + ->method('toFlatArray') |
| 241 | + ->with($this->addressItem) |
| 242 | + ->willReturn([]); |
| 243 | + |
| 244 | + $this->directoryHelper->expects($this->once()) |
| 245 | + ->method('getDefaultCountry') |
| 246 | + ->with($this->store) |
| 247 | + ->willReturn($this->defaultCountryId); |
| 248 | + $this->formFactory->expects($this->at(0)) |
| 249 | + ->method('create') |
| 250 | + ->with( |
| 251 | + 'customer_address', |
| 252 | + 'adminhtml_customer_address', |
| 253 | + [AddressInterface::COUNTRY_ID => $this->defaultCountryId] |
| 254 | + ) |
| 255 | + ->willReturn($emptyForm); |
| 256 | + $this->formFactory->expects($this->at(1)) |
| 257 | + ->method('create') |
| 258 | + ->with('customer_address', 'adminhtml_customer_address', [], false, false) |
| 259 | + ->willReturn($addressForm); |
| 260 | + |
| 261 | + $this->address->getAddressCollectionJson(); |
| 262 | + } |
| 263 | +} |
0 commit comments