Skip to content

Commit ac76e2e

Browse files
committed
magento/graphql-ce#602: [Test Coverage] Get Specified shipping address
1 parent 93a8162 commit ac76e2e

File tree

2 files changed

+389
-0
lines changed

2 files changed

+389
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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\GraphQl\Quote\Customer;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\Integration\Api\CustomerTokenServiceInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test for get specified shipping address
17+
*/
18+
class GetSpecifiedShippingAddressTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var CustomerTokenServiceInterface
22+
*/
23+
private $customerTokenService;
24+
25+
/**
26+
* @var GetMaskedQuoteIdByReservedOrderId
27+
*/
28+
private $getMaskedQuoteIdByReservedOrderId;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
protected function setUp()
34+
{
35+
$objectManager = Bootstrap::getObjectManager();
36+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
37+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
38+
}
39+
40+
/**
41+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
42+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
43+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
44+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
45+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
46+
*/
47+
public function testGetSpecifiedShippingAddress()
48+
{
49+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
50+
$query = $this->getQuery($maskedQuoteId);
51+
52+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
53+
self::assertArrayHasKey('cart', $response);
54+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
55+
56+
$expectedShippingAddressData = [
57+
'firstname' => 'John',
58+
'lastname' => 'Smith',
59+
'company' => 'CompanyName',
60+
'street' => [
61+
'Green str, 67'
62+
],
63+
'city' => 'CityM',
64+
'region' => [
65+
'code' => 'AL',
66+
'label' => 'Alabama',
67+
],
68+
'postcode' => '75477',
69+
'country' => [
70+
'code' => 'US',
71+
'label' => 'US',
72+
],
73+
'telephone' => '3468676',
74+
'__typename' => 'ShippingCartAddress',
75+
'customer_notes' => null,
76+
];
77+
self::assertEquals($expectedShippingAddressData, current($response['cart']['shipping_addresses']));
78+
}
79+
80+
/**
81+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
82+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
83+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
84+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
85+
*/
86+
public function testGetSpecifiedShippingAddressIfShippingAddressIsNotSet()
87+
{
88+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
89+
$query = $this->getQuery($maskedQuoteId);
90+
91+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
92+
self::assertArrayHasKey('cart', $response);
93+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
94+
95+
$expectedShippingAddressData = [
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' => 'ShippingCartAddress',
114+
'customer_notes' => null,
115+
];
116+
self::assertEquals($expectedShippingAddressData, current($response['cart']['shipping_addresses']));
117+
}
118+
119+
/**
120+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
121+
* @expectedException \Exception
122+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
123+
*/
124+
public function testGetSpecifiedShippingAddressOfNonExistentCart()
125+
{
126+
$maskedQuoteId = 'non_existent_masked_id';
127+
$query = $this->getQuery($maskedQuoteId);
128+
129+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
130+
}
131+
132+
/**
133+
* _security
134+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
135+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
136+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
137+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
138+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
139+
*/
140+
public function testGetSpecifiedShippingAddressFromAnotherGuestCart()
141+
{
142+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
143+
144+
$this->expectExceptionMessage(
145+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
146+
);
147+
$this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());
148+
}
149+
150+
/**
151+
* _security
152+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
153+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
154+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
155+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
156+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
157+
*/
158+
public function testGetSpecifiedShippingAddressFromAnotherCustomerCart()
159+
{
160+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
161+
162+
$this->expectExceptionMessage(
163+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
164+
);
165+
$this->graphQlQuery(
166+
$this->getQuery($maskedQuoteId),
167+
[],
168+
'',
169+
$this->getHeaderMap('[email protected]')
170+
);
171+
}
172+
173+
/**
174+
* @param string $maskedQuoteId
175+
* @return string
176+
*/
177+
private function getQuery(string $maskedQuoteId): string
178+
{
179+
return <<<QUERY
180+
{
181+
cart(cart_id: "$maskedQuoteId") {
182+
shipping_addresses {
183+
firstname
184+
lastname
185+
company
186+
street
187+
city
188+
region
189+
{
190+
code
191+
label
192+
}
193+
postcode
194+
country
195+
{
196+
code
197+
label
198+
}
199+
telephone
200+
__typename
201+
customer_notes
202+
}
203+
}
204+
}
205+
QUERY;
206+
}
207+
208+
/**
209+
* @param string $username
210+
* @param string $password
211+
* @return array
212+
*/
213+
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array
214+
{
215+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
216+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
217+
return $headerMap;
218+
}
219+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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\GraphQl\Quote\Guest;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
14+
/**
15+
* Test for get specified shipping address
16+
*/
17+
class GetSpecifiedShippingAddressTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var GetMaskedQuoteIdByReservedOrderId
21+
*/
22+
private $getMaskedQuoteIdByReservedOrderId;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
$objectManager = Bootstrap::getObjectManager();
30+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
31+
}
32+
33+
/**
34+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
35+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
36+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
37+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
38+
*/
39+
public function testGetSpecifiedShippingAddress()
40+
{
41+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
42+
$query = $this->getQuery($maskedQuoteId);
43+
44+
$response = $this->graphQlQuery($query);
45+
self::assertArrayHasKey('cart', $response);
46+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
47+
48+
$expectedShippingAddressData = [
49+
'firstname' => 'John',
50+
'lastname' => 'Smith',
51+
'company' => 'CompanyName',
52+
'street' => [
53+
'Green str, 67'
54+
],
55+
'city' => 'CityM',
56+
'region' => [
57+
'code' => 'AL',
58+
'label' => 'Alabama',
59+
],
60+
'postcode' => '75477',
61+
'country' => [
62+
'code' => 'US',
63+
'label' => 'US',
64+
],
65+
'telephone' => '3468676',
66+
'__typename' => 'ShippingCartAddress',
67+
];
68+
self::assertEquals($expectedShippingAddressData, current($response['cart']['shipping_addresses']));
69+
}
70+
71+
/**
72+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
73+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
74+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
75+
*/
76+
public function testGetSpecifiedShippingAddressIfShippingAddressIsNotSet()
77+
{
78+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
79+
$query = $this->getQuery($maskedQuoteId);
80+
81+
$response = $this->graphQlQuery($query);
82+
self::assertArrayHasKey('cart', $response);
83+
self::assertArrayHasKey('shipping_addresses', $response['cart']);
84+
85+
$expectedShippingAddressData = [
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' => 'ShippingCartAddress',
104+
];
105+
self::assertEquals($expectedShippingAddressData, current($response['cart']['shipping_addresses']));
106+
}
107+
108+
/**
109+
* @expectedException \Exception
110+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
111+
*/
112+
public function testGetShippingAddressOfNonExistentCart()
113+
{
114+
$maskedQuoteId = 'non_existent_masked_id';
115+
$query = $this->getQuery($maskedQuoteId);
116+
$this->graphQlQuery($query);
117+
}
118+
119+
/**
120+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
121+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
122+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
123+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
124+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
125+
*/
126+
public function testGetShippingAddressFromAnotherCustomerCart()
127+
{
128+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
129+
$query = $this->getQuery($maskedQuoteId);
130+
131+
$this->expectExceptionMessage(
132+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
133+
);
134+
$this->graphQlQuery($query);
135+
}
136+
137+
/**
138+
* @param string $maskedQuoteId
139+
* @return string
140+
*/
141+
private function getQuery(string $maskedQuoteId): string
142+
{
143+
return <<<QUERY
144+
{
145+
cart(cart_id: "$maskedQuoteId") {
146+
shipping_addresses {
147+
firstname
148+
lastname
149+
company
150+
street
151+
city
152+
region
153+
{
154+
code
155+
label
156+
}
157+
postcode
158+
country
159+
{
160+
code
161+
label
162+
}
163+
telephone
164+
__typename
165+
}
166+
}
167+
}
168+
QUERY;
169+
}
170+
}

0 commit comments

Comments
 (0)