@@ -301,6 +301,65 @@ public function testBeforeSaveProductWebsiteIdsNotArray()
301301 $ this ->plugin ->beforeSave ($ this ->cartItemRepositoryMock , $ this ->cartItemMock );
302302 }
303303
304+ /**
305+ * Test validation when product is not found
306+ */
307+ public function testBeforeSaveProductNotFound ()
308+ {
309+ $ sku = 'test-product ' ;
310+ $ quoteId = 1 ;
311+ $ storeId = 1 ;
312+ $ productId = 123 ;
313+
314+ $ this ->cartItemMock ->expects ($ this ->once ())
315+ ->method ('getSku ' )
316+ ->willReturn ($ sku );
317+
318+ $ this ->cartItemMock ->expects ($ this ->once ())
319+ ->method ('getQuoteId ' )
320+ ->willReturn ($ quoteId );
321+
322+ $ this ->cartRepositoryMock ->expects ($ this ->once ())
323+ ->method ('getActive ' )
324+ ->with ($ quoteId )
325+ ->willReturn ($ this ->quoteMock );
326+
327+ $ this ->quoteMock ->expects ($ this ->once ())
328+ ->method ('getAllItems ' )
329+ ->willReturn ([$ this ->quoteItemMock ]);
330+
331+ $ this ->quoteItemMock ->expects ($ this ->once ())
332+ ->method ('getSku ' )
333+ ->willReturn ($ sku );
334+
335+ $ this ->quoteItemMock ->expects ($ this ->once ())
336+ ->method ('getStoreId ' )
337+ ->willReturn ($ storeId );
338+
339+ $ this ->quoteItemMock ->expects ($ this ->once ())
340+ ->method ('getProductId ' )
341+ ->willReturn ($ productId );
342+
343+ $ this ->storeManagerMock ->expects ($ this ->once ())
344+ ->method ('getStore ' )
345+ ->with ($ storeId )
346+ ->willReturn ($ this ->storeMock );
347+
348+ $ this ->storeMock ->expects ($ this ->once ())
349+ ->method ('getWebsiteId ' )
350+ ->willReturn (1 );
351+
352+ $ this ->productRepositoryMock ->expects ($ this ->once ())
353+ ->method ('getById ' )
354+ ->with ($ productId , false , $ storeId )
355+ ->willThrowException (new NoSuchEntityException (__ ('Product not found ' )));
356+
357+ $ this ->expectException (LocalizedException::class);
358+ $ this ->expectExceptionMessage ('Product that you are trying to add is not available. ' );
359+
360+ $ this ->plugin ->beforeSave ($ this ->cartItemRepositoryMock , $ this ->cartItemMock );
361+ }
362+
304363 /**
305364 * Test validation skips when no SKU provided
306365 */
@@ -531,4 +590,42 @@ public function testBeforeSaveWebsiteIdZero()
531590 // If we reach this point, validation passed
532591 $ this ->assertTrue (true );
533592 }
593+
594+ /**
595+ * Test validation when no matching quote item found for SKU
596+ */
597+ public function testBeforeSaveNoMatchingQuoteItem ()
598+ {
599+ $ sku = 'test-product ' ;
600+ $ quoteId = 1 ;
601+ $ differentSku = 'different-product ' ;
602+
603+ $ this ->cartItemMock ->expects ($ this ->once ())
604+ ->method ('getSku ' )
605+ ->willReturn ($ sku );
606+
607+ $ this ->cartItemMock ->expects ($ this ->once ())
608+ ->method ('getQuoteId ' )
609+ ->willReturn ($ quoteId );
610+
611+ $ this ->cartRepositoryMock ->expects ($ this ->once ())
612+ ->method ('getActive ' )
613+ ->with ($ quoteId )
614+ ->willReturn ($ this ->quoteMock );
615+
616+ $ this ->quoteMock ->expects ($ this ->once ())
617+ ->method ('getAllItems ' )
618+ ->willReturn ([$ this ->quoteItemMock ]);
619+
620+ $ this ->quoteItemMock ->expects ($ this ->once ())
621+ ->method ('getSku ' )
622+ ->willReturn ($ differentSku ); // Different SKU
623+
624+ // No further method calls expected since no matching item found
625+ // Method returns void, so we just verify no exception is thrown
626+ $ this ->plugin ->beforeSave ($ this ->cartItemRepositoryMock , $ this ->cartItemMock );
627+
628+ // If we reach this point, validation passed
629+ $ this ->assertTrue (true );
630+ }
534631}
0 commit comments