Skip to content

Commit 8fbf850

Browse files
committed
AC-14987::company field validation fails for guest checkout
1 parent a22ccae commit 8fbf850

File tree

1 file changed

+74
-27
lines changed

1 file changed

+74
-27
lines changed

app/code/Magento/Quote/Test/Unit/Model/CustomerManagementTest.php

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Magento\Quote\Model\Quote\Address;
2323
use PHPUnit\Framework\MockObject\MockObject;
2424
use PHPUnit\Framework\TestCase;
25+
use Magento\Customer\Api\Data\RegionInterfaceFactory;
26+
use Magento\Customer\Api\Data\RegionInterface;
2527

2628
/**
2729
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -83,6 +85,11 @@ class CustomerManagementTest extends TestCase
8385
*/
8486
private $customerAddressFactoryMock;
8587

88+
/**
89+
* @var MockObject
90+
*/
91+
private $regionFactoryMock;
92+
8693
protected function setUp(): void
8794
{
8895
$this->customerRepositoryMock = $this->getMockForAbstractClass(
@@ -153,11 +160,16 @@ protected function setUp(): void
153160
true,
154161
['create']
155162
);
163+
$this->regionFactoryMock = $this->getMockBuilder(RegionInterfaceFactory::class)
164+
->disableOriginalConstructor()
165+
->onlyMethods(['create'])
166+
->getMock();
156167
$this->customerManagement = new CustomerManagement(
157168
$this->customerRepositoryMock,
158169
$this->customerAddressRepositoryMock,
159170
$this->accountManagementMock,
160171
$this->customerAddressFactoryMock,
172+
$this->regionFactoryMock,
161173
$this->validatorFactoryMock,
162174
$this->addressFactoryMock
163175
);
@@ -268,34 +280,69 @@ public function testValidateAddresses()
268280
public function testValidateAddressesNotSavedInAddressBook()
269281
{
270282
$this->expectException(ValidatorException::class);
283+
284+
$regionData = [
285+
'region' => 'California',
286+
'region_code' => 'CA',
287+
'region_id' => 12,
288+
];
289+
271290
$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
285-
->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([]);
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->expects($this->once())->method('setCustomAttributes')->with(['custom_attr' => 'value']);
336+
337+
// Validator to fail
338+
$validatorMock = $this->createMock(Validator::class);
339+
$this->validatorFactoryMock->method('createValidator')->willReturn($validatorMock);
340+
$validatorMock->expects($this->once())->method('isValid')->willReturn(false);
341+
$validatorMock->expects($this->once())->method('getMessages')->willReturn([]);
342+
343+
$addressModelMock = $this->createMock(\Magento\Customer\Model\Address::class);
344+
$this->addressFactoryMock->method('create')->willReturn($addressModelMock);
345+
299346
$this->customerManagement->validateAddresses($this->quoteMock);
300347
}
301348
}

0 commit comments

Comments
 (0)