Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
graphQl-914: [Customer] Improve consistency of country field in custo…
Browse files Browse the repository at this point in the history
…mer address
  • Loading branch information
kisroman committed Oct 3, 2019
1 parent 42ecae6 commit fd7522f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __construct(
*/
public function execute(int $customerId, array $data): AddressInterface
{
// It is needed because AddressInterface has country_id field.
if (isset($data['country_code'])) {
$data['country_id'] = $data['country_code'];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public function execute(AddressInterface $address): array

$addressData['customer_id'] = null;

if (isset($addressData['country_id'])) {
$addressData['country_code'] = $addressData['country_id'];
}

return $addressData;
}
}
5 changes: 3 additions & 2 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ input CustomerAddressInput {
city: String @doc(description: "The city or town")
region: CustomerAddressRegionInput @doc(description: "An object containing the region name, region code, and region ID")
postcode: String @doc(description: "The customer's ZIP or postal code")
country_id: CountryCodeEnum @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.")
country_id: CountryCodeEnum @doc(description: "Deprecated, use country_code instead.")
country_code: CountryCodeEnum @doc(description: "The customer's country")
default_shipping: Boolean @doc(description: "Indicates whether the address is the default shipping address")
default_billing: Boolean @doc(description: "Indicates whether the address is the default billing address")
Expand Down Expand Up @@ -103,7 +103,8 @@ type CustomerAddress @doc(description: "CustomerAddress contains detailed inform
customer_id: Int @doc(description: "The customer ID") @deprecated(reason: "customer_id is not needed as part of CustomerAddress, address ID (id) is unique identifier for the addresses.")
region: CustomerAddressRegion @doc(description: "An object containing the region name, region code, and region ID")
region_id: Int @deprecated(reason: "Region ID is excessive on storefront and region code should suffice for all scenarios")
country_id: String @doc(description: "The customer's country")
country_id: String @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.")
country_code: CountryCodeEnum @doc(description: "The customer's country")
street: [String] @doc(description: "An array of strings that define the street number and name")
company: String @doc(description: "The customer's company")
telephone: String @doc(description: "The telephone number")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public function testCreateCustomerAddress()
*/
public function testCreateCustomerAddressWithCountryCode()
{
$customerId = 1;
$newAddress = [
'region' => [
'region' => 'Arizona',
Expand Down Expand Up @@ -195,7 +194,7 @@ public function testCreateCustomerAddressWithCountryCode()
region_id
region_code
}
country_id
country_code
street
company
telephone
Expand All @@ -220,16 +219,14 @@ public function testCreateCustomerAddressWithCountryCode()
$response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
$this->assertArrayHasKey('createCustomerAddress', $response);
$this->assertArrayHasKey('customer_id', $response['createCustomerAddress']);
$this->assertEquals($customerId, $response['createCustomerAddress']['customer_id']);
$this->assertEquals(null, $response['createCustomerAddress']['customer_id']);
$this->assertArrayHasKey('id', $response['createCustomerAddress']);

$address = $this->addressRepository->getById($response['createCustomerAddress']['id']);
$this->assertEquals($address->getId(), $response['createCustomerAddress']['id']);

$newAddress['country_id'] = $newAddress['country_code'];
unset($newAddress['country_code']);
$this->assertCustomerAddressesFields($address, $response['createCustomerAddress']);
$this->assertCustomerAddressesFields($address, $newAddress);
$this->assertCustomerAddressesFields($address, $response['createCustomerAddress'], 'country_code');
$this->assertCustomerAddressesFields($address, $newAddress, 'country_code');
}

/**
Expand Down Expand Up @@ -412,12 +409,16 @@ public function invalidInputDataProvider()
*
* @param AddressInterface $address
* @param array $actualResponse
* @param string $countryFieldName
*/
private function assertCustomerAddressesFields(AddressInterface $address, array $actualResponse): void
{
private function assertCustomerAddressesFields(
AddressInterface $address,
array $actualResponse,
string $countryFieldName = 'country_id'
): void {
/** @var $addresses */
$assertionMap = [
['response_field' => 'country_id', 'expected_value' => $address->getCountryId()],
['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()],
['response_field' => 'street', 'expected_value' => $address->getStreet()],
['response_field' => 'company', 'expected_value' => $address->getCompany()],
['response_field' => 'telephone', 'expected_value' => $address->getTelephone()],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,22 @@ public function testUpdateCustomerAddressWithCountryCode()
{
$userName = 'customer@example.com';
$password = 'password';
$customerId = 1;
$addressId = 1;

$mutation = $this->getMutationWithCountryCode($addressId);

$response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
$this->assertArrayHasKey('updateCustomerAddress', $response);
$this->assertArrayHasKey('customer_id', $response['updateCustomerAddress']);
$this->assertEquals($customerId, $response['updateCustomerAddress']['customer_id']);
$this->assertEquals(null, $response['updateCustomerAddress']['customer_id']);
$this->assertArrayHasKey('id', $response['updateCustomerAddress']);

$address = $this->addressRepository->getById($addressId);
$this->assertEquals($address->getId(), $response['updateCustomerAddress']['id']);
$this->assertCustomerAddressesFields($address, $response['updateCustomerAddress']);
$this->assertCustomerAddressesFields($address, $response['updateCustomerAddress'], 'country_code');

$updateAddress = $this->getAddressDataCanadaCountry();
$this->assertCustomerAddressesFields($address, $updateAddress);
$this->assertCustomerAddressesFields($address, $updateAddress, 'country_code');
}

/**
Expand Down Expand Up @@ -159,12 +158,16 @@ public function testUpdateCustomerAddressWithMissingAttribute()
*
* @param AddressInterface $address
* @param array $actualResponse
* @param string $countryFieldName
*/
private function assertCustomerAddressesFields(AddressInterface $address, $actualResponse): void
{
private function assertCustomerAddressesFields(
AddressInterface $address,
$actualResponse,
string $countryFieldName = 'country_id'
): void {
/** @var $addresses */
$assertionMap = [
['response_field' => 'country_id', 'expected_value' => $address->getCountryId()],
['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()],
['response_field' => 'street', 'expected_value' => $address->getStreet()],
['response_field' => 'company', 'expected_value' => $address->getCompany()],
['response_field' => 'telephone', 'expected_value' => $address->getTelephone()],
Expand Down Expand Up @@ -443,7 +446,7 @@ private function getAddressDataCanadaCountry(): array
'region_id' => 66,
'region_code' => 'AB'
],
'country_id' => 'CA',
'country_code' => 'CA',
'street' => ['Line 1 Street', 'Line 2'],
'company' => 'Company Name',
'telephone' => '123456789',
Expand Down Expand Up @@ -531,8 +534,8 @@ private function getMutation(int $addressId): string
private function getMutationWithCountryCode(int $addressId): string
{
$updateAddress = $this->getAddressDataCanadaCountry();
$defaultShippingText = $updateAddress['default_shipping'] ? "true" : "false";
$defaultBillingText = $updateAddress['default_billing'] ? "true" : "false";
$defaultShippingText = $updateAddress['default_shipping'] ? 'true' : 'false';
$defaultBillingText = $updateAddress['default_billing'] ? 'true' : 'false';

$mutation
= <<<MUTATION
Expand All @@ -543,7 +546,7 @@ private function getMutationWithCountryCode(int $addressId): string
region_id: {$updateAddress['region']['region_id']}
region_code: "{$updateAddress['region']['region_code']}"
}
country_code: {$updateAddress['country_id']}
country_code: {$updateAddress['country_code']}
street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"]
company: "{$updateAddress['company']}"
telephone: "{$updateAddress['telephone']}"
Expand All @@ -566,7 +569,7 @@ private function getMutationWithCountryCode(int $addressId): string
region_id
region_code
}
country_id
country_code
street
company
telephone
Expand Down

0 comments on commit fd7522f

Please sign in to comment.