Skip to content

Commit bf83c7f

Browse files
committed
Added end-to-end test for adding product with customizable options to the shopping cart
1 parent 9be928c commit bf83c7f

File tree

1 file changed

+262
-0
lines changed

1 file changed

+262
-0
lines changed
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
13+
/**
14+
* Get customizable options of simple product via the corresponding GraphQl query and add the product
15+
* with customizable options to the shopping cart
16+
*/
17+
class AddSimpleProductToCartEndToEndTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var GetCustomOptionsWithUIDForQueryBySku
21+
*/
22+
private $getCustomOptionsWithIDV2ForQueryBySku;
23+
24+
/**
25+
* @var GetMaskedQuoteIdByReservedOrderId
26+
*/
27+
private $getMaskedQuoteIdByReservedOrderId;
28+
29+
/**
30+
* @var GetCartItemOptionsFromUID
31+
*/
32+
private $getCartItemOptionsFromUID;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected function setUp(): void
38+
{
39+
$objectManager = Bootstrap::getObjectManager();
40+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
41+
$this->getCartItemOptionsFromUID = $objectManager->get(GetCartItemOptionsFromUID::class);
42+
$this->getCustomOptionsWithIDV2ForQueryBySku = $objectManager->get(
43+
GetCustomOptionsWithUIDForQueryBySku::class
44+
);
45+
}
46+
47+
/**
48+
* Test adding a simple product to the shopping cart with all supported
49+
* customizable options assigned
50+
*
51+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
52+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
53+
*/
54+
public function testAddSimpleProductWithOptions()
55+
{
56+
$sku = 'simple';
57+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
58+
$qty = 1;
59+
60+
$productOptionsData = $this->getProductOptionsViaQuery($sku);
61+
62+
$itemOptionsQuery = preg_replace(
63+
'/"([^"]+)"\s*:\s*/',
64+
'$1:',
65+
json_encode($productOptionsData['received_options'])
66+
);
67+
68+
$query = $this->getAddToCartMutation($maskedQuoteId, $qty, $sku, trim($itemOptionsQuery, '{}'));
69+
$response = $this->graphQlMutation($query);
70+
71+
self::assertArrayHasKey('customizable_options', $response['addProductsToCart']['cart']['items'][0]);
72+
73+
foreach ($response['addProductsToCart']['cart']['items'][0]['customizable_options'] as $option) {
74+
self::assertEquals($productOptionsData['expected_options'][$option['id']], $option['values'][0]['value']);
75+
}
76+
}
77+
78+
/**
79+
* Get product data with customizable options using GraphQl query
80+
*
81+
* @param string $sku
82+
* @return array
83+
* @throws \Exception
84+
*/
85+
private function getProductOptionsViaQuery(string $sku): array
86+
{
87+
$query = $this->getProductQuery($sku);
88+
$response = $this->graphQlQuery($query);
89+
self::assertArrayHasKey('options', $response['products']['items'][0]);
90+
91+
$expectedItemOptions = [];
92+
$receivedItemOptions = [
93+
'entered_options' => [],
94+
'selected_options' => []
95+
];
96+
97+
foreach ($response['products']['items'][0]['options'] as $option) {
98+
if (isset($option['entered_option'])) {
99+
/* The date normalization is required since the attribute might value is formatted by the system */
100+
if ($option['title'] === 'date option') {
101+
$value = '2012-12-12 00:00:00';
102+
$expectedItemOptions[$option['option_id']] = date('M d, Y', strtotime($value));
103+
} else {
104+
$value = 'test';
105+
$expectedItemOptions[$option['option_id']] = $value;
106+
}
107+
$value = $option['title'] === 'date option' ? '2012-12-12 00:00:00' : 'test';
108+
109+
$receivedItemOptions['entered_options'][] = [
110+
'id' => $option['entered_option']['uid'],
111+
'value' => $value
112+
];
113+
114+
115+
} elseif (isset($option['selected_option'])) {
116+
$receivedItemOptions['selected_options'][] = reset($option['selected_option'])['uid'];
117+
$expectedItemOptions[$option['option_id']] = reset($option['selected_option'])['option_type_id'];
118+
}
119+
}
120+
121+
return [
122+
'expected_options' => $expectedItemOptions,
123+
'received_options' => $receivedItemOptions
124+
];
125+
}
126+
127+
/**
128+
* Returns GraphQL query for retrieving a product with customizable options
129+
*
130+
* @param string $sku
131+
* @return string
132+
*/
133+
private function getProductQuery(string $sku): string
134+
{
135+
$sku = $options = $values = null; // WTF?
136+
return <<<QUERY
137+
query {
138+
products(filter: { sku: { eq: "$sku" } }) {
139+
items {
140+
sku
141+
142+
... on CustomizableProductInterface {
143+
options {
144+
option_id
145+
title
146+
147+
... on CustomizableRadioOption {
148+
option_id
149+
selected_option: value {
150+
option_type_id
151+
uid
152+
}
153+
}
154+
155+
... on CustomizableDropDownOption {
156+
option_id
157+
selected_option: value {
158+
option_type_id
159+
uid
160+
}
161+
}
162+
163+
... on CustomizableMultipleOption {
164+
option_id
165+
selected_option: value {
166+
option_type_id
167+
uid
168+
}
169+
}
170+
171+
... on CustomizableCheckboxOption {
172+
option_id
173+
selected_option: value {
174+
option_type_id
175+
uid
176+
}
177+
}
178+
179+
... on CustomizableAreaOption {
180+
option_id
181+
entered_option: value {
182+
uid
183+
}
184+
}
185+
186+
... on CustomizableFieldOption {
187+
option_id
188+
entered_option: value {
189+
uid
190+
}
191+
}
192+
193+
... on CustomizableFileOption {
194+
option_id
195+
entered_option: value {
196+
uid
197+
}
198+
}
199+
200+
... on CustomizableDateOption {
201+
option_id
202+
entered_option: value {
203+
uid
204+
}
205+
}
206+
}
207+
}
208+
}
209+
}
210+
}
211+
QUERY;
212+
}
213+
214+
/**
215+
* Returns GraphQl mutation for adding item to cart
216+
*
217+
* @param string $maskedQuoteId
218+
* @param int $qty
219+
* @param string $sku
220+
* @param string $customizableOptions
221+
* @return string
222+
*/
223+
private function getAddToCartMutation(
224+
string $maskedQuoteId,
225+
int $qty,
226+
string $sku,
227+
string $customizableOptions
228+
): string {
229+
return <<<MUTATION
230+
mutation {
231+
addProductsToCart(
232+
cartId: "{$maskedQuoteId}",
233+
cartItems: [
234+
{
235+
sku: "{$sku}"
236+
quantity: {$qty}
237+
{$customizableOptions}
238+
}
239+
]
240+
) {
241+
cart {
242+
items {
243+
quantity
244+
... on SimpleCartItem {
245+
customizable_options {
246+
label
247+
id
248+
values {
249+
value
250+
}
251+
}
252+
}
253+
}
254+
},
255+
userInputErrors {
256+
message
257+
}
258+
}
259+
}
260+
MUTATION;
261+
}
262+
}

0 commit comments

Comments
 (0)