3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Bundle \Api ;
7
9
10
+ use Magento \Framework \Webapi \Rest \Request ;
11
+ use Magento \Quote \Model \Quote ;
12
+ use Magento \TestFramework \Helper \Bootstrap ;
13
+ use Magento \TestFramework \ObjectManager ;
8
14
use Magento \TestFramework \TestCase \WebapiAbstract ;
9
15
16
+ /**
17
+ * API test for cart item repository with bundle product.
18
+ */
10
19
class CartItemRepositoryTest extends WebapiAbstract
11
20
{
12
21
const SERVICE_VERSION = 'V1 ' ;
13
22
const SERVICE_NAME = 'quoteCartItemRepositoryV1 ' ;
14
23
const RESOURCE_PATH = '/V1/carts/ ' ;
15
24
16
25
/**
17
- * @var \Magento\TestFramework\ ObjectManager
26
+ * @var ObjectManager
18
27
*/
19
28
protected $ objectManager ;
20
29
30
+ /**
31
+ * @inheritDoc
32
+ */
21
33
protected function setUp (): void
22
34
{
23
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
35
+ $ this ->objectManager = Bootstrap::getObjectManager ();
24
36
}
25
37
26
38
/**
27
39
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_bundle_and_options.php
28
40
*/
29
- public function testGetAll ()
41
+ public function testGetAll (): void
30
42
{
31
- /** @var $quote \Magento\Quote\Model\ Quote */
32
- $ quote = $ this ->objectManager ->create (\ Magento \ Quote \ Model \ Quote::class)->load (
43
+ /** @var $quote Quote */
44
+ $ quote = $ this ->objectManager ->create (Quote::class)->load (
33
45
'test_order_bundle ' ,
34
46
'reserved_order_id '
35
47
);
@@ -38,7 +50,7 @@ public function testGetAll()
38
50
$ serviceInfo = [
39
51
'rest ' => [
40
52
'resourcePath ' => self ::RESOURCE_PATH . $ quoteId . '/items ' ,
41
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_GET ,
53
+ 'httpMethod ' => Request::HTTP_METHOD_GET ,
42
54
],
43
55
'soap ' => [
44
56
'service ' => self ::SERVICE_NAME ,
@@ -61,146 +73,4 @@ public function testGetAll()
61
73
$ this ->assertEquals ($ expectedSelections , $ option ['option_selections ' ]);
62
74
}
63
75
}
64
-
65
- /**
66
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
67
- * @magentoApiDataFixture Magento/Bundle/_files/product.php
68
- */
69
- public function testAddItem ()
70
- {
71
- /** @var \Magento\Catalog\Model\Product $product */
72
- $ product = $ this ->objectManager ->create (\Magento \Catalog \Model \Product::class)->load (3 );
73
- $ quote = $ this ->objectManager ->create (\Magento \Quote \Model \Quote::class)->load (
74
- 'test_order_item_with_items ' ,
75
- 'reserved_order_id '
76
- );
77
-
78
- $ itemQty = 1 ;
79
- $ bundleProductOptions = $ product ->getExtensionAttributes ()->getBundleProductOptions ();
80
- $ bundleOptionId = $ bundleProductOptions [0 ]->getId ();
81
- $ optionSelections = $ bundleProductOptions [0 ]->getProductLinks ()[0 ]->getId ();
82
- $ buyRequest = [
83
- 'bundle_option ' => [$ bundleOptionId => [$ optionSelections ]],
84
- 'bundle_option_qty ' => [$ bundleOptionId => 1 ],
85
- 'qty ' => $ itemQty ,
86
- 'original_qty ' => $ itemQty
87
- ];
88
-
89
- $ productSku = $ product ->getSku ();
90
- $ productId = $ product ->getId ();
91
- /** @var \Magento\Quote\Model\Quote $quote */
92
-
93
- $ cartId = $ quote ->getId ();
94
-
95
- $ serviceInfo = [
96
- 'rest ' => [
97
- 'resourcePath ' => self ::RESOURCE_PATH . $ cartId . '/items ' ,
98
- 'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_POST ,
99
- ],
100
- 'soap ' => [
101
- 'service ' => self ::SERVICE_NAME ,
102
- 'serviceVersion ' => self ::SERVICE_VERSION ,
103
- 'operation ' => self ::SERVICE_NAME . 'Save ' ,
104
- ],
105
- ];
106
-
107
- $ requestData = [
108
- "cartItem " => [
109
- "sku " => $ productSku ,
110
- "qty " => $ itemQty ,
111
- "quote_id " => $ cartId ,
112
- "product_option " => [
113
- "extension_attributes " => [
114
- "bundle_options " => [
115
- [
116
- "option_id " => (int )$ bundleOptionId ,
117
- "option_qty " => $ itemQty ,
118
- "option_selections " => [(int )$ optionSelections ]
119
- ]
120
- ]
121
- ]
122
- ]
123
- ]
124
- ];
125
- $ response = $ this ->_webApiCall ($ serviceInfo , $ requestData );
126
- $ this ->assertTrue ($ quote ->hasProductId ($ productId ));
127
- $ this ->assertEquals ($ buyRequest , $ quote ->getItemById ($ response ['item_id ' ])->getBuyRequest ()->getData ());
128
- }
129
-
130
- /**
131
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_bundle_and_options.php
132
- */
133
- public function testUpdate ()
134
- {
135
- /** @var \Magento\Quote\Model\Quote $quote */
136
- $ quote = $ this ->objectManager ->create (\Magento \Quote \Model \Quote::class)->load (
137
- 'test_order_bundle ' ,
138
- 'reserved_order_id '
139
- );
140
- $ cartId = $ quote ->getId ();
141
- $ cartItem = $ quote ->getAllVisibleItems ()[0 ];
142
- $ itemSku = $ cartItem ->getSku ();
143
- $ itemId = $ cartItem ->getId ();
144
-
145
- $ product = $ cartItem ->getProduct ();
146
- /** @var $typeInstance \Magento\Bundle\Model\Product\Type */
147
- $ typeInstance = $ product ->getTypeInstance ();
148
- $ typeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
149
- $ optionCollection = $ typeInstance ->getOptionsCollection ($ product );
150
- $ bundleOptions = [];
151
- /** @var $option \Magento\Bundle\Model\Option */
152
- foreach ($ optionCollection as $ option ) {
153
- if (!$ option ->getRequired ()) {
154
- continue ;
155
- }
156
- $ selectionsCollection = $ typeInstance ->getSelectionsCollection ([$ option ->getId ()], $ product );
157
- $ option = ['option_id ' => $ option ->getId (), 'option_qty ' => 1 ];
158
- $ option ['option_selections ' ] = [$ selectionsCollection ->getLastItem ()->getSelectionId ()];
159
- $ bundleOptions [] = $ option ;
160
- }
161
-
162
- $ serviceInfo = [
163
- 'rest ' => [
164
- 'resourcePath ' => self ::RESOURCE_PATH . $ cartId . '/items/ ' . $ itemId ,
165
- 'httpMethod ' => \Magento \Framework \Webapi \Rest \Request::HTTP_METHOD_PUT ,
166
- ],
167
- 'soap ' => [
168
- 'service ' => self ::SERVICE_NAME ,
169
- 'serviceVersion ' => self ::SERVICE_VERSION ,
170
- 'operation ' => self ::SERVICE_NAME . 'Save ' ,
171
- ],
172
- ];
173
- $ requestData = [
174
- "cartItem " => [
175
- "sku " => $ itemSku ,
176
- "qty " => 2 ,
177
- "quote_id " => $ cartId ,
178
- "item_id " => $ itemId ,
179
- "product_option " => [
180
- "extension_attributes " => [
181
- "bundle_options " => $ bundleOptions
182
- ]
183
- ]
184
- ]
185
- ];
186
- $ this ->_webApiCall ($ serviceInfo , $ requestData );
187
-
188
- $ quoteUpdated = $ this ->objectManager ->create (\Magento \Quote \Model \Quote::class)->load (
189
- 'test_order_bundle ' ,
190
- 'reserved_order_id '
191
- );
192
- $ cartItems = $ quoteUpdated ->getAllVisibleItems ();
193
- $ buyRequest = $ cartItems [0 ]->getBuyRequest ()->toArray ();
194
-
195
- $ this ->assertCount (1 , $ cartItems );
196
- $ this ->assertEquals (count ($ buyRequest ['bundle_option ' ]), count ($ bundleOptions ));
197
- foreach ($ bundleOptions as $ option ) {
198
- $ optionId = $ option ['option_id ' ];
199
- $ optionQty = $ option ['option_qty ' ];
200
- $ optionSelections = $ option ['option_selections ' ];
201
- $ this ->assertArrayHasKey ($ optionId , $ buyRequest ['bundle_option ' ]);
202
- $ this ->assertEquals ($ optionQty , $ buyRequest ['bundle_option_qty ' ][$ optionId ]);
203
- $ this ->assertEquals ($ optionSelections , $ buyRequest ['bundle_option ' ][$ optionId ]);
204
- }
205
- }
206
76
}
0 commit comments