6
6
*/
7
7
namespace Magento \Quote \Api ;
8
8
9
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
10
+ use Magento \CatalogInventory \Model \Stock ;
9
11
use Magento \TestFramework \TestCase \WebapiAbstract ;
10
12
11
13
class GuestCartItemRepositoryTest extends WebapiAbstract
@@ -167,9 +169,13 @@ public function testRemoveItem()
167
169
168
170
/**
169
171
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
172
+ * @param array $stockData
173
+ * @param string|null $errorMessage
174
+ * @dataProvider updateItemDataProvider
170
175
*/
171
- public function testUpdateItem ()
176
+ public function testUpdateItem (array $ stockData , string $ errorMessage = null )
172
177
{
178
+ $ this ->updateStockData ('simple_one ' , $ stockData );
173
179
/** @var \Magento\Quote\Model\Quote $quote */
174
180
$ quote = $ this ->objectManager ->create (\Magento \Quote \Model \Quote::class);
175
181
$ quote ->load ('test_order_item_with_items ' , 'reserved_order_id ' );
@@ -215,6 +221,9 @@ public function testUpdateItem()
215
221
],
216
222
];
217
223
}
224
+ if ($ errorMessage ) {
225
+ $ this ->expectExceptionMessage ($ errorMessage );
226
+ }
218
227
$ this ->_webApiCall ($ serviceInfo , $ requestData );
219
228
$ quote = $ this ->objectManager ->create (\Magento \Quote \Model \Quote::class);
220
229
$ quote ->load ('test_order_item_with_items ' , 'reserved_order_id ' );
@@ -223,4 +232,66 @@ public function testUpdateItem()
223
232
$ this ->assertEquals (5 , $ item ->getQty ());
224
233
$ this ->assertEquals ($ itemId , $ item ->getItemId ());
225
234
}
235
+
236
+ /**
237
+ * @return array
238
+ */
239
+ public function updateItemDataProvider (): array
240
+ {
241
+ return [
242
+ [
243
+ []
244
+ ],
245
+ [
246
+ [
247
+ 'qty ' => 0 ,
248
+ 'is_in_stock ' => 1 ,
249
+ 'use_config_manage_stock ' => 0 ,
250
+ 'manage_stock ' => 1 ,
251
+ 'use_config_backorders ' => 0 ,
252
+ 'backorders ' => Stock::BACKORDERS_YES_NOTIFY ,
253
+ ]
254
+ ],
255
+ [
256
+ [
257
+ 'qty ' => 0 ,
258
+ 'is_in_stock ' => 1 ,
259
+ 'use_config_manage_stock ' => 0 ,
260
+ 'manage_stock ' => 1 ,
261
+ 'use_config_backorders ' => 0 ,
262
+ 'backorders ' => Stock::BACKORDERS_NO ,
263
+ ],
264
+ 'This product is out of stock. '
265
+ ],
266
+ [
267
+ [
268
+ 'qty ' => 2 ,
269
+ 'is_in_stock ' => 1 ,
270
+ 'use_config_manage_stock ' => 0 ,
271
+ 'manage_stock ' => 1 ,
272
+ 'use_config_backorders ' => 0 ,
273
+ 'backorders ' => Stock::BACKORDERS_NO ,
274
+ ],
275
+ 'The requested qty is not available '
276
+ ]
277
+ ];
278
+ }
279
+
280
+ /**
281
+ * Update product stock
282
+ *
283
+ * @param string $sku
284
+ * @param array $stockData
285
+ * @return void
286
+ */
287
+ private function updateStockData (string $ sku , array $ stockData ): void
288
+ {
289
+ if ($ stockData ) {
290
+ /** @var $stockRegistry StockRegistryInterface */
291
+ $ stockRegistry = $ this ->objectManager ->create (StockRegistryInterface::class);
292
+ $ stockItem = $ stockRegistry ->getStockItemBySku ($ sku );
293
+ $ stockItem ->addData ($ stockData );
294
+ $ stockRegistry ->updateStockItemBySku ($ sku , $ stockItem );
295
+ }
296
+ }
226
297
}
0 commit comments