Skip to content

Commit f39b0dd

Browse files
authored
ENGCOM-4663: [Checkout coverage] Place order concept #404
2 parents 19c4a50 + 6951d0d commit f39b0dd

28 files changed

+573
-85
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Quote\Api\CartManagementInterface;
18+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
19+
use Magento\Sales\Api\OrderRepositoryInterface;
20+
21+
/**
22+
* @inheritdoc
23+
*/
24+
class PlaceOrder implements ResolverInterface
25+
{
26+
/**
27+
* @var CartManagementInterface
28+
*/
29+
private $cartManagement;
30+
31+
/**
32+
* @var GetCartForUser
33+
*/
34+
private $getCartForUser;
35+
36+
/**
37+
* @var OrderRepositoryInterface
38+
*/
39+
private $orderRepository;
40+
41+
/**
42+
* @param GetCartForUser $getCartForUser
43+
* @param CartManagementInterface $cartManagement
44+
* @param OrderRepositoryInterface $orderRepository
45+
*/
46+
public function __construct(
47+
GetCartForUser $getCartForUser,
48+
CartManagementInterface $cartManagement,
49+
OrderRepositoryInterface $orderRepository
50+
) {
51+
$this->getCartForUser = $getCartForUser;
52+
$this->cartManagement = $cartManagement;
53+
$this->orderRepository = $orderRepository;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
60+
{
61+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
62+
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
63+
}
64+
$maskedCartId = $args['input']['cart_id'];
65+
66+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
67+
68+
try {
69+
$orderId = $this->cartManagement->placeOrder($cart->getId());
70+
$order = $this->orderRepository->get($orderId);
71+
72+
return [
73+
'order' => [
74+
'order_id' => $order->getIncrementId(),
75+
],
76+
];
77+
} catch (NoSuchEntityException $e) {
78+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
79+
} catch (LocalizedException $e) {
80+
throw new GraphQlInputException(__('Unable to place order: %message', ['message' => $e->getMessage()]), $e);
81+
}
82+
}
83+
}

app/code/Magento/QuoteGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"magento/module-catalog": "*",
1111
"magento/module-store": "*",
1212
"magento/module-customer": "*",
13-
"magento/module-customer-graph-ql": "*"
13+
"magento/module-customer-graph-ql": "*",
14+
"magento/module-sales": "*"
1415
},
1516
"suggest": {
1617
"magento/module-graph-ql": "*"

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Mutation {
1717
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart")
1818
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
1919
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
20+
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
2021
}
2122

2223
input AddSimpleProductsToCartInput {
@@ -114,6 +115,10 @@ input ShippingMethodInput {
114115
method_code: String!
115116
}
116117

118+
input PlaceOrderInput {
119+
cart_id: String!
120+
}
121+
117122
input SetPaymentMethodOnCartInput {
118123
cart_id: String!
119124
payment_method: PaymentMethodInput!
@@ -144,6 +149,10 @@ type ApplyCouponToCartOutput {
144149
cart: Cart!
145150
}
146151

152+
type PlaceOrderOutput {
153+
order: Order!
154+
}
155+
147156
type Cart {
148157
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
149158
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon")
@@ -285,3 +294,7 @@ type CartItemSelectedOptionValuePrice {
285294
units: String!
286295
type: PriceTypeEnum!
287296
}
297+
298+
type Order {
299+
order_id: String
300+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function setUp()
3939

4040
/**
4141
* @magentoApiDataFixture Magento/Customer/_files/customer.php
42-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
42+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
4343
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
4444
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
4545
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -60,7 +60,7 @@ public function testGetAvailablePaymentMethods()
6060
/**
6161
* _security
6262
* @magentoApiDataFixture Magento/Customer/_files/customer.php
63-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
63+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
6464
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
6565
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
6666
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -79,7 +79,7 @@ public function testGetAvailablePaymentMethodsFromGuestCart()
7979
/**
8080
* _security
8181
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
82-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
82+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
8383
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
8484
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
8585
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -97,7 +97,7 @@ public function testGetAvailablePaymentMethodsFromAnotherCustomerCart()
9797

9898
/**
9999
* @magentoApiDataFixture Magento/Customer/_files/customer.php
100-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
100+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
101101
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
102102
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
103103
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function setUp()
4141
* Test case: get available shipping methods from current customer quote
4242
*
4343
* @magentoApiDataFixture Magento/Customer/_files/customer.php
44-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
44+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
4545
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
4646
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
4747
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -77,7 +77,7 @@ public function testGetAvailableShippingMethods()
7777
/**
7878
* _security
7979
* @magentoApiDataFixture Magento/Customer/_files/customer.php
80-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
80+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
8181
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
8282
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
8383
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -98,7 +98,7 @@ public function testGetAvailableShippingMethodsFromGuestCart()
9898
*
9999
* _security
100100
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
101-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
101+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
102102
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
103103
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
104104
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -118,7 +118,7 @@ public function testGetAvailableShippingMethodsFromAnotherCustomerCart()
118118
* Test case: get available shipping methods when all shipping methods are disabled
119119
*
120120
* @magentoApiDataFixture Magento/Customer/_files/customer.php
121-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
121+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
122122
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
123123
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
124124
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setUp()
3636

3737
/**
3838
* @magentoApiDataFixture Magento/Customer/_files/customer.php
39-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
39+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
4040
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
4141
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
4242
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
@@ -55,7 +55,7 @@ public function testGetCart()
5555

5656
self::assertNotEmpty($response['cart']['items'][0]['id']);
5757
self::assertEquals(2, $response['cart']['items'][0]['qty']);
58-
self::assertEquals('simple', $response['cart']['items'][0]['product']['sku']);
58+
self::assertEquals('simple_product', $response['cart']['items'][0]['product']['sku']);
5959

6060
self::assertNotEmpty($response['cart']['items'][1]['id']);
6161
self::assertEquals(2, $response['cart']['items'][1]['qty']);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function setUp()
3939

4040
/**
4141
* @magentoApiDataFixture Magento/Customer/_files/customer.php
42-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
42+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
4343
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
4444
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
4545
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -69,7 +69,7 @@ public function testGetSelectedShippingMethod()
6969
/**
7070
* _security
7171
* @magentoApiDataFixture Magento/Customer/_files/customer.php
72-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
72+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
7373
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
7474
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
7575
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -89,7 +89,7 @@ public function testGetSelectedShippingMethodFromGuestCart()
8989
/**
9090
* _security
9191
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
92-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
92+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
9393
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
9494
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
9595
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
@@ -108,7 +108,7 @@ public function testGetSelectedShippingMethodFromAnotherCustomerCart()
108108

109109
/**
110110
* @magentoApiDataFixture Magento/Customer/_files/customer.php
111-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
111+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
112112
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
113113
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
114114
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected function setUp()
3939

4040
/**
4141
* @magentoApiDataFixture Magento/Customer/_files/customer.php
42-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
42+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
4343
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
4444
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
4545
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
@@ -79,7 +79,7 @@ public function testGeSpecifiedBillingAddress()
7979

8080
/**
8181
* @magentoApiDataFixture Magento/Customer/_files/customer.php
82-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
82+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
8383
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
8484
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
8585
*/
@@ -132,7 +132,7 @@ public function testGeSpecifiedBillingAddressOfNonExistentCart()
132132
/**
133133
* _security
134134
* @magentoApiDataFixture Magento/Customer/_files/customer.php
135-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
135+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
136136
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
137137
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
138138
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
@@ -150,7 +150,7 @@ public function testGeSpecifiedBillingAddressFromAnotherGuestCart()
150150
/**
151151
* _security
152152
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
153-
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
153+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
154154
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
155155
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
156156
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php

0 commit comments

Comments
 (0)