Skip to content

Commit f2f05df

Browse files
committed
GraphQL-418: [Shipping methods] Set Shipping Methods on Cart
1 parent e02dc94 commit f2f05df

File tree

3 files changed

+353
-356
lines changed

3 files changed

+353
-356
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\Quote\Model\QuoteFactory;
12+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
13+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\TestFramework\TestCase\GraphQlAbstract;
16+
17+
/**
18+
* Test for setting shipping methods on cart for customer
19+
*/
20+
class SetShippingMethodsOnCartTest extends GraphQlAbstract
21+
{
22+
/**
23+
* @var CustomerTokenServiceInterface
24+
*/
25+
private $customerTokenService;
26+
27+
/**
28+
* @var QuoteResource
29+
*/
30+
private $quoteResource;
31+
32+
/**
33+
* @var QuoteFactory
34+
*/
35+
private $quoteFactory;
36+
37+
/**
38+
* @var QuoteIdToMaskedQuoteIdInterface
39+
*/
40+
private $quoteIdToMaskedId;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$objectManager = Bootstrap::getObjectManager();
48+
$this->quoteResource = $objectManager->get(QuoteResource::class);
49+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
50+
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
51+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
52+
}
53+
54+
public function testShippingMethodWithVirtualProduct()
55+
{
56+
57+
}
58+
59+
public function testShippingMethodWithSimpleProduct()
60+
{
61+
62+
}
63+
64+
public function testShippingMethodWithSimpleProductWithoutAddress()
65+
{
66+
67+
}
68+
69+
public function testSetShippingMethodWithMissedRequiredParameters()
70+
{
71+
72+
}
73+
74+
public function testSetNonExistentShippingMethod()
75+
{
76+
}
77+
78+
public function testSetShippingMethodIfAddressIsNotBelongToCart()
79+
{
80+
}
81+
82+
public function testSetShippingMethodToNonExistentCart()
83+
{
84+
}
85+
86+
public function testSetShippingMethodToGuestCart()
87+
{
88+
89+
}
90+
91+
public function testSetShippingMethodToAnotherCustomerCart()
92+
{
93+
94+
}
95+
96+
public function testSetShippingMethodToNonExistentCartAddress()
97+
{
98+
}
99+
100+
public function testSetShippingMethodToGuestCartAddress()
101+
{
102+
103+
}
104+
105+
public function testSetShippingMethodToAnotherCustomerCartAddress()
106+
{
107+
108+
}
109+
110+
/**
111+
* @param string $maskedQuoteId
112+
* @param string $shippingMethodCode
113+
* @param string $shippingCarrierCode
114+
* @param string $shippingAddressId
115+
* @return string
116+
*/
117+
private function prepareMutationQuery(
118+
string $maskedQuoteId,
119+
string $shippingMethodCode,
120+
string $shippingCarrierCode,
121+
string $shippingAddressId
122+
) : string {
123+
return <<<QUERY
124+
mutation {
125+
setShippingMethodsOnCart(input:
126+
{
127+
cart_id: "$maskedQuoteId",
128+
shipping_addresses: [{
129+
cart_address_id: $shippingAddressId
130+
shipping_method: {
131+
method_code: "$shippingMethodCode"
132+
carrier_code: "$shippingCarrierCode"
133+
}
134+
}]
135+
}) {
136+
137+
cart {
138+
cart_id,
139+
shipping_addresses {
140+
selected_shipping_method {
141+
carrier_code
142+
method_code
143+
label
144+
amount
145+
}
146+
}
147+
}
148+
}
149+
}
150+
151+
QUERY;
152+
}
153+
154+
/**
155+
* @param string $reversedQuoteId
156+
* @return string
157+
*/
158+
private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string
159+
{
160+
$quote = $this->quoteFactory->create();
161+
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');
162+
163+
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
164+
}
165+
166+
/**
167+
* @param string $reversedQuoteId
168+
* @param int $customerId
169+
* @return string
170+
*/
171+
private function assignQuoteToCustomer(
172+
string $reversedQuoteId,
173+
int $customerId
174+
): string {
175+
$quote = $this->quoteFactory->create();
176+
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');
177+
$quote->setCustomerId($customerId);
178+
$this->quoteResource->save($quote);
179+
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
180+
}
181+
182+
/**
183+
* @param string $username
184+
* @param string $password
185+
* @return array
186+
*/
187+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
188+
{
189+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
190+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
191+
return $headerMap;
192+
}
193+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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\Quote\Model\QuoteFactory;
11+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
12+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
/**
17+
* Test for setting shipping methods on cart for guest
18+
*/
19+
class SetShippingMethodsOnCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var QuoteResource
23+
*/
24+
private $quoteResource;
25+
26+
/**
27+
* @var QuoteFactory
28+
*/
29+
private $quoteFactory;
30+
31+
/**
32+
* @var QuoteIdToMaskedQuoteIdInterface
33+
*/
34+
private $quoteIdToMaskedId;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
$objectManager = Bootstrap::getObjectManager();
42+
$this->quoteResource = $objectManager->get(QuoteResource::class);
43+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
44+
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
45+
}
46+
47+
public function testShippingMethodWithVirtualProduct()
48+
{
49+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
50+
}
51+
52+
public function testShippingMethodWithSimpleProduct()
53+
{
54+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
55+
}
56+
57+
public function testShippingMethodWithSimpleProductWithoutAddress()
58+
{
59+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
60+
}
61+
62+
public function testSetShippingMethodWithMissedRequiredParameters()
63+
{
64+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
65+
}
66+
67+
public function testSetNonExistentShippingMethod()
68+
{
69+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
70+
}
71+
72+
public function testSetShippingMethodIfAddressIsNotBelongToCart()
73+
{
74+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
75+
}
76+
77+
public function testSetShippingMethodToNonExistentCart()
78+
{
79+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
80+
}
81+
82+
public function testSetShippingMethodToGuestCart()
83+
{
84+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
85+
}
86+
87+
public function testSetShippingMethodToAnotherCustomerCart()
88+
{
89+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
90+
}
91+
92+
public function testSetShippingMethodToNonExistentCartAddress()
93+
{
94+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
95+
}
96+
97+
public function testSetShippingMethodToGuestCartAddress()
98+
{
99+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
100+
}
101+
102+
public function testSetShippingMethodToAnotherCustomerCartAddress()
103+
{
104+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/422');
105+
}
106+
107+
/**
108+
* @param string $maskedQuoteId
109+
* @param string $shippingMethodCode
110+
* @param string $shippingCarrierCode
111+
* @param string $shippingAddressId
112+
* @return string
113+
*/
114+
private function prepareMutationQuery(
115+
string $maskedQuoteId,
116+
string $shippingMethodCode,
117+
string $shippingCarrierCode,
118+
string $shippingAddressId
119+
) : string {
120+
return <<<QUERY
121+
mutation {
122+
setShippingMethodsOnCart(input:
123+
{
124+
cart_id: "$maskedQuoteId",
125+
shipping_addresses: [{
126+
cart_address_id: $shippingAddressId
127+
shipping_method: {
128+
method_code: "$shippingMethodCode"
129+
carrier_code: "$shippingCarrierCode"
130+
}
131+
}]
132+
}) {
133+
cart {
134+
shipping_addresses {
135+
selected_shipping_method {
136+
carrier_code
137+
method_code
138+
label
139+
amount
140+
}
141+
}
142+
}
143+
}
144+
}
145+
146+
QUERY;
147+
}
148+
149+
/**
150+
* @param string $reversedQuoteId
151+
* @return string
152+
*/
153+
private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string
154+
{
155+
$quote = $this->quoteFactory->create();
156+
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');
157+
158+
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
159+
}
160+
}

0 commit comments

Comments
 (0)