7
7
8
8
namespace Magento \Quote \Api ;
9
9
10
+ use Magento \Catalog \Model \Product ;
11
+ use Magento \Catalog \Model \Product \Option ;
12
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
13
+ use Magento \Framework \Webapi \Rest \Request ;
14
+ use Magento \Quote \Model \Quote ;
15
+ use Magento \TestFramework \Helper \Bootstrap ;
10
16
use Magento \TestFramework \TestCase \WebapiAbstract ;
11
17
12
18
/**
13
19
* Class for testing adding and deleting items flow.
14
20
*/
15
21
class GuestCartAddingItemsTest extends WebapiAbstract
16
22
{
17
- const SERVICE_VERSION = 'V1 ' ;
18
- const SERVICE_NAME = 'quoteGuestCartManagementV1 ' ;
19
- const RESOURCE_PATH = '/V1/guest-carts/ ' ;
23
+ private const SERVICE_VERSION = 'V1 ' ;
24
+ private const SERVICE_NAME = 'quoteGuestCartManagementV1 ' ;
25
+ private const RESOURCE_PATH = '/V1/guest-carts/ ' ;
20
26
21
27
/**
22
28
* @var \Magento\TestFramework\ObjectManager
23
29
*/
24
30
protected $ objectManager ;
31
+
32
+ /**
33
+ * @var ProductResource|mixed
34
+ */
35
+ private mixed $ productResource ;
25
36
26
37
protected function setUp (): void
27
38
{
28
- $ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
39
+ $ this ->objectManager = Bootstrap::getObjectManager ();
40
+ $ this ->productResource = $ this ->objectManager ->get (ProductResource::class);
41
+ }
42
+
43
+ /**
44
+ * Test add to product with custom option and test with updating custom options.
45
+ *
46
+ * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_custom_options.php
47
+ * @return void
48
+ */
49
+ public function testAddtoCartWithCustomOptionsForCreatingQuoteFromEmptyCart ()
50
+ {
51
+ $ this ->_markTestAsRestOnly ();
52
+
53
+ $ productId = $ this ->productResource ->getIdBySku ('simple_with_custom_options ' );
54
+ $ product = $ this ->objectManager ->create (Product::class)->load ($ productId );
55
+ $ customOptionCollection = $ this ->objectManager ->get (Option::class)
56
+ ->getProductOptionCollection ($ product );
57
+ $ customOptions = [];
58
+ foreach ($ customOptionCollection as $ option ) {
59
+ $ customOptions [] = [
60
+ 'option_id ' => $ option ->getId (),
61
+ 'option_value ' => $ option ->getType () !== 'field ' ? 1 : 'test '
62
+ ];
63
+ }
64
+
65
+ // Creating empty cart
66
+ $ serviceInfoForCreatingEmptyCart = [
67
+ 'rest ' => [
68
+ 'resourcePath ' => self ::RESOURCE_PATH ,
69
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
70
+ ]
71
+ ];
72
+ $ quoteId = $ this ->_webApiCall ($ serviceInfoForCreatingEmptyCart );
73
+
74
+ // Adding item to the cart
75
+ $ serviceInfoForAddingProduct = [
76
+ 'rest ' => [
77
+ 'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items ' ,
78
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
79
+ ]
80
+ ];
81
+
82
+ $ requestData = [
83
+ 'cartItem ' => [
84
+ 'quote_id ' => $ quoteId ,
85
+ 'sku ' => 'simple_with_custom_options ' ,
86
+ 'qty ' => 1 ,
87
+ 'product_option ' => [
88
+ 'extension_attributes ' => [
89
+ 'custom_options ' => $ customOptions
90
+ ]
91
+ ]
92
+ ]
93
+ ];
94
+ $ item = $ this ->_webApiCall ($ serviceInfoForAddingProduct , $ requestData );
95
+ $ this ->assertNotEmpty ($ item );
96
+ foreach ($ customOptionCollection as $ option ) {
97
+ $ customOptions [] = [
98
+ 'option_id ' => $ option ->getId (),
99
+ 'option_value ' => $ option ->getType () != 'field ' ? 2 : 'test2 '
100
+ ];
101
+ }
102
+ $ requestData = [
103
+ 'cartItem ' => [
104
+ 'quote_id ' => $ quoteId ,
105
+ 'sku ' => 'simple_with_custom_options ' ,
106
+ 'qty ' => 1 ,
107
+ 'product_option ' => [
108
+ 'extension_attributes ' => [
109
+ 'custom_options ' => $ customOptions
110
+ ]
111
+ ]
112
+ ]
113
+ ];
114
+
115
+ // Update the item for the cart
116
+ $ serviceInfoForUpdateProduct = [
117
+ 'rest ' => [
118
+ 'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items/ ' . $ item ['item_id ' ],
119
+ 'httpMethod ' => Request::HTTP_METHOD_PUT ,
120
+ ]
121
+ ];
122
+
123
+ $ item = $ this ->_webApiCall ($ serviceInfoForUpdateProduct , $ requestData );
124
+ $ this ->assertNotEmpty ($ item );
29
125
}
30
126
31
127
/**
@@ -40,7 +136,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
40
136
$ serviceInfoForCreatingEmptyCart = [
41
137
'rest ' => [
42
138
'resourcePath ' => self ::RESOURCE_PATH ,
43
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
139
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
44
140
],
45
141
'soap ' => [
46
142
'service ' => self ::SERVICE_NAME ,
@@ -54,7 +150,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
54
150
$ serviceInfoForAddingProduct = [
55
151
'rest ' => [
56
152
'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items ' ,
57
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
153
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
58
154
],
59
155
'soap ' => [
60
156
'service ' => GuestCartItemRepositoryTest::SERVICE_NAME ,
@@ -76,7 +172,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
76
172
$ serviceInfoForDeleteProduct = [
77
173
'rest ' => [
78
174
'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items/ ' . $ item ['item_id ' ],
79
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_DELETE ,
175
+ 'httpMethod ' => Request::HTTP_METHOD_DELETE ,
80
176
],
81
177
'soap ' => [
82
178
'service ' => GuestCartItemRepositoryTest::SERVICE_NAME ,
@@ -93,7 +189,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
93
189
$ serviceInfoForAddingProduct = [
94
190
'rest ' => [
95
191
'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items ' ,
96
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
192
+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
97
193
],
98
194
'soap ' => [
99
195
'service ' => GuestCartItemRepositoryTest::SERVICE_NAME ,
@@ -112,8 +208,8 @@ public function testPriceForCreatingQuoteFromEmptyCart()
112
208
$ this ->assertNotEmpty ($ item );
113
209
$ this ->assertEquals ($ item ['price ' ], 10 );
114
210
115
- /** @var \Magento\Quote\Model\ Quote $quote */
116
- $ quote = $ this ->objectManager ->create (\ Magento \ Quote \ Model \ Quote::class);
211
+ /** @var Quote $quote */
212
+ $ quote = $ this ->objectManager ->create (Quote::class);
117
213
$ quote ->load ($ quoteId );
118
214
$ quote ->delete ();
119
215
}
0 commit comments