Skip to content

Commit f8a929e

Browse files
committed
MC-31524: Create/update customer address via customer repository
1 parent a25c107 commit f8a929e

File tree

6 files changed

+247
-33
lines changed

6 files changed

+247
-33
lines changed

dev/tests/integration/testsuite/Magento/Customer/Model/Address/CreateAddressTest.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,50 @@ class CreateAddressTest extends TestCase
3838
AddressInterface::COUNTRY_ID => 'US',
3939
'custom_region_name' => 'Alabama',
4040
AddressInterface::CITY => 'CityM',
41-
AddressInterface::STREET => 'Green str, 67',
41+
AddressInterface::STREET => ['Green str, 67'],
4242
AddressInterface::LASTNAME => 'Smith',
4343
AddressInterface::FIRSTNAME => 'John',
4444
];
4545

4646
/**
4747
* @var ObjectManager
4848
*/
49-
private $objectManager;
49+
protected $objectManager;
5050

5151
/**
52-
* @var GetRegionIdByName
52+
* @var AddressInterfaceFactory
5353
*/
54-
private $getRegionIdByName;
54+
protected $addressFactory;
5555

5656
/**
57-
* @var AddressInterfaceFactory
57+
* @var CustomerRegistry
5858
*/
59-
private $addressFactory;
59+
protected $customerRegistry;
6060

6161
/**
62-
* @var AddressRegistry
62+
* @var AddressRepositoryInterface
6363
*/
64-
private $addressRegistry;
64+
protected $addressRepository;
6565

6666
/**
67-
* @var Address
67+
* @var GetRegionIdByName
6868
*/
69-
private $addressResource;
69+
protected $getRegionIdByName;
7070

7171
/**
72-
* @var CustomerRegistry
72+
* @var CustomerRepositoryInterface
7373
*/
74-
private $customerRegistry;
74+
protected $customerRepository;
7575

7676
/**
77-
* @var AddressRepositoryInterface
77+
* @var AddressRegistry
7878
*/
79-
private $addressRepository;
79+
private $addressRegistry;
8080

8181
/**
82-
* @var CustomerRepositoryInterface
82+
* @var Address
8383
*/
84-
private $customerRepository;
84+
private $addressResource;
8585

8686
/**
8787
* @var int[]
@@ -94,13 +94,13 @@ class CreateAddressTest extends TestCase
9494
protected function setUp()
9595
{
9696
$this->objectManager = Bootstrap::getObjectManager();
97-
$this->getRegionIdByName = $this->objectManager->get(GetRegionIdByName::class);
9897
$this->addressFactory = $this->objectManager->get(AddressInterfaceFactory::class);
99-
$this->addressRegistry = $this->objectManager->get(AddressRegistry::class);
100-
$this->addressResource = $this->objectManager->get(Address::class);
10198
$this->customerRegistry = $this->objectManager->get(CustomerRegistry::class);
10299
$this->addressRepository = $this->objectManager->get(AddressRepositoryInterface::class);
100+
$this->getRegionIdByName = $this->objectManager->get(GetRegionIdByName::class);
103101
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
102+
$this->addressRegistry = $this->objectManager->get(AddressRegistry::class);
103+
$this->addressResource = $this->objectManager->get(Address::class);
104104
parent::setUp();
105105
}
106106

@@ -310,10 +310,6 @@ public function createWrongAddressesDataProvider(): array
310310
array_replace(self::STATIC_CUSTOMER_ADDRESS_DATA, [AddressInterface::LASTNAME => '']),
311311
InputException::requiredField('lastname'),
312312
],
313-
'required_field_empty_street_as_string' => [
314-
array_replace(self::STATIC_CUSTOMER_ADDRESS_DATA, [AddressInterface::STREET => '']),
315-
InputException::requiredField('street'),
316-
],
317313
'required_field_empty_street_as_array' => [
318314
array_replace(self::STATIC_CUSTOMER_ADDRESS_DATA, [AddressInterface::STREET => []]),
319315
InputException::requiredField('street'),
@@ -339,7 +335,7 @@ public function createWrongAddressesDataProvider(): array
339335
* @param bool $isDefaultBilling
340336
* @return AddressInterface
341337
*/
342-
private function createAddress(
338+
protected function createAddress(
343339
int $customerId,
344340
array $addressData,
345341
bool $isDefaultShipping = false,

dev/tests/integration/testsuite/Magento/Customer/Model/Address/DeleteAddressTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ class DeleteAddressTest extends TestCase
3030
/**
3131
* @var CustomerRegistry
3232
*/
33-
private $customerRegistry;
33+
protected $customerRegistry;
3434

3535
/**
3636
* @var AddressRepositoryInterface
3737
*/
38-
private $addressRepository;
38+
protected $addressRepository;
3939

4040
/**
4141
* @var CustomerRepositoryInterface
4242
*/
43-
private $customerRepository;
43+
protected $customerRepository;
4444

4545
/**
4646
* @inheritdoc

dev/tests/integration/testsuite/Magento/Customer/Model/Address/UpdateAddressTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,32 +33,32 @@ class UpdateAddressTest extends TestCase
3333
/**
3434
* @var AddressRegistry
3535
*/
36-
private $addressRegistry;
36+
protected $addressRegistry;
3737

3838
/**
3939
* @var Address
4040
*/
41-
private $addressResource;
41+
protected $addressResource;
4242

4343
/**
4444
* @var CustomerRegistry
4545
*/
46-
private $customerRegistry;
46+
protected $customerRegistry;
4747

4848
/**
4949
* @var AddressRepositoryInterface
5050
*/
51-
private $addressRepository;
51+
protected $addressRepository;
5252

5353
/**
5454
* @var CustomerRepositoryInterface
5555
*/
56-
private $customerRepository;
56+
protected $customerRepository;
5757

5858
/**
5959
* @var int[]
6060
*/
61-
private $processedAddressesIds = [];
61+
protected $processedAddressesIds = [];
6262

6363
/**
6464
* @inheritdoc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Customer\Model\ResourceModel\CustomerRepository;
9+
10+
use Magento\Customer\Api\Data\AddressInterface;
11+
use Magento\Customer\Model\Address\CreateAddressTest as CreateAddressViaAddressRepositoryTest;
12+
use Magento\Framework\Api\DataObjectHelper;
13+
14+
/**
15+
* Test cases related to create customer address using customer repository.
16+
*
17+
* @magentoDbIsolation enabled
18+
*/
19+
class CreateAddressTest extends CreateAddressViaAddressRepositoryTest
20+
{
21+
/**
22+
* @var DataObjectHelper
23+
*/
24+
private $dataObjectHelper;
25+
26+
/**
27+
* @inheritdoc
28+
*/
29+
protected function setUp()
30+
{
31+
parent::setUp();
32+
$this->dataObjectHelper = $this->objectManager->get(DataObjectHelper::class);
33+
}
34+
35+
/**
36+
* Create customer address with provided address data.
37+
*
38+
* @param int $customerId
39+
* @param array $addressData
40+
* @param bool $isDefaultShipping
41+
* @param bool $isDefaultBilling
42+
* @return AddressInterface
43+
*/
44+
protected function createAddress(
45+
int $customerId,
46+
array $addressData,
47+
bool $isDefaultShipping = false,
48+
bool $isDefaultBilling = false
49+
): AddressInterface {
50+
if (isset($addressData['custom_region_name'])) {
51+
$addressData[AddressInterface::REGION_ID] = $this->getRegionIdByName->execute(
52+
$addressData['custom_region_name'],
53+
$addressData[AddressInterface::COUNTRY_ID]
54+
);
55+
unset($addressData['custom_region_name']);
56+
}
57+
$address = $this->addressFactory->create();
58+
$this->dataObjectHelper->populateWithArray($address, $addressData, AddressInterface::class);
59+
$address->setIsDefaultShipping($isDefaultShipping);
60+
$address->setIsDefaultBilling($isDefaultBilling);
61+
$customer = $this->customerRepository->getById($customerId);
62+
$customer->setAddresses([$address]);
63+
$this->customerRepository->save($customer);
64+
$addressId = (int)$address->getId();
65+
$this->customerRegistry->remove($customerId);
66+
67+
return $this->addressRepository->getById($addressId);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Customer\Model\ResourceModel\CustomerRepository;
9+
10+
use Magento\Customer\Model\Address\DeleteAddressTest as DeleteAddressViaAddressRepositoryTest;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
13+
/**
14+
* Test cases related to delete customer address using customer repository.
15+
*
16+
* @magentoDbIsolation enabled
17+
*/
18+
class DeleteAddressTest extends DeleteAddressViaAddressRepositoryTest
19+
{
20+
/**
21+
* Assert that address deleted successfully.
22+
*
23+
* @magentoDataFixture Magento/Customer/_files/customer.php
24+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
25+
*
26+
* @return void
27+
*/
28+
public function testDeleteDefaultAddress(): void
29+
{
30+
$customer = $this->customerRepository->get('customer@example.com');
31+
$this->assertEquals(1, $customer->getDefaultShipping());
32+
$this->assertEquals(1, $customer->getDefaultBilling());
33+
$customer->setAddresses([]);
34+
$this->customerRepository->save($customer);
35+
$this->customerRegistry->remove($customer->getId());
36+
$customer = $this->customerRepository->get('customer@example.com');
37+
$this->assertNull($customer->getDefaultShipping());
38+
$this->assertNull($customer->getDefaultBilling());
39+
$this->expectExceptionObject(new NoSuchEntityException(__('No such entity with addressId = 1')));
40+
$this->addressRepository->getById(1);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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\Customer\Model\ResourceModel\CustomerRepository;
9+
10+
use Magento\Customer\Model\Address\UpdateAddressTest as UpdateAddressViaAddressRepositoryTest;
11+
12+
/**
13+
* Test cases related to update customer address using customer repository.
14+
*
15+
* @magentoDbIsolation enabled
16+
*/
17+
class UpdateAddressTest extends UpdateAddressViaAddressRepositoryTest
18+
{
19+
/**
20+
* Assert that default addresses properly updated for customer.
21+
*
22+
* @magentoDataFixture Magento/Customer/_files/customer.php
23+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
24+
*
25+
* @dataProvider updateAddressIsDefaultDataProvider
26+
*
27+
* @param bool $isShippingDefault
28+
* @param bool $isBillingDefault
29+
* @param int|null $expectedShipping
30+
* @param int|null $expectedBilling
31+
* @return void
32+
*/
33+
public function testUpdateAddressIsDefault(
34+
bool $isShippingDefault,
35+
bool $isBillingDefault,
36+
?int $expectedShipping,
37+
?int $expectedBilling
38+
): void {
39+
$customer = $this->customerRepository->get('customer@example.com');
40+
$this->assertEquals(1, $customer->getDefaultShipping());
41+
$this->assertEquals(1, $customer->getDefaultBilling());
42+
$this->processedAddressesIds[] = 1;
43+
$address = $this->addressRepository->getById(1);
44+
$address->setIsDefaultShipping($isShippingDefault);
45+
$address->setIsDefaultBilling($isBillingDefault);
46+
$customer->setAddresses([$address]);
47+
$this->customerRepository->save($customer);
48+
$this->customerRegistry->remove(1);
49+
$customer = $this->customerRepository->get('customer@example.com');
50+
$this->assertEquals($customer->getDefaultShipping(), $expectedShipping);
51+
$this->assertEquals($customer->getDefaultBilling(), $expectedBilling);
52+
}
53+
54+
/**
55+
* Assert that address updated successfully.
56+
*
57+
* @magentoDataFixture Magento/Customer/_files/customer.php
58+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
59+
*
60+
* @dataProvider updateAddressesDataProvider
61+
*
62+
* @param array $updateData
63+
* @param array $expectedData
64+
* @return void
65+
*/
66+
public function testUpdateAddress(array $updateData, array $expectedData): void
67+
{
68+
$this->processedAddressesIds[] = 1;
69+
$address = $this->addressRepository->getById(1);
70+
foreach ($updateData as $setFieldName => $setValue) {
71+
$address->setData($setFieldName, $setValue);
72+
}
73+
$customer = $this->customerRepository->get('customer@example.com');
74+
$customer->setAddresses([$address]);
75+
$this->customerRepository->save($customer);
76+
$updatedAddressData = $this->addressRepository->getById((int)$address->getId())->__toArray();
77+
foreach ($expectedData as $getFieldName => $getValue) {
78+
$this->assertTrue(isset($updatedAddressData[$getFieldName]), "Field $getFieldName wasn't found.");
79+
$this->assertEquals($getValue, $updatedAddressData[$getFieldName]);
80+
}
81+
}
82+
83+
/**
84+
* Assert that error message has thrown during process address update.
85+
*
86+
* @magentoDataFixture Magento/Customer/_files/customer.php
87+
* @magentoDataFixture Magento/Customer/_files/customer_address.php
88+
*
89+
* @dataProvider updateWrongAddressesDataProvider
90+
*
91+
* @param array $updateData
92+
* @param \Exception $expectException
93+
* @return void
94+
*/
95+
public function testExceptionThrownDuringUpdateAddress(array $updateData, \Exception $expectException): void
96+
{
97+
$this->processedAddressesIds[] = 1;
98+
$address = $this->addressRepository->getById(1);
99+
$customer = $this->customerRepository->get('customer@example.com');
100+
foreach ($updateData as $setFieldName => $setValue) {
101+
$address->setData($setFieldName, $setValue);
102+
}
103+
$customer->setAddresses([$address]);
104+
$this->expectExceptionObject($expectException);
105+
$this->customerRepository->save($customer);
106+
}
107+
}

0 commit comments

Comments
 (0)