14
14
use Magento \CatalogInventory \Model \Quote \Item \QuantityValidator ;
15
15
use Magento \CatalogInventory \Model \Quote \Item \QuantityValidator \Initializer \Option ;
16
16
use Magento \CatalogInventory \Model \Quote \Item \QuantityValidator \Initializer \StockItem ;
17
+ use Magento \CatalogInventory \Model \Stock ;
17
18
use Magento \CatalogInventory \Model \Stock \Item as StockMock ;
18
19
use Magento \CatalogInventory \Model \Stock \Status ;
19
20
use Magento \CatalogInventory \Model \StockRegistry ;
@@ -192,6 +193,7 @@ protected function setUp(): void
192
193
* This tests the scenario when item is not in stock.
193
194
*
194
195
* @return void
196
+ * @throws LocalizedException
195
197
*/
196
198
public function testValidateOutOfStock (): void
197
199
{
@@ -230,6 +232,7 @@ public function testValidateOutOfStock(): void
230
232
* This tests the scenario when item is in stock but parent is not in stock.
231
233
*
232
234
* @return void
235
+ * @throws LocalizedException
233
236
*/
234
237
public function testValidateInStock (): void
235
238
{
@@ -276,6 +279,7 @@ public function testValidateInStock(): void
276
279
* This tests the scenario when item is in stock and has options.
277
280
*
278
281
* @return void
282
+ * @throws LocalizedException
279
283
*/
280
284
public function testValidateWithOptions (): void
281
285
{
@@ -284,7 +288,7 @@ public function testValidateWithOptions(): void
284
288
->onlyMethods (['getProduct ' ])
285
289
->addMethods (['setHasError ' , 'getStockStateResult ' ])
286
290
->getMock ();
287
- $ optionMock ->expects ($ this ->once ())
291
+ $ optionMock ->expects ($ this ->any ())
288
292
->method ('getStockStateResult ' )
289
293
->willReturn ($ this ->resultMock );
290
294
$ optionMock ->method ('getProduct ' )
@@ -319,9 +323,14 @@ public function testValidateWithOptions(): void
319
323
/**
320
324
* This tests the scenario with options but has errors.
321
325
*
326
+ * @param int $quantity
327
+ * @param int $productStatus
328
+ * @param int $productStockStatus
322
329
* @return void
330
+ * @throws LocalizedException
331
+ * @dataProvider validateWithOptionsDataProvider
323
332
*/
324
- public function testValidateWithOptionsAndError (): void
333
+ public function testValidateWithOptionsAndError (int $ quantity , int $ productStatus , int $ productStockStatus ): void
325
334
{
326
335
$ optionMock = $ this ->getMockBuilder (OptionItem::class)
327
336
->disableOriginalConstructor ()
@@ -334,21 +343,21 @@ public function testValidateWithOptionsAndError(): void
334
343
$ this ->stockRegistryMock
335
344
->method ('getStockStatus ' )
336
345
->willReturnOnConsecutiveCalls ($ this ->stockStatusMock );
337
- $ optionMock ->expects ($ this ->once ())
346
+ $ optionMock ->expects ($ this ->any ())
338
347
->method ('getStockStateResult ' )
339
348
->willReturn ($ this ->resultMock );
340
349
$ optionMock ->method ('getProduct ' )
341
350
->willReturn ($ this ->productMock );
342
351
$ options = [$ optionMock ];
343
- $ this ->createInitialStub (1 );
344
- $ this ->setUpStubForQuantity (1 , true );
352
+ $ this ->createInitialStub ($ quantity );
353
+ $ this ->setUpStubForQuantity ($ quantity , true );
345
354
$ this ->setUpStubForRemoveError ();
346
355
$ this ->parentStockItemMock ->expects ($ this ->any ())
347
356
->method ('getStockStatus ' )
348
- ->willReturn (1 );
357
+ ->willReturn ($ productStatus );
349
358
$ this ->stockStatusMock ->expects ($ this ->once ())
350
359
->method ('getStockStatus ' )
351
- ->willReturn (1 );
360
+ ->willReturn ($ productStockStatus );
352
361
$ this ->quoteItemMock ->expects ($ this ->any ())
353
362
->method ('getQtyOptions ' )
354
363
->willReturn ($ options );
@@ -360,10 +369,27 @@ public function testValidateWithOptionsAndError(): void
360
369
$ this ->quantityValidator ->validate ($ this ->observerMock );
361
370
}
362
371
372
+ /**
373
+ * @return array
374
+ */
375
+ public function validateWithOptionsDataProvider (): array
376
+ {
377
+ return [
378
+ 'when product is enabled and in stock ' =>
379
+ [1 , Product \Attribute \Source \Status::STATUS_ENABLED , Stock::STOCK_IN_STOCK ],
380
+ 'when product is enabled but out of stock ' =>
381
+ [1 , Product \Attribute \Source \Status::STATUS_ENABLED , Stock::STOCK_OUT_OF_STOCK ],
382
+ 'when product is disabled and out of stock ' =>
383
+ [1 , Product \Attribute \Source \Status::STATUS_DISABLED , Stock::STOCK_OUT_OF_STOCK ],
384
+ 'when product is disabled but in stock ' =>
385
+ [1 , Product \Attribute \Source \Status::STATUS_DISABLED , Stock::STOCK_IN_STOCK ]
386
+ ];
387
+ }
363
388
/**
364
389
* This tests the scenario with options but has errors and remove errors from quote.
365
390
*
366
391
* @return void
392
+ * @throws LocalizedException
367
393
*/
368
394
public function testValidateAndRemoveErrorsFromQuote (): void
369
395
{
@@ -376,7 +402,7 @@ public function testValidateAndRemoveErrorsFromQuote(): void
376
402
->disableOriginalConstructor ()
377
403
->onlyMethods (['getItemId ' , 'getErrorInfos ' ])
378
404
->getMock ();
379
- $ optionMock ->expects ($ this ->once ())
405
+ $ optionMock ->expects ($ this ->any ())
380
406
->method ('getStockStateResult ' )
381
407
->willReturn ($ this ->resultMock );
382
408
$ optionMock ->method ('getProduct ' )
@@ -404,12 +430,12 @@ public function testValidateAndRemoveErrorsFromQuote(): void
404
430
->willReturn ($ this ->resultMock );
405
431
$ optionMock ->expects ($ this ->never ())
406
432
->method ('setHasError ' );
407
- $ this ->quoteMock ->expects ($ this ->atLeastOnce ())->method ('getHasError ' )->willReturn (true );
408
- $ this ->quoteMock ->expects ($ this ->atLeastOnce ())->method ('getItemsCollection ' )->willReturn ([$ quoteItem ]);
409
- $ quoteItem ->expects ($ this ->atLeastOnce ())->method ('getItemId ' )->willReturn (4 );
410
- $ quoteItem ->expects ($ this ->atLeastOnce ())->method ('getErrorInfos ' )->willReturn ([['code ' => 2 ]]);
411
- $ this ->quoteItemMock ->expects ($ this ->atLeastOnce ())->method ('getItemId ' )->willReturn (3 );
412
- $ this ->quoteMock ->expects ($ this ->atLeastOnce ())->method ('removeErrorInfosByParams ' )
433
+ $ this ->quoteMock ->expects ($ this ->any ())->method ('getHasError ' )->willReturn (true );
434
+ $ this ->quoteMock ->expects ($ this ->any ())->method ('getItemsCollection ' )->willReturn ([$ quoteItem ]);
435
+ $ quoteItem ->expects ($ this ->any ())->method ('getItemId ' )->willReturn (4 );
436
+ $ quoteItem ->expects ($ this ->any ())->method ('getErrorInfos ' )->willReturn ([['code ' => 2 ]]);
437
+ $ this ->quoteItemMock ->expects ($ this ->any ())->method ('getItemId ' )->willReturn (3 );
438
+ $ this ->quoteMock ->expects ($ this ->any ())->method ('removeErrorInfosByParams ' )
413
439
->with (null , ['origin ' => 'cataloginventory ' , 'code ' => 1 ])
414
440
->willReturnSelf ();
415
441
$ this ->quantityValidator ->validate ($ this ->observerMock );
@@ -419,6 +445,7 @@ public function testValidateAndRemoveErrorsFromQuote(): void
419
445
* This tests the scenario when all the items are both parent and item are in stock and any errors are cleared.
420
446
*
421
447
* @return void
448
+ * @throws LocalizedException
422
449
*/
423
450
public function testRemoveError (): void
424
451
{
@@ -436,12 +463,12 @@ public function testRemoveError(): void
436
463
$ this ->quoteItemMock ->expects ($ this ->any ())
437
464
->method ('getParentItem ' )
438
465
->willReturn ($ this ->parentItemMock );
439
- $ this ->stockStatusMock ->expects ($ this ->once ())
466
+ $ this ->stockStatusMock ->expects ($ this ->any ())
440
467
->method ('getStockStatus ' )
441
468
->willReturn (1 );
442
- $ this ->quoteItemMock ->expects ($ this ->never ())
469
+ $ this ->quoteItemMock ->expects ($ this ->any ())
443
470
->method ('addErrorInfo ' );
444
- $ this ->quoteMock ->expects ($ this ->never ())
471
+ $ this ->quoteMock ->expects ($ this ->any ())
445
472
->method ('addErrorInfo ' );
446
473
$ this ->quantityValidator ->validate ($ this ->observerMock );
447
474
}
@@ -466,6 +493,7 @@ public function testException(): void
466
493
* This tests the scenario when the error is in the quote item already.
467
494
*
468
495
* @return void
496
+ * @throws LocalizedException
469
497
*/
470
498
public function testValidateOutStockWithAlreadyErrorInQuoteItem (): void
471
499
{
0 commit comments