77
88namespace Magento \Quote \Api ;
99
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 ;
1016use Magento \TestFramework \TestCase \WebapiAbstract ;
1117
1218/**
1319 * Class for testing adding and deleting items flow.
1420 */
1521class GuestCartAddingItemsTest extends WebapiAbstract
1622{
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/ ' ;
2026
2127 /**
2228 * @var \Magento\TestFramework\ObjectManager
2329 */
2430 protected $ objectManager ;
31+
32+ /**
33+ * @var ProductResource|mixed
34+ */
35+ private mixed $ productResource ;
2536
2637 protected function setUp (): void
2738 {
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 );
29125 }
30126
31127 /**
@@ -40,7 +136,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
40136 $ serviceInfoForCreatingEmptyCart = [
41137 'rest ' => [
42138 'resourcePath ' => self ::RESOURCE_PATH ,
43- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
139+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
44140 ],
45141 'soap ' => [
46142 'service ' => self ::SERVICE_NAME ,
@@ -54,7 +150,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
54150 $ serviceInfoForAddingProduct = [
55151 'rest ' => [
56152 'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items ' ,
57- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
153+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
58154 ],
59155 'soap ' => [
60156 'service ' => GuestCartItemRepositoryTest::SERVICE_NAME ,
@@ -76,7 +172,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
76172 $ serviceInfoForDeleteProduct = [
77173 'rest ' => [
78174 '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 ,
80176 ],
81177 'soap ' => [
82178 'service ' => GuestCartItemRepositoryTest::SERVICE_NAME ,
@@ -93,7 +189,7 @@ public function testPriceForCreatingQuoteFromEmptyCart()
93189 $ serviceInfoForAddingProduct = [
94190 'rest ' => [
95191 'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items ' ,
96- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST ,
192+ 'httpMethod ' => Request::HTTP_METHOD_POST ,
97193 ],
98194 'soap ' => [
99195 'service ' => GuestCartItemRepositoryTest::SERVICE_NAME ,
@@ -112,8 +208,8 @@ public function testPriceForCreatingQuoteFromEmptyCart()
112208 $ this ->assertNotEmpty ($ item );
113209 $ this ->assertEquals ($ item ['price ' ], 10 );
114210
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);
117213 $ quote ->load ($ quoteId );
118214 $ quote ->delete ();
119215 }
0 commit comments