Skip to content

Commit

Permalink
MAGETWO-61725: [GITHUB] Improve address save flow to allow to use cus…
Browse files Browse the repository at this point in the history
…tom validators #7552
  • Loading branch information
Aleksandr Osadchyi committed Dec 6, 2016
1 parent df3c890 commit 042ae5f
Showing 1 changed file with 11 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public function testSaveWithException()
{
$customerId = 34;
$addressId = 53;
$errors[] = __('Please enter the state/province.');
$customerAddress = $this->getMockForAbstractClass(
\Magento\Customer\Api\Data\AddressInterface::class,
[],
Expand All @@ -246,7 +247,9 @@ public function testSaveWithException()
$this->address->expects($this->once())
->method('updateData')
->with($customerAddress);
$this->prepareMocksForInvalidAddressValidation();
$this->address->expects($this->once())
->method('validate')
->willReturn($errors);

$this->repository->save($customerAddress);
}
Expand All @@ -259,6 +262,7 @@ public function testSaveWithInvalidRegion()
{
$customerId = 34;
$addressId = 53;
$errors[] = __('region is a required field.');
$customerAddress = $this->getMockForAbstractClass(
\Magento\Customer\Api\Data\AddressInterface::class,
[],
Expand All @@ -282,60 +286,13 @@ public function testSaveWithInvalidRegion()
$this->address->expects($this->once())
->method('updateData')
->with($customerAddress);
$countryModel = $this->getMock(\Magento\Directory\Model\Country::class, [], [], '', false);
$regionCollection = $this->getMock(
\Magento\Directory\Model\ResourceModel\Region\Collection::class,
[],
[],
'',
false
);

$this->address->expects($this->once())
->method('getShouldIgnoreValidation')
->willReturn(false);
$this->address->expects($this->atLeastOnce())
->method('getCountryId')
->willReturn(1);
$this->address->expects($this->once())
->method('getFirstname')
->willReturn('firstname');
$this->address->expects($this->once())
->method('getLastname')
->willReturn('lastname');
$this->address->expects($this->once())
->method('getStreetLine')
->with(1)
->willReturn('street line');
$this->address->expects($this->once())
->method('getCity')
->willReturn('city');
$this->address->expects($this->once())
->method('getTelephone')
->willReturn('23423423423');
$this->address->expects($this->never())
->method('getRegionId')
->willReturn(null);

$this->directoryData->expects($this->once())
->method('getCountriesWithOptionalZip')
->willReturn([1]);
$this->address->expects($this->once())
->method('getCountryModel')
->willReturn($countryModel);
$countryModel->expects($this->once())
->method('getRegionCollection')
->willReturn($regionCollection);
$regionCollection->expects($this->once())
->method('count')
->willReturn(0);
$this->directoryData->expects($this->once())
->method('isRegionRequired')
->with(1)
->willReturn(true);
$this->address->expects($this->once())
->method('getRegion')
->willReturn('');
->method('validate')
->willReturn($errors);

$this->repository->save($customerAddress);
}
Expand All @@ -348,6 +305,7 @@ public function testSaveWithInvalidRegionId()
{
$customerId = 34;
$addressId = 53;
$errors[] = __('regionId is a required field.');
$customerAddress = $this->getMockForAbstractClass(
\Magento\Customer\Api\Data\AddressInterface::class,
[],
Expand Down Expand Up @@ -379,106 +337,14 @@ public function testSaveWithInvalidRegionId()
'',
false
);

$this->address->expects($this->once())
->method('getShouldIgnoreValidation')
->willReturn(false);
$this->address->expects($this->atLeastOnce())
->method('getCountryId')
->willReturn(1);
$this->address->expects($this->once())
->method('getFirstname')
->willReturn('firstname');
$this->address->expects($this->once())
->method('getLastname')
->willReturn('lastname');
$this->address->expects($this->once())
->method('getStreetLine')
->with(1)
->willReturn('street line');
$this->address->expects($this->once())
->method('getCity')
->willReturn('city');
$this->address->expects($this->once())
->method('getTelephone')
->willReturn('23423423423');
$this->address->expects($this->once())
->method('getRegionId')
->willReturn(2);

$this->directoryData->expects($this->once())
->method('getCountriesWithOptionalZip')
->willReturn([1]);
$this->address->expects($this->once())
->method('getCountryModel')
->willReturn($countryModel);
$countryModel->expects($this->once())
->method('getRegionCollection')
->willReturn($regionCollection);
$regionCollection->expects($this->atLeastOnce())
->method('count')
->willReturn(2);
$regionCollection->expects($this->once())
->method('getData')
->willReturn([5, 6, 7, 8, 9]);
$this->directoryData->expects($this->once())
->method('isRegionRequired')
->with(1)
->willReturn(true);
$this->address->expects($this->never())
->method('getRegion')
->willReturn('');

$this->repository->save($customerAddress);
}

protected function prepareMocksForInvalidAddressValidation()
{
$countryModel = $this->getMock(\Magento\Directory\Model\Country::class, [], [], '', false);
$regionCollection = $this->getMock(
\Magento\Directory\Model\ResourceModel\Region\Collection::class,
[],
[],
'',
false
);

$this->address->expects($this->once())
->method('getShouldIgnoreValidation')
->willReturn(false);
$this->address->expects($this->atLeastOnce())
->method('getCountryId');
$this->address->expects($this->once())
->method('getFirstname');
$this->address->expects($this->once())
->method('getLastname');
$this->address->expects($this->once())
->method('getStreetLine')
->with(1);
$this->address->expects($this->once())
->method('getCity');
$this->address->expects($this->once())
->method('getTelephone');
$this->address->expects($this->never())
->method('getRegionId')
->willReturn(null);
->method('validate')
->willReturn($errors);

$this->directoryData->expects($this->once())
->method('getCountriesWithOptionalZip')
->willReturn([]);
$this->address->expects($this->once())
->method('getCountryModel')
->willReturn($countryModel);
$countryModel->expects($this->once())
->method('getRegionCollection')
->willReturn($regionCollection);
$regionCollection->expects($this->once())
->method('count')
->willReturn(0);
$this->directoryData->expects($this->once())
->method('isRegionRequired')
->with(null)
->willReturn(true);
$this->repository->save($customerAddress);
}

public function testGetById()
Expand Down

0 comments on commit 042ae5f

Please sign in to comment.