Skip to content

Commit

Permalink
ENGCOM-4853: GraphQl-621: BillingAddress and ShippingAddress should h…
Browse files Browse the repository at this point in the history
…ave different fields but share the common interface #637
  • Loading branch information
naydav authored Apr 29, 2019
2 parents 3ccf676 + 274bb5e commit 2426688
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,7 @@ public function execute(QuoteAddress $address): array
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
$addressData['model'] = $address;

if ($address->getAddressType() == AbstractAddress::TYPE_SHIPPING) {
$addressType = 'SHIPPING';
} elseif ($address->getAddressType() == AbstractAddress::TYPE_BILLING) {
$addressType = 'BILLING';
} else {
$addressType = null;
}

$addressData = array_merge($addressData, [
'address_type' => $addressType,
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Customer\Model\Address\AbstractAddress;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
use Magento\Quote\Model\Quote\Address;

/**
* @inheritdoc
*/
class CartAddressTypeResolver implements TypeResolverInterface
{
/**
* @inheritdoc
*/
public function resolveType(array $data) : string
{
if (!isset($data['model'])) {
throw new LocalizedException(__('Missing key "model" in cart address data'));
}
/** @var Address $address */
$address = $data['model'];

if ($address->getAddressType() == AbstractAddress::TYPE_SHIPPING) {
$addressType = 'ShippingCartAddress';
} elseif ($address->getAddressType() == AbstractAddress::TYPE_BILLING) {
$addressType = 'BillingCartAddress';
} else {
throw new LocalizedException( __('Unsupported cart address type'));
}
return $addressType;
}
}
20 changes: 10 additions & 10 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ type Cart {
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon")
email: String @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartEmail")
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
shipping_addresses: [ShippingCartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
billing_address: BillingCartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
available_payment_methods: [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod")
prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPrices")
}

type CartAddress {
interface CartAddressInterface @typeResolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressTypeResolver") {
firstname: String
lastname: String
company: String
Expand All @@ -200,14 +200,19 @@ type CartAddress {
postcode: String
country: CartAddressCountry
telephone: String
address_type: AdressTypeEnum
customer_notes: String
}

type ShippingCartAddress implements CartAddressInterface {
available_shipping_methods: [AvailableShippingMethod] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\AvailableShippingMethods")
selected_shipping_method: SelectedShippingMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddress\\SelectedShippingMethod")
items_weight: Float
customer_notes: String
cart_items: [CartItemQuantity]
}

type BillingCartAddress implements CartAddressInterface {
}

type CartItemQuantity {
cart_item_id: Int!
quantity: Float!
Expand Down Expand Up @@ -257,11 +262,6 @@ type SelectedPaymentMethod {
type SelectedPaymentMethodAdditionalData {
}

enum AdressTypeEnum {
SHIPPING
BILLING
}

type AppliedCoupon {
code: String!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private function setBillingAddress(string $cartId): void
) {
cart {
billing_address {
address_type
__typename
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testGeSpecifiedBillingAddress()
'label' => 'US',
],
'telephone' => '3468676',
'address_type' => 'BILLING',
'__typename' => 'BillingCartAddress',
'customer_notes' => null,
];
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testGeSpecifiedBillingAddressIfBillingAddressIsNotSet()
'label' => null,
],
'telephone' => null,
'address_type' => 'BILLING',
'__typename' => null,
'customer_notes' => null,
];
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
Expand Down Expand Up @@ -197,7 +197,7 @@ private function getQuery(string $maskedQuoteId): string
label
}
telephone
address_type
__typename
customer_notes
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testSetNewBillingAddress()
code
label
}
address_type
__typename
}
}
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
code
label
}
address_type
__typename
}
shipping_addresses {
firstname
Expand All @@ -173,7 +173,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
code
label
}
address_type
__typename
}
}
}
Expand All @@ -188,7 +188,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
self::assertArrayHasKey('shipping_addresses', $cartResponse);
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
$this->assertNewAddressFields($billingAddressResponse);
$this->assertNewAddressFields($shippingAddressResponse, 'SHIPPING');
$this->assertNewAddressFields($shippingAddressResponse, 'ShippingCartAddress');
}

/**
Expand Down Expand Up @@ -560,7 +560,7 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
* @param array $addressResponse
* @param string $addressType
*/
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BILLING'): void
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BillingCartAddress'): void
{
$assertionMap = [
['response_field' => 'firstname', 'expected_value' => 'test firstname'],
Expand All @@ -571,7 +571,7 @@ private function assertNewAddressFields(array $addressResponse, string $addressT
['response_field' => 'postcode', 'expected_value' => '887766'],
['response_field' => 'telephone', 'expected_value' => '88776655'],
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
['response_field' => 'address_type', 'expected_value' => $addressType]
['response_field' => '__typename', 'expected_value' => $addressType]
];

$this->assertResponseFields($addressResponse, $assertionMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function testSetNewShippingAddressOnCartWithSimpleProduct()
label
code
}
address_type
__typename
}
}
}
Expand Down Expand Up @@ -548,7 +548,7 @@ private function assertNewShippingAddressFields(array $shippingAddressResponse):
['response_field' => 'postcode', 'expected_value' => '887766'],
['response_field' => 'telephone', 'expected_value' => '88776655'],
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
['response_field' => 'address_type', 'expected_value' => 'SHIPPING']
['response_field' => '__typename', 'expected_value' => 'ShippingCartAddress']
];

$this->assertResponseFields($shippingAddressResponse, $assertionMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private function setBillingAddress(string $cartId): void
) {
cart {
billing_address {
address_type
__typename
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testGeSpecifiedBillingAddress()
'label' => 'US',
],
'telephone' => '3468676',
'address_type' => 'BILLING',
'__typename' => 'BillingCartAddress',
];
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ public function testGeSpecifiedBillingAddressIfBillingAddressIsNotSet()
'label' => null,
],
'telephone' => null,
'address_type' => 'BILLING',
'__typename' => 'BillingCartAddress',
];
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
}
Expand Down Expand Up @@ -161,7 +161,7 @@ private function getQuery(string $maskedQuoteId): string
label
}
telephone
address_type
__typename
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testSetNewBillingAddress()
code
label
}
address_type
__typename
}
}
}
Expand Down Expand Up @@ -129,7 +129,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
code
label
}
address_type
__typename
}
shipping_addresses {
firstname
Expand All @@ -143,7 +143,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
code
label
}
address_type
__typename
}
}
}
Expand All @@ -158,7 +158,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
self::assertArrayHasKey('shipping_addresses', $cartResponse);
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
$this->assertNewAddressFields($billingAddressResponse);
$this->assertNewAddressFields($shippingAddressResponse, 'SHIPPING');
$this->assertNewAddressFields($shippingAddressResponse, 'ShippingCartAddress');
}

/**
Expand Down Expand Up @@ -380,7 +380,7 @@ public function testSetNewBillingAddressRedundantStreetLine()
* @param array $addressResponse
* @param string $addressType
*/
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BILLING'): void
private function assertNewAddressFields(array $addressResponse, string $addressType = 'BillingCartAddress'): void
{
$assertionMap = [
['response_field' => 'firstname', 'expected_value' => 'test firstname'],
Expand All @@ -391,7 +391,7 @@ private function assertNewAddressFields(array $addressResponse, string $addressT
['response_field' => 'postcode', 'expected_value' => '887766'],
['response_field' => 'telephone', 'expected_value' => '88776655'],
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
['response_field' => 'address_type', 'expected_value' => $addressType]
['response_field' => '__typename', 'expected_value' => $addressType]
];

$this->assertResponseFields($addressResponse, $assertionMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testSetNewShippingAddressOnCartWithSimpleProduct()
code
label
}
address_type
__typename
}
}
}
Expand Down Expand Up @@ -383,7 +383,7 @@ private function assertNewShippingAddressFields(array $shippingAddressResponse):
['response_field' => 'postcode', 'expected_value' => '887766'],
['response_field' => 'telephone', 'expected_value' => '88776655'],
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
['response_field' => 'address_type', 'expected_value' => 'SHIPPING']
['response_field' => '__typename', 'expected_value' => 'ShippingCartAddress']
];

$this->assertResponseFields($shippingAddressResponse, $assertionMap);
Expand Down

0 comments on commit 2426688

Please sign in to comment.