Skip to content

Commit 555d643

Browse files
authored
ENGCOM-6078: graphQl-892: Improved strict typing in CartAddressInterface #897
2 parents 0dcb19e + 742c9aa commit 555d643

File tree

6 files changed

+89
-94
lines changed

6 files changed

+89
-94
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\GraphQl\Schema\Type\TypeRegistry;
11+
12+
/**
13+
* Validates address against required fields from schema
14+
*/
15+
class ValidateAddressFromSchema
16+
{
17+
/**
18+
* @var TypeRegistry
19+
*/
20+
private $typeRegistry;
21+
22+
/**
23+
* @param TypeRegistry $typeRegistry
24+
*/
25+
public function __construct(
26+
TypeRegistry $typeRegistry
27+
) {
28+
$this->typeRegistry = $typeRegistry;
29+
}
30+
31+
/**
32+
* Validate data from address against mandatory fields from graphql schema for address
33+
*
34+
* @param array $address
35+
* @return bool
36+
*/
37+
public function execute(array $address = []) : bool
38+
{
39+
/** @var \Magento\Framework\GraphQL\Schema\Type\Input\InputObjectType $cartAddressInput */
40+
$cartAddressInput = $this->typeRegistry->get('CartAddressInput');
41+
$fields = $cartAddressInput->getFields();
42+
43+
foreach ($fields as $field) {
44+
if ($field->getType() instanceof \Magento\Framework\GraphQL\Schema\Type\NonNull) {
45+
// an array key has to exist but it's value should not be null
46+
if (array_key_exists($field->name, $address)
47+
&& !is_array($address[$field->name])
48+
&& !isset($address[$field->name])) {
49+
return false;
50+
}
51+
}
52+
}
53+
return true;
54+
}
55+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Quote\Api\Data\CartInterface;
1515
use Magento\QuoteGraphQl\Model\Cart\ExtractQuoteAddressData;
16+
use Magento\QuoteGraphQl\Model\Cart\ValidateAddressFromSchema;
1617

1718
/**
1819
* @inheritdoc
@@ -24,12 +25,21 @@ class BillingAddress implements ResolverInterface
2425
*/
2526
private $extractQuoteAddressData;
2627

28+
/**
29+
* @var ValidateAddressFromSchema
30+
*/
31+
private $validateAddressFromSchema;
32+
2733
/**
2834
* @param ExtractQuoteAddressData $extractQuoteAddressData
35+
* @param ValidateAddressFromSchema $validateAddressFromSchema
2936
*/
30-
public function __construct(ExtractQuoteAddressData $extractQuoteAddressData)
31-
{
37+
public function __construct(
38+
ExtractQuoteAddressData $extractQuoteAddressData,
39+
ValidateAddressFromSchema $validateAddressFromSchema
40+
) {
3241
$this->extractQuoteAddressData = $extractQuoteAddressData;
42+
$this->validateAddressFromSchema = $validateAddressFromSchema;
3343
}
3444

3545
/**
@@ -44,11 +54,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4454
$cart = $value['model'];
4555

4656
$billingAddress = $cart->getBillingAddress();
47-
if (null === $billingAddress) {
57+
$addressData = $this->extractQuoteAddressData->execute($billingAddress);
58+
if (!$this->validateAddressFromSchema->execute($addressData)) {
4859
return null;
4960
}
50-
51-
$addressData = $this->extractQuoteAddressData->execute($billingAddress);
5261
return $addressData;
5362
}
5463
}

app/code/Magento/QuoteGraphQl/Model/Resolver/ShippingAddresses.php

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\Quote\Model\Quote;
1515
use Magento\QuoteGraphQl\Model\Cart\ExtractQuoteAddressData;
16-
use Magento\Framework\GraphQl\Schema\Type\TypeRegistry;
17-
use Magento\Framework\App\ObjectManager;
16+
use Magento\QuoteGraphQl\Model\Cart\ValidateAddressFromSchema;
1817

1918
/**
2019
* @inheritdoc
@@ -27,20 +26,20 @@ class ShippingAddresses implements ResolverInterface
2726
private $extractQuoteAddressData;
2827

2928
/**
30-
* @var TypeRegistry
29+
* @var ValidateAddressFromSchema
3130
*/
32-
private $typeRegistry;
31+
private $validateAddressFromSchema;
3332

3433
/**
3534
* @param ExtractQuoteAddressData $extractQuoteAddressData
36-
* @param TypeRegistry|null $typeRegistry
35+
* @param ValidateAddressFromSchema $validateAddressFromSchema
3736
*/
3837
public function __construct(
3938
ExtractQuoteAddressData $extractQuoteAddressData,
40-
TypeRegistry $typeRegistry = null
39+
ValidateAddressFromSchema $validateAddressFromSchema
4140
) {
4241
$this->extractQuoteAddressData = $extractQuoteAddressData;
43-
$this->typeRegistry = $typeRegistry ?: ObjectManager::getInstance()->get(TypeRegistry::class);
42+
$this->validateAddressFromSchema = $validateAddressFromSchema;
4443
}
4544

4645
/**
@@ -61,36 +60,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6160
foreach ($shippingAddresses as $shippingAddress) {
6261
$address = $this->extractQuoteAddressData->execute($shippingAddress);
6362

64-
if ($this->validateAddressFromSchema($address)) {
63+
if ($this->validateAddressFromSchema->execute($address)) {
6564
$addressesData[] = $address;
6665
}
6766
}
6867
}
6968
return $addressesData;
7069
}
71-
72-
/**
73-
* Validate data from address against mandatory fields from graphql schema for address
74-
*
75-
* @param array $address
76-
* @return bool
77-
*/
78-
private function validateAddressFromSchema(array $address) : bool
79-
{
80-
/** @var \Magento\Framework\GraphQL\Schema\Type\Input\InputObjectType $cartAddressInput */
81-
$cartAddressInput = $this->typeRegistry->get('CartAddressInput');
82-
$fields = $cartAddressInput->getFields();
83-
84-
foreach ($fields as $field) {
85-
if ($field->getType() instanceof \Magento\Framework\GraphQL\Schema\Type\NonNull) {
86-
// an array key has to exist but it's value should not be null
87-
if (array_key_exists($field->name, $address)
88-
&& !is_array($address[$field->name])
89-
&& !isset($address[$field->name])) {
90-
return false;
91-
}
92-
}
93-
}
94-
return true;
95-
}
9670
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ type Cart {
196196
applied_coupons: [AppliedCoupon] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupons") @doc(description:"An array of `AppliedCoupon` objects. Each object contains the `code` text attribute, which specifies the coupon code")
197197
email: String @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartEmail")
198198
shipping_addresses: [ShippingCartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
199-
billing_address: BillingCartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
199+
billing_address: BillingCartAddress @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
200200
available_payment_methods: [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
201201
selected_payment_method: SelectedPaymentMethod @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SelectedPaymentMethod")
202202
prices: CartPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartPrices")
@@ -205,15 +205,15 @@ type Cart {
205205
}
206206

207207
interface CartAddressInterface @typeResolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddressTypeResolver") {
208-
firstname: String
209-
lastname: String
208+
firstname: String!
209+
lastname: String!
210210
company: String
211-
street: [String]
212-
city: String
211+
street: [String!]!
212+
city: String!
213213
region: CartAddressRegion
214214
postcode: String
215-
country: CartAddressCountry
216-
telephone: String
215+
country: CartAddressCountry!
216+
telephone: String!
217217
}
218218

219219
type ShippingCartAddress implements CartAddressInterface {
@@ -234,13 +234,13 @@ type CartItemQuantity {
234234
}
235235

236236
type CartAddressRegion {
237-
code: String
238-
label: String
237+
code: String!
238+
label: String!
239239
}
240240

241241
type CartAddressCountry {
242-
code: String
243-
label: String
242+
code: String!
243+
label: String!
244244
}
245245

246246
type SelectedShippingMethod {

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/GetSpecifiedBillingAddressTest.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,7 @@ public function testGetSpecifiedBillingAddressIfBillingAddressIsNotSet()
9191
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
9292
self::assertArrayHasKey('cart', $response);
9393
self::assertArrayHasKey('billing_address', $response['cart']);
94-
95-
$expectedBillingAddressData = [
96-
'firstname' => null,
97-
'lastname' => null,
98-
'company' => null,
99-
'street' => [
100-
''
101-
],
102-
'city' => null,
103-
'region' => [
104-
'code' => null,
105-
'label' => null,
106-
],
107-
'postcode' => null,
108-
'country' => [
109-
'code' => null,
110-
'label' => null,
111-
],
112-
'telephone' => null,
113-
'__typename' => 'BillingCartAddress',
114-
'customer_notes' => null,
115-
];
116-
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
94+
self::assertNull($response['cart']['billing_address']);
11795
}
11896

11997
/**

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/GetSpecifiedBillingAddressTest.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,7 @@ public function testGetSpecifiedBillingAddressIfBillingAddressIsNotSet()
8181
$response = $this->graphQlQuery($query);
8282
self::assertArrayHasKey('cart', $response);
8383
self::assertArrayHasKey('billing_address', $response['cart']);
84-
85-
$expectedBillingAddressData = [
86-
'firstname' => null,
87-
'lastname' => null,
88-
'company' => null,
89-
'street' => [
90-
''
91-
],
92-
'city' => null,
93-
'region' => [
94-
'code' => null,
95-
'label' => null,
96-
],
97-
'postcode' => null,
98-
'country' => [
99-
'code' => null,
100-
'label' => null,
101-
],
102-
'telephone' => null,
103-
'__typename' => 'BillingCartAddress',
104-
];
105-
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
84+
self::assertNull($response['cart']['billing_address']);
10685
}
10786

10887
/**

0 commit comments

Comments
 (0)