|
22 | 22 | use Magento\Quote\Model\Quote\Address;
|
23 | 23 | use PHPUnit\Framework\MockObject\MockObject;
|
24 | 24 | use PHPUnit\Framework\TestCase;
|
| 25 | +use Magento\Customer\Api\Data\RegionInterfaceFactory; |
| 26 | +use Magento\Customer\Api\Data\RegionInterface; |
25 | 27 |
|
26 | 28 | /**
|
27 | 29 | * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
@@ -83,6 +85,11 @@ class CustomerManagementTest extends TestCase
|
83 | 85 | */
|
84 | 86 | private $customerAddressFactoryMock;
|
85 | 87 |
|
| 88 | + /** |
| 89 | + * @var MockObject |
| 90 | + */ |
| 91 | + private $regionFactoryMock; |
| 92 | + |
86 | 93 | protected function setUp(): void
|
87 | 94 | {
|
88 | 95 | $this->customerRepositoryMock = $this->getMockForAbstractClass(
|
@@ -153,11 +160,16 @@ protected function setUp(): void
|
153 | 160 | true,
|
154 | 161 | ['create']
|
155 | 162 | );
|
| 163 | + $this->regionFactoryMock = $this->getMockBuilder(RegionInterfaceFactory::class) |
| 164 | + ->disableOriginalConstructor() |
| 165 | + ->onlyMethods(['create']) |
| 166 | + ->getMock(); |
156 | 167 | $this->customerManagement = new CustomerManagement(
|
157 | 168 | $this->customerRepositoryMock,
|
158 | 169 | $this->customerAddressRepositoryMock,
|
159 | 170 | $this->accountManagementMock,
|
160 | 171 | $this->customerAddressFactoryMock,
|
| 172 | + $this->regionFactoryMock, |
161 | 173 | $this->validatorFactoryMock,
|
162 | 174 | $this->addressFactoryMock
|
163 | 175 | );
|
@@ -268,34 +280,72 @@ public function testValidateAddresses()
|
268 | 280 | public function testValidateAddressesNotSavedInAddressBook()
|
269 | 281 | {
|
270 | 282 | $this->expectException(ValidatorException::class);
|
| 283 | + |
| 284 | + $regionData = [ |
| 285 | + 'region' => 'California', |
| 286 | + 'region_code' => 'CA', |
| 287 | + 'region_id' => 12, |
| 288 | + ]; |
| 289 | + |
271 | 290 | $this->quoteMock->method('getCustomerIsGuest')->willReturn(true);
|
272 |
| - $this->quoteAddressMock->method('getStreet')->willReturn(['test']); |
273 |
| - $this->quoteAddressMock->method('getCustomAttributes')->willReturn(['test']); |
274 |
| - $this->customerAddressFactoryMock->method('create') |
275 |
| - ->willReturn($this->customerAddressMock); |
276 |
| - $addressMock = $this->getMockBuilder(Address::class) |
277 |
| - ->disableOriginalConstructor() |
278 |
| - ->getMock(); |
279 |
| - $this->addressFactoryMock->expects($this->exactly(1))->method('create')->willReturn($addressMock); |
280 |
| - $this->quoteMock |
281 |
| - ->expects($this->atMost(2)) |
282 |
| - ->method('getBillingAddress') |
283 |
| - ->willReturn($this->quoteAddressMock); |
284 |
| - $this->quoteMock |
| 291 | + $this->quoteMock->method('getBillingAddress')->willReturn($this->quoteAddressMock); |
| 292 | + $this->quoteMock->method('getShippingAddress')->willReturn($this->quoteAddressMock); |
| 293 | + $this->quoteAddressMock->method('getCustomerAddressId')->willReturn(null); |
| 294 | + |
| 295 | + // Set up billing address data |
| 296 | + $this->quoteAddressMock->method('getPrefix')->willReturn('Mr'); |
| 297 | + $this->quoteAddressMock->method('getFirstname')->willReturn('John'); |
| 298 | + $this->quoteAddressMock->method('getMiddlename')->willReturn('Q'); |
| 299 | + $this->quoteAddressMock->method('getLastname')->willReturn('Public'); |
| 300 | + $this->quoteAddressMock->method('getSuffix')->willReturn('Jr'); |
| 301 | + $this->quoteAddressMock->method('getCompany')->willReturn('Acme Inc.'); |
| 302 | + $this->quoteAddressMock->method('getStreet')->willReturn(['123 Main St']); |
| 303 | + $this->quoteAddressMock->method('getCountryId')->willReturn('US'); |
| 304 | + $this->quoteAddressMock->method('getCity')->willReturn('Los Angeles'); |
| 305 | + $this->quoteAddressMock->method('getPostcode')->willReturn('90001'); |
| 306 | + $this->quoteAddressMock->method('getTelephone')->willReturn('1234567890'); |
| 307 | + $this->quoteAddressMock->method('getFax')->willReturn('9876543210'); |
| 308 | + $this->quoteAddressMock->method('getVatId')->willReturn('US123456789'); |
| 309 | + $this->quoteAddressMock->method('getRegion')->willReturn($regionData); |
| 310 | + $this->quoteAddressMock->method('getCustomAttributes')->willReturn(['custom_attr' => 'value']); |
| 311 | + |
| 312 | + // Region setup |
| 313 | + $regionMock = $this->createMock(RegionInterface::class); |
| 314 | + $this->regionFactoryMock->method('create')->willReturn($regionMock); |
| 315 | + $regionMock->expects($this->once())->method('setRegion')->with('California')->willReturnSelf(); |
| 316 | + $regionMock->expects($this->once())->method('setRegionCode')->with('CA')->willReturnSelf(); |
| 317 | + $regionMock->expects($this->once())->method('setRegionId')->with(12)->willReturnSelf(); |
| 318 | + |
| 319 | + // Customer address object to be created |
| 320 | + $this->customerAddressFactoryMock->method('create')->willReturn($this->customerAddressMock); |
| 321 | + $this->customerAddressMock->expects($this->once())->method('setPrefix')->with('Mr'); |
| 322 | + $this->customerAddressMock->expects($this->once())->method('setFirstname')->with('John'); |
| 323 | + $this->customerAddressMock->expects($this->once())->method('setMiddlename')->with('Q'); |
| 324 | + $this->customerAddressMock->expects($this->once())->method('setLastname')->with('Public'); |
| 325 | + $this->customerAddressMock->expects($this->once())->method('setSuffix')->with('Jr'); |
| 326 | + $this->customerAddressMock->expects($this->once())->method('setCompany')->with('Acme Inc.'); |
| 327 | + $this->customerAddressMock->expects($this->once())->method('setStreet')->with(['123 Main St']); |
| 328 | + $this->customerAddressMock->expects($this->once())->method('setCountryId')->with('US'); |
| 329 | + $this->customerAddressMock->expects($this->once())->method('setCity')->with('Los Angeles'); |
| 330 | + $this->customerAddressMock->expects($this->once())->method('setPostcode')->with('90001'); |
| 331 | + $this->customerAddressMock->expects($this->once())->method('setTelephone')->with('1234567890'); |
| 332 | + $this->customerAddressMock->expects($this->once())->method('setFax')->with('9876543210'); |
| 333 | + $this->customerAddressMock->expects($this->once())->method('setVatId')->with('US123456789'); |
| 334 | + $this->customerAddressMock->expects($this->once())->method('setRegion')->with($regionMock); |
| 335 | + $this->customerAddressMock |
285 | 336 | ->expects($this->once())
|
286 |
| - ->method('getShippingAddress') |
287 |
| - ->willReturn($this->quoteAddressMock); |
288 |
| - $this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(null); |
289 |
| - $validatorMock = $this->getMockBuilder(Validator::class) |
290 |
| - ->disableOriginalConstructor() |
291 |
| - ->getMock(); |
292 |
| - $this->validatorFactoryMock |
293 |
| - ->expects($this->exactly(1)) |
294 |
| - ->method('createValidator') |
295 |
| - ->with('customer_address', 'save', null) |
296 |
| - ->willReturn($validatorMock); |
297 |
| - $validatorMock->expects($this->exactly(1))->method('isValid')->with($addressMock)->willReturn(false); |
298 |
| - $validatorMock->expects($this->exactly(1))->method('getMessages')->willReturn([]); |
| 337 | + ->method('setCustomAttributes') |
| 338 | + ->with(['custom_attr' => 'value']); |
| 339 | + |
| 340 | + // Validator to fail |
| 341 | + $validatorMock = $this->createMock(Validator::class); |
| 342 | + $this->validatorFactoryMock->method('createValidator')->willReturn($validatorMock); |
| 343 | + $validatorMock->expects($this->once())->method('isValid')->willReturn(false); |
| 344 | + $validatorMock->expects($this->once())->method('getMessages')->willReturn([]); |
| 345 | + |
| 346 | + $addressModelMock = $this->createMock(\Magento\Customer\Model\Address::class); |
| 347 | + $this->addressFactoryMock->method('create')->willReturn($addressModelMock); |
| 348 | + |
299 | 349 | $this->customerManagement->validateAddresses($this->quoteMock);
|
300 | 350 | }
|
301 | 351 | }
|
0 commit comments