1414use Magento \CatalogInventory \Model \Quote \Item \QuantityValidator ;
1515use Magento \CatalogInventory \Model \Quote \Item \QuantityValidator \Initializer \Option ;
1616use Magento \CatalogInventory \Model \Quote \Item \QuantityValidator \Initializer \StockItem ;
17+ use Magento \CatalogInventory \Model \Stock ;
1718use Magento \CatalogInventory \Model \Stock \Item as StockMock ;
1819use Magento \CatalogInventory \Model \Stock \Status ;
1920use Magento \CatalogInventory \Model \StockRegistry ;
@@ -192,6 +193,7 @@ protected function setUp(): void
192193 * This tests the scenario when item is not in stock.
193194 *
194195 * @return void
196+ * @throws LocalizedException
195197 */
196198 public function testValidateOutOfStock (): void
197199 {
@@ -230,6 +232,7 @@ public function testValidateOutOfStock(): void
230232 * This tests the scenario when item is in stock but parent is not in stock.
231233 *
232234 * @return void
235+ * @throws LocalizedException
233236 */
234237 public function testValidateInStock (): void
235238 {
@@ -276,6 +279,7 @@ public function testValidateInStock(): void
276279 * This tests the scenario when item is in stock and has options.
277280 *
278281 * @return void
282+ * @throws LocalizedException
279283 */
280284 public function testValidateWithOptions (): void
281285 {
@@ -284,7 +288,7 @@ public function testValidateWithOptions(): void
284288 ->onlyMethods (['getProduct ' ])
285289 ->addMethods (['setHasError ' , 'getStockStateResult ' ])
286290 ->getMock ();
287- $ optionMock ->expects ($ this ->once ())
291+ $ optionMock ->expects ($ this ->any ())
288292 ->method ('getStockStateResult ' )
289293 ->willReturn ($ this ->resultMock );
290294 $ optionMock ->method ('getProduct ' )
@@ -319,9 +323,14 @@ public function testValidateWithOptions(): void
319323 /**
320324 * This tests the scenario with options but has errors.
321325 *
326+ * @param int $quantity
327+ * @param int $productStatus
328+ * @param int $productStockStatus
322329 * @return void
330+ * @throws LocalizedException
331+ * @dataProvider validateWithOptionsDataProvider
323332 */
324- public function testValidateWithOptionsAndError (): void
333+ public function testValidateWithOptionsAndError (int $ quantity , int $ productStatus , int $ productStockStatus ): void
325334 {
326335 $ optionMock = $ this ->getMockBuilder (OptionItem::class)
327336 ->disableOriginalConstructor ()
@@ -334,21 +343,21 @@ public function testValidateWithOptionsAndError(): void
334343 $ this ->stockRegistryMock
335344 ->method ('getStockStatus ' )
336345 ->willReturnOnConsecutiveCalls ($ this ->stockStatusMock );
337- $ optionMock ->expects ($ this ->once ())
346+ $ optionMock ->expects ($ this ->any ())
338347 ->method ('getStockStateResult ' )
339348 ->willReturn ($ this ->resultMock );
340349 $ optionMock ->method ('getProduct ' )
341350 ->willReturn ($ this ->productMock );
342351 $ options = [$ optionMock ];
343- $ this ->createInitialStub (1 );
344- $ this ->setUpStubForQuantity (1 , true );
352+ $ this ->createInitialStub ($ quantity );
353+ $ this ->setUpStubForQuantity ($ quantity , true );
345354 $ this ->setUpStubForRemoveError ();
346355 $ this ->parentStockItemMock ->expects ($ this ->any ())
347356 ->method ('getStockStatus ' )
348- ->willReturn (1 );
357+ ->willReturn ($ productStatus );
349358 $ this ->stockStatusMock ->expects ($ this ->once ())
350359 ->method ('getStockStatus ' )
351- ->willReturn (1 );
360+ ->willReturn ($ productStockStatus );
352361 $ this ->quoteItemMock ->expects ($ this ->any ())
353362 ->method ('getQtyOptions ' )
354363 ->willReturn ($ options );
@@ -360,10 +369,27 @@ public function testValidateWithOptionsAndError(): void
360369 $ this ->quantityValidator ->validate ($ this ->observerMock );
361370 }
362371
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+ }
363388 /**
364389 * This tests the scenario with options but has errors and remove errors from quote.
365390 *
366391 * @return void
392+ * @throws LocalizedException
367393 */
368394 public function testValidateAndRemoveErrorsFromQuote (): void
369395 {
@@ -376,7 +402,7 @@ public function testValidateAndRemoveErrorsFromQuote(): void
376402 ->disableOriginalConstructor ()
377403 ->onlyMethods (['getItemId ' , 'getErrorInfos ' ])
378404 ->getMock ();
379- $ optionMock ->expects ($ this ->once ())
405+ $ optionMock ->expects ($ this ->any ())
380406 ->method ('getStockStateResult ' )
381407 ->willReturn ($ this ->resultMock );
382408 $ optionMock ->method ('getProduct ' )
@@ -404,12 +430,12 @@ public function testValidateAndRemoveErrorsFromQuote(): void
404430 ->willReturn ($ this ->resultMock );
405431 $ optionMock ->expects ($ this ->never ())
406432 ->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 ' )
413439 ->with (null , ['origin ' => 'cataloginventory ' , 'code ' => 1 ])
414440 ->willReturnSelf ();
415441 $ this ->quantityValidator ->validate ($ this ->observerMock );
@@ -419,6 +445,7 @@ public function testValidateAndRemoveErrorsFromQuote(): void
419445 * This tests the scenario when all the items are both parent and item are in stock and any errors are cleared.
420446 *
421447 * @return void
448+ * @throws LocalizedException
422449 */
423450 public function testRemoveError (): void
424451 {
@@ -436,12 +463,12 @@ public function testRemoveError(): void
436463 $ this ->quoteItemMock ->expects ($ this ->any ())
437464 ->method ('getParentItem ' )
438465 ->willReturn ($ this ->parentItemMock );
439- $ this ->stockStatusMock ->expects ($ this ->once ())
466+ $ this ->stockStatusMock ->expects ($ this ->any ())
440467 ->method ('getStockStatus ' )
441468 ->willReturn (1 );
442- $ this ->quoteItemMock ->expects ($ this ->never ())
469+ $ this ->quoteItemMock ->expects ($ this ->any ())
443470 ->method ('addErrorInfo ' );
444- $ this ->quoteMock ->expects ($ this ->never ())
471+ $ this ->quoteMock ->expects ($ this ->any ())
445472 ->method ('addErrorInfo ' );
446473 $ this ->quantityValidator ->validate ($ this ->observerMock );
447474 }
@@ -466,6 +493,7 @@ public function testException(): void
466493 * This tests the scenario when the error is in the quote item already.
467494 *
468495 * @return void
496+ * @throws LocalizedException
469497 */
470498 public function testValidateOutStockWithAlreadyErrorInQuoteItem (): void
471499 {
0 commit comments