diff --git a/InventoryCatalog/Plugin/InventoryIndexer/Model/ResourceModel/GetBulkLegacyStockStatusDataFromStockRegistry.php b/InventoryCatalog/Plugin/InventoryIndexer/Model/ResourceModel/GetBulkLegacyStockStatusDataFromStockRegistry.php new file mode 100644 index 000000000000..913d29c75465 --- /dev/null +++ b/InventoryCatalog/Plugin/InventoryIndexer/Model/ResourceModel/GetBulkLegacyStockStatusDataFromStockRegistry.php @@ -0,0 +1,110 @@ +stockConfiguration = $stockConfiguration; + $this->legacyStockStatusStorage = $legacyStockStatusStorage; + $this->getProductIdsBySkus = $getProductIdsBySkus; + $this->defaultStockProvider = $defaultStockProvider; + } + + /** + * Retrieve legacy stock item data from stock registry by bulk operation + * + * @param GetStockItemsData $subject + * @param callable $proceed + * @param array $skus + * @param int $stockId + * @return array + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function aroundExecute(GetStockItemsData $subject, callable $proceed, array $skus, int $stockId): array + { + $results = []; + + if ($this->defaultStockProvider->getId() === $stockId) { + try { + $productIds = $this->getProductIdsBySkus->execute($skus); + } catch (NoSuchEntityException $e) { + return $proceed($skus, $stockId); + } + + foreach ($skus as $sku) { + $productId = $productIds[$sku] ?? null; + + if ($productId !== null) { + $stockItem = $this->legacyStockStatusStorage->get( + (int) $productId, + $this->stockConfiguration->getDefaultScopeId() + ); + + if ($stockItem !== null) { + $results[$sku] = [ + GetStockItemDataInterface::QUANTITY => $stockItem->getQty(), + GetStockItemDataInterface::IS_SALABLE => $stockItem->getStockStatus(), + ]; + } + } + } + } + + $originalResults = $proceed($skus, $stockId); + + // Merging custom results with the original method results + foreach ($skus as $sku) { + $results[$sku] = $results[$sku] ?? $originalResults[$sku] ?? null; + } + + return $results; + } +} diff --git a/InventoryCatalog/etc/frontend/di.xml b/InventoryCatalog/etc/frontend/di.xml index 455f925f10d9..8f9c7371864f 100644 --- a/InventoryCatalog/etc/frontend/di.xml +++ b/InventoryCatalog/etc/frontend/di.xml @@ -10,6 +10,10 @@ + + + diff --git a/InventoryCatalog/etc/graphql/di.xml b/InventoryCatalog/etc/graphql/di.xml index 455f925f10d9..8f9c7371864f 100644 --- a/InventoryCatalog/etc/graphql/di.xml +++ b/InventoryCatalog/etc/graphql/di.xml @@ -10,6 +10,10 @@ + + + diff --git a/InventoryConfigurableProduct/Plugin/Model/Product/Type/Configurable/IsSalableOptionPlugin.php b/InventoryConfigurableProduct/Plugin/Model/Product/Type/Configurable/IsSalableOptionPlugin.php index bc8bcc35e722..deaee69b2f50 100644 --- a/InventoryConfigurableProduct/Plugin/Model/Product/Type/Configurable/IsSalableOptionPlugin.php +++ b/InventoryConfigurableProduct/Plugin/Model/Product/Type/Configurable/IsSalableOptionPlugin.php @@ -9,6 +9,8 @@ use Magento\CatalogInventory\Api\StockConfigurationInterface; use Magento\ConfigurableProduct\Model\Product\Type\Configurable; +use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\InventorySalesApi\Api\AreProductsSalableInterface; use Magento\InventorySalesApi\Api\Data\IsProductSalableResultInterface; use Magento\InventorySalesApi\Api\Data\SalesChannelInterface; @@ -69,33 +71,42 @@ public function __construct( * @param Configurable $subject * @param array $products * @return array + * @throws LocalizedException + * @throws NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterGetUsedProducts(Configurable $subject, array $products): array { - $skus = []; - foreach ($products as $product) { - foreach ($this->productsSalableStatuses as $isProductSalableResult) { - if ($isProductSalableResult->getSku() === $product->getSku()) { - continue 2; - } - } - $skus[] = $product->getSku(); - } + // Use associative array for fast SKU lookup + $salableSkus = array_flip(array_map(function ($status) { + return $status->getSku(); + }, $this->productsSalableStatuses)); + // Collect SKUs not already in $this->productsSalableStatuses + $skus = array_filter(array_map(function ($product) use ($salableSkus) { + $sku = $product->getSku(); + return isset($salableSkus[$sku]) ? null : $sku; // Return null if SKU exists, SKU otherwise + }, $products)); + + // If there are no new SKUs to process, filter products and return if (empty($skus)) { $this->filterProducts($products, $this->productsSalableStatuses); return $products; } + // Only now do we need the website and stock information $website = $this->storeManager->getWebsite(); $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $website->getCode()); + // Update products salable statuses with new salable information $this->productsSalableStatuses = array_merge( $this->productsSalableStatuses, $this->areProductsSalable->execute($skus, $stock->getStockId()) ); + + // Filter products once all updates are made $this->filterProducts($products, $this->productsSalableStatuses); + return $products; } @@ -106,15 +117,22 @@ public function afterGetUsedProducts(Configurable $subject, array $products): ar * @param array $isSalableResults * @return void */ - private function filterProducts(array $products, array $isSalableResults) : void + private function filterProducts(array &$products, array $isSalableResults) : void { + // Transform $isSalableResults into an associative array with SKU as the key + $salabilityBySku = []; + foreach ($isSalableResults as $result) { + $salabilityBySku[$result->getSku()] = $result->isSalable(); + } + foreach ($products as $key => $product) { - foreach ($isSalableResults as $result) { - if ($result->getSku() === $product->getSku() && !$result->isSalable()) { - $product->setIsSalable(0); - if (!$this->stockConfiguration->isShowOutOfStock()) { - unset($products[$key]); - } + $sku = $product->getSku(); + + // Check if the SKU exists in the salability results and if it's not salable + if (isset($salabilityBySku[$sku]) && !$salabilityBySku[$sku]) { + $product->setIsSalable(0); + if (!$this->stockConfiguration->isShowOutOfStock()) { + unset($products[$key]); } } } diff --git a/InventoryConfigurableProduct/Test/Unit/Plugin/Model/Product/Type/Configurable/IsSalableOptionPluginTest.php b/InventoryConfigurableProduct/Test/Unit/Plugin/Model/Product/Type/Configurable/IsSalableOptionPluginTest.php new file mode 100644 index 000000000000..0295c80c7f31 --- /dev/null +++ b/InventoryConfigurableProduct/Test/Unit/Plugin/Model/Product/Type/Configurable/IsSalableOptionPluginTest.php @@ -0,0 +1,197 @@ +areProductsSalableMock = $this->createMock(AreProductsSalableInterface::class); + $this->storeManagerMock = $this->createMock(StoreManagerInterface::class); + $this->stockResolverMock = $this->createMock(StockResolverInterface::class); + $this->stockConfigurationMock = $this->createMock(StockConfigurationInterface::class); + $this->configurableMock = $this->createMock(Configurable::class); + $this->websiteMock = $this->createMock(WebsiteInterface::class); + $this->stockMock = $this->createMock(StockInterface::class); + + $this->plugin = new IsSalableOptionPlugin( + $this->areProductsSalableMock, + $this->storeManagerMock, + $this->stockResolverMock, + $this->stockConfigurationMock + ); + } + + public function testAllProductsAreSalable() + { + $products = $this->createProducts(['sku1' => true, 'sku2' => true]); + $this->mockAreProductsSalable(['sku1' => true, 'sku2' => true]); + $this->createAdditionalMocks(); + + $result = $this->plugin->afterGetUsedProducts($this->configurableMock, $products); + + $this->assertEquals(2, count($result)); + foreach ($result as $product) { + $this->assertEquals(1, $product->getIsSalable()); + } + } + + /** + * @dataProvider productSalabilityDataProvider + */ + public function testSomeProductsAreNotSalable(bool $isShowOutOfStock, int $expectedCount) + { + $this->stockConfigurationMock->method('isShowOutOfStock')->willReturn($isShowOutOfStock); + + $products = $this->createProducts(['sku1' => true, 'sku2' => false, 'sku3' => true]); + $this->mockAreProductsSalable(['sku1' => true, 'sku2' => false, 'sku3' => true]); + $this->createAdditionalMocks(); + + $result = $this->plugin->afterGetUsedProducts($this->configurableMock, $products); + + $this->assertEquals($expectedCount, count($result)); + foreach ($result as $product) { + if ($product->getSku() === 'sku2') { + $this->assertEquals(0, $product->getIsSalable()); + } else { + $this->assertEquals(1, $product->getIsSalable()); + } + } + } + + public function productSalabilityDataProvider(): array + { + return [ + 'Hide Out Of Stock' => [false, 2], + 'Show Out Of Stock' => [true, 3], + ]; + } + + public function testNoProductsAreSalable() + { + $products = $this->createProducts(['sku1' => false, 'sku2' => false]); + $this->mockAreProductsSalable(['sku1' => false, 'sku2' => false]); + $this->createAdditionalMocks(); + + $result = $this->plugin->afterGetUsedProducts($this->configurableMock, $products); + + $this->assertEquals(0, count($result)); + foreach ($result as $product) { + $this->assertEquals(0, $product->getIsSalable()); + } + } + + public function testEmptyProductsArray() + { + $products = []; + + $result = $this->plugin->afterGetUsedProducts($this->configurableMock, $products); + + $this->assertIsArray($result); + $this->assertEmpty($result); + } + + private function createProducts(array $productData): array + { + return array_map(function ($sku, $isSalable) { + $productMock = $this->createMock(Product::class); + $productMock->method('getSku')->willReturn($sku); + $productMock->method('getIsSalable')->willReturn($isSalable); + return $productMock; + }, array_keys($productData), $productData); + } + + private function mockAreProductsSalable(array $skus): void + { + $salableResults = []; + + // Handle a map of SKUs to their salable statuses + foreach ($skus as $sku => $isSalable) { + $salableResultMock = $this->createMock(IsProductSalableResultInterface::class); + $salableResultMock->method('getSku')->willReturn($sku); + $salableResultMock->method('isSalable')->willReturn($isSalable); + $salableResults[] = $salableResultMock; + } + + $this->areProductsSalableMock->method('execute')->willReturn($salableResults); + } + + private function createAdditionalMocks(): void + { + $this->storeManagerMock->expects($this->once()) + ->method('getWebsite') + ->willReturn($this->websiteMock); + $this->websiteMock->expects($this->once()) + ->method('getCode') + ->willReturn('website_code'); + $this->stockResolverMock->expects($this->once()) + ->method('execute') + ->with(SalesChannelInterface::TYPE_WEBSITE, 'website_code') + ->willReturn($this->stockMock); + $this->stockMock->expects($this->once()) + ->method('getStockId') + ->willReturn(1); + } +} diff --git a/InventoryIndexer/Model/AreMultipleProductsSalable.php b/InventoryIndexer/Model/AreMultipleProductsSalable.php new file mode 100644 index 000000000000..c5e9cd0ece59 --- /dev/null +++ b/InventoryIndexer/Model/AreMultipleProductsSalable.php @@ -0,0 +1,75 @@ +getStockItemsData = $getStockItemsData; + $this->logger = $logger; + } + + /** + * Define if multiple products are salable for a specified stock. + * + * @param array $skus + * @param int $stockId + * @return array + */ + public function execute(array $skus, int $stockId): array + { + $isSalableResults = []; + try { + $stockItemsData = $this->getStockItemsData->execute($skus, $stockId); + + foreach ($stockItemsData as $sku => $stockItemData) { + $isSalable = (bool)($stockItemData[GetStockItemsDataInterface::IS_SALABLE] ?? false); + $isSalableResults[$sku] = $isSalable; + } + } catch (LocalizedException $exception) { + $this->logger->warning( + sprintf( + 'Unable to fetch stock #%s data for SKUs %s. Reason: %s', + $stockId, + implode(', ', $skus), + $exception->getMessage() + ) + ); + // Set all SKUs as not salable if an exception occurs + foreach ($skus as $sku) { + $isSalableResults[$sku] = false; + } + } + + return $isSalableResults; + } +} diff --git a/InventoryIndexer/Model/ResourceModel/GetStockItemData.php b/InventoryIndexer/Model/ResourceModel/GetStockItemData.php index a31a63d09fc3..686e556aa2d0 100644 --- a/InventoryIndexer/Model/ResourceModel/GetStockItemData.php +++ b/InventoryIndexer/Model/ResourceModel/GetStockItemData.php @@ -18,7 +18,6 @@ use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface; use Magento\InventorySalesApi\Model\GetStockItemDataInterface; - /** * @inheritdoc */ @@ -45,14 +44,9 @@ class GetStockItemData implements GetStockItemDataInterface private $getProductIdsBySkus; /** - * @var IsSingleSourceModeInterface - */ - private $isSingleSourceMode; - - /** - * @var IsSourceItemManagementAllowedForSkuInterface + * @var StockItemDataHandler */ - private $isSourceItemManagementAllowedForSku; + private $stockItemDataHandler; /** * @param ResourceConnection $resource @@ -61,6 +55,8 @@ class GetStockItemData implements GetStockItemDataInterface * @param GetProductIdsBySkusInterface $getProductIdsBySkus * @param IsSingleSourceModeInterface|null $isSingleSourceMode * @param IsSourceItemManagementAllowedForSkuInterface|null $isSourceItemManagementAllowedForSku + * @param StockItemDataHandler|null $stockItemDataHandler + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function __construct( ResourceConnection $resource, @@ -68,17 +64,15 @@ public function __construct( DefaultStockProviderInterface $defaultStockProvider, GetProductIdsBySkusInterface $getProductIdsBySkus, ?IsSingleSourceModeInterface $isSingleSourceMode = null, - ?IsSourceItemManagementAllowedForSkuInterface $isSourceItemManagementAllowedForSku = null + ?IsSourceItemManagementAllowedForSkuInterface $isSourceItemManagementAllowedForSku = null, + ?StockItemDataHandler $stockItemDataHandler = null ) { $this->resource = $resource; $this->stockIndexTableNameResolver = $stockIndexTableNameResolver; $this->defaultStockProvider = $defaultStockProvider; $this->getProductIdsBySkus = $getProductIdsBySkus; - $this->isSingleSourceMode = $isSingleSourceMode - ?: ObjectManager::getInstance()->get(IsSingleSourceModeInterface::class); - $this->isSourceItemManagementAllowedForSku = $isSourceItemManagementAllowedForSku - ?: ObjectManager::getInstance()->get(IsSourceItemManagementAllowedForSkuInterface::class); - + $this->stockItemDataHandler = $stockItemDataHandler + ?: ObjectManager::getInstance()->get(StockItemDataHandler::class); } /** @@ -122,7 +116,7 @@ public function execute(string $sku, int $stockId): ?array * for disabled products assigned to the default stock. */ if ($stockItemRow === null) { - $stockItemRow = $this->getStockItemDataFromStockItemTable($sku, $stockId); + $stockItemRow = $this->stockItemDataHandler->getStockItemDataFromStockItemTable($sku, $stockId); } } catch (\Exception $e) { throw new LocalizedException(__('Could not receive Stock Item data'), $e); @@ -130,38 +124,4 @@ public function execute(string $sku, int $stockId): ?array return $stockItemRow; } - - /** - * Retrieve stock item data for product assigned to the default stock. - * - * @param string $sku - * @param int $stockId - * @return array|null - */ - private function getStockItemDataFromStockItemTable(string $sku, int $stockId): ?array - { - if ($this->defaultStockProvider->getId() !== $stockId - || $this->isSingleSourceMode->execute() - || !$this->isSourceItemManagementAllowedForSku->execute($sku) - ) { - return null; - } - - $productId = current($this->getProductIdsBySkus->execute([$sku])); - $connection = $this->resource->getConnection(); - $select = $connection->select(); - - $select->from( - $this->resource->getTableName('cataloginventory_stock_item'), - [ - GetStockItemDataInterface::QUANTITY => 'qty', - GetStockItemDataInterface::IS_SALABLE => 'is_in_stock', - ] - )->where( - 'product_id = ?', - $productId - ); - - return $connection->fetchRow($select) ?: null; - } } diff --git a/InventoryIndexer/Model/ResourceModel/GetStockItemsData.php b/InventoryIndexer/Model/ResourceModel/GetStockItemsData.php new file mode 100644 index 000000000000..970c74a1ac4e --- /dev/null +++ b/InventoryIndexer/Model/ResourceModel/GetStockItemsData.php @@ -0,0 +1,129 @@ +resource = $resource; + $this->stockIndexTableNameResolver = $stockIndexTableNameResolver; + $this->defaultStockProvider = $defaultStockProvider; + $this->stockItemDataHandler = $stockItemDataHandler; + } + + /** + * @inheritdoc + */ + public function execute(array $skus, int $stockId): array + { + $connection = $this->resource->getConnection(); + $select = $connection->select(); + $results = []; + + if ($this->defaultStockProvider->getId() === $stockId) { + $select->from( + ['stock_status' => $this->resource->getTableName('cataloginventory_stock_status')], + [ + GetStockItemDataInterface::SKU => 'product_entity.sku', + GetStockItemDataInterface::QUANTITY => 'stock_status.qty', + GetStockItemDataInterface::IS_SALABLE => 'stock_status.stock_status', + ] + )->join( + ['product_entity' => $this->resource->getTableName('catalog_product_entity')], + 'stock_status.product_id = product_entity.entity_id', + [] + )->where( + 'product_entity.sku IN (?)', + $skus + ); + } else { + $select->from( + $this->stockIndexTableNameResolver->execute($stockId), + [ + GetStockItemsDataInterface::SKU => IndexStructure::SKU, + GetStockItemsDataInterface::QUANTITY => IndexStructure::QUANTITY, + GetStockItemsDataInterface::IS_SALABLE => IndexStructure::IS_SALABLE, + ] + )->where( + IndexStructure::SKU . ' IN (?)', + $skus + ); + } + + try { + $stockItemRows = $connection->fetchAll($select) ?: []; + + if (!empty($stockItemRows)) { + foreach ($stockItemRows as $row) { + $results[$row['sku']] = [ + GetStockItemsDataInterface::QUANTITY => $row['quantity'], + GetStockItemsDataInterface::IS_SALABLE => $row['is_salable'], + ]; + } + } else { + /** + * Fallback to the legacy cataloginventory_stock_item table. + * Caused by data absence in legacy cataloginventory_stock_status table + * for disabled products assigned to the default stock. + */ + foreach ($skus as $sku) { + if (!isset($results[$sku])) { + $fallbackRow = $this->stockItemDataHandler->getStockItemDataFromStockItemTable($sku, $stockId); + $results[$sku] = $fallbackRow ?: null; + } + } + } + } catch (\Exception $e) { + throw new LocalizedException(__('Could not receive Stock Item data'), $e); + } + + return $results; + } +} diff --git a/InventoryIndexer/Model/ResourceModel/GetStockItemsDataCache.php b/InventoryIndexer/Model/ResourceModel/GetStockItemsDataCache.php new file mode 100644 index 000000000000..cd6c2c83d5af --- /dev/null +++ b/InventoryIndexer/Model/ResourceModel/GetStockItemsDataCache.php @@ -0,0 +1,73 @@ +getStockItemsData = $getStockItemsData; + $this->cacheStorage = $cacheStorage; + } + + /** + * @inheritdoc + */ + public function execute(array $skus, int $stockId): array + { + $stockItemsData = []; + + // Get data from the cache and identify which SKUs need to be fetched + $skusToFetch = []; + foreach ($skus as $sku) { + $cachedData = $this->cacheStorage->get($stockId, (string)$sku); + if ($cachedData !== null) { + $stockItemsData[$sku] = $cachedData; + } else { + $skusToFetch[] = $sku; + } + } + + // Fetch the data for the remaining SKUs and cache it + if (!empty($skusToFetch)) { + $fetchedItemsData = $this->getStockItemsData->execute($skusToFetch, $stockId); + + foreach ($fetchedItemsData as $sku => $stockItemData) { + $stockItemsData[$sku] = $stockItemData; + + if ($stockItemData !== null) { + $this->cacheStorage->set($stockId, (string)$sku, $stockItemData); + } + } + } + + return $stockItemsData; + } +} diff --git a/InventoryIndexer/Model/ResourceModel/StockItemDataHandler.php b/InventoryIndexer/Model/ResourceModel/StockItemDataHandler.php new file mode 100644 index 000000000000..f796524cec02 --- /dev/null +++ b/InventoryIndexer/Model/ResourceModel/StockItemDataHandler.php @@ -0,0 +1,103 @@ +resource = $resource; + $this->defaultStockProvider = $defaultStockProvider; + $this->getProductIdsBySkus = $getProductIdsBySkus; + $this->isSingleSourceMode = $isSingleSourceMode; + $this->isSourceItemManagementAllowedForSku = $isSourceItemManagementAllowedForSku; + } + + /** + * Retrieve stock item data for product assigned to the default stock. + * + * @param string $sku + * @param int $stockId + * @return array|null + * @throws NoSuchEntityException + */ + public function getStockItemDataFromStockItemTable(string $sku, int $stockId): ?array + { + if ($this->defaultStockProvider->getId() !== $stockId + || $this->isSingleSourceMode->execute() + || !$this->isSourceItemManagementAllowedForSku->execute($sku) + ) { + return null; + } + + $productId = current($this->getProductIdsBySkus->execute([$sku])); + $connection = $this->resource->getConnection(); + $select = $connection->select(); + + $select->from( + $this->resource->getTableName('cataloginventory_stock_item'), + [ + GetStockItemDataInterface::QUANTITY => 'qty', + GetStockItemDataInterface::IS_SALABLE => 'is_in_stock', + ] + )->where( + 'product_id = ?', + $productId + ); + + return $connection->fetchRow($select) ?: null; + } +} diff --git a/InventoryIndexer/Plugin/InventorySales/AreProductsSalable.php b/InventoryIndexer/Plugin/InventorySales/AreProductsSalable.php new file mode 100644 index 000000000000..602f9eeecd00 --- /dev/null +++ b/InventoryIndexer/Plugin/InventorySales/AreProductsSalable.php @@ -0,0 +1,73 @@ +isProductSalableResultFactory = $isProductSalableResultFactory; + $this->areProductsSalable = $areProductSalable; + } + + /** + * Define if products are salable in a bulk operation instead of iterating through each sku. + * + * @param AreProductsSalableInventorySales $subject + * @param callable $proceed + * @param array|string[] $skus + * @param int $stockId + * @return array + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function aroundExecute( + AreProductsSalableInventorySales $subject, + callable $proceed, + array $skus, + int $stockId + ): array { + $results = []; + + $salabilityResults = $this->areProductsSalable->execute($skus, $stockId); + + foreach ($salabilityResults as $sku => $isSalable) { + $results[] = $this->isProductSalableResultFactory->create( + [ + 'sku' => $sku, + 'stockId' => $stockId, + 'isSalable' => $isSalable, + ] + ); + } + + return $results; + } +} diff --git a/InventoryIndexer/Test/Model/AreMultipleProductsSalableTest.php b/InventoryIndexer/Test/Model/AreMultipleProductsSalableTest.php new file mode 100644 index 000000000000..094ebb77f63b --- /dev/null +++ b/InventoryIndexer/Test/Model/AreMultipleProductsSalableTest.php @@ -0,0 +1,90 @@ +getStockItemsDataMock = $this->createMock(GetStockItemsDataInterface::class); + $this->loggerMock = $this->createMock(LoggerInterface::class); + + $this->areMultipleProductsSalable = new AreMultipleProductsSalable( + $this->getStockItemsDataMock, + $this->loggerMock + ); + } + + public function testExecuteWithSuccessfulDataFetch() + { + $skus = ['sku1', 'sku2']; + $stockId = 123; + $stockItemsData = [ + 'sku1' => [GetStockItemsDataInterface::IS_SALABLE => true], + 'sku2' => [GetStockItemsDataInterface::IS_SALABLE => false], + ]; + + $this->getStockItemsDataMock + ->method('execute') + ->with($skus, $stockId) + ->willReturn($stockItemsData); + + $result = $this->areMultipleProductsSalable->execute($skus, $stockId); + + $this->assertEquals([ + 'sku1' => true, + 'sku2' => false, + ], $result); + } + + public function testExecuteWithException() + { + $skus = ['sku1', 'sku2']; + $stockId = 123; + $exceptionMessage = 'Error fetching stock data'; + + $this->getStockItemsDataMock + ->method('execute') + ->with($skus, $stockId) + ->willThrowException(new LocalizedException(__($exceptionMessage))); + + $this->loggerMock + ->expects($this->once()) + ->method('warning') + ->with($this->stringContains($exceptionMessage)); + + $result = $this->areMultipleProductsSalable->execute($skus, $stockId); + + $this->assertEquals([ + 'sku1' => false, + 'sku2' => false, + ], $result); + } +} diff --git a/InventoryIndexer/Test/Model/ResourceModel/GetStockItemsDataCacheTest.php b/InventoryIndexer/Test/Model/ResourceModel/GetStockItemsDataCacheTest.php new file mode 100644 index 000000000000..0dfabb7dd74f --- /dev/null +++ b/InventoryIndexer/Test/Model/ResourceModel/GetStockItemsDataCacheTest.php @@ -0,0 +1,83 @@ +getStockItemsDataMock = $this->createMock(GetStockItemsData::class); + $this->cacheStorageMock = $this->createMock(CacheStorage::class); + + $this->getStockItemsDataCache = new GetStockItemsDataCache( + $this->getStockItemsDataMock, + $this->cacheStorageMock + ); + } + + public function testExecute() + { + $skus = ['sku1', 'sku2']; + $stockId = 123; + $cachedData = ['cachedData']; + $fetchedData = ['fetchedData']; + + // Setup cache storage to return cached data for sku1 + $this->cacheStorageMock + ->method('get') + ->willReturnCallback(function ($requestedStockId, $requestedSku) use ($stockId, $cachedData) { + if ($requestedSku == 'sku1' && $requestedStockId == $stockId) { + return $cachedData; + } + return null; + }); + + // Setup GetStockItemsData to return fetched data for sku2 + $this->getStockItemsDataMock + ->expects($this->once()) + ->method('execute') + ->with($this->equalTo(['sku2']), $this->equalTo($stockId)) + ->willReturn(['sku2' => $fetchedData]); + + // Expect cache storage to set fetched data for sku2 + $this->cacheStorageMock + ->expects($this->once()) + ->method('set') + ->with($this->equalTo($stockId), $this->equalTo('sku2'), $this->equalTo($fetchedData)); + + $result = $this->getStockItemsDataCache->execute($skus, $stockId); + + // Assertions + $this->assertArrayHasKey('sku1', $result); + $this->assertEquals($cachedData, $result['sku1']); + + $this->assertArrayHasKey('sku2', $result); + $this->assertEquals($fetchedData, $result['sku2']); + } +} diff --git a/InventoryIndexer/Test/Model/ResourceModel/StockItemDataHandlerTest.php b/InventoryIndexer/Test/Model/ResourceModel/StockItemDataHandlerTest.php new file mode 100644 index 000000000000..9816a09cd4a5 --- /dev/null +++ b/InventoryIndexer/Test/Model/ResourceModel/StockItemDataHandlerTest.php @@ -0,0 +1,100 @@ +resourceMock = $this->createMock(ResourceConnection::class); + $this->defaultStockProviderMock = $this->createMock(DefaultStockProviderInterface::class); + $this->getProductIdsBySkusMock = $this->createMock(GetProductIdsBySkusInterface::class); + $this->isSingleSourceModeMock = $this->createMock(IsSingleSourceModeInterface::class); + $this->isSourceItemManagementAllowedForSkuMock = + $this->createMock(IsSourceItemManagementAllowedForSkuInterface::class); + + $this->stockItemDataHandler = new StockItemDataHandler( + $this->resourceMock, + $this->defaultStockProviderMock, + $this->getProductIdsBySkusMock, + $this->isSingleSourceModeMock, + $this->isSourceItemManagementAllowedForSkuMock + ); + } + + public function testGetStockItemDataReturnsNull() + { + // Set up the conditions to return null + $this->defaultStockProviderMock->method('getId')->willReturn(1); + $this->isSingleSourceModeMock->method('execute')->willReturn(true); // Single source mode is on + + $result = $this->stockItemDataHandler->getStockItemDataFromStockItemTable('sku', 1); + $this->assertNull($result); + } + + public function testGetStockItemDataFromDatabase() + { + $this->defaultStockProviderMock->method('getId')->willReturn(2); + $this->isSingleSourceModeMock->method('execute')->willReturn(false); + $this->isSourceItemManagementAllowedForSkuMock->method('execute')->willReturn(true); + $this->getProductIdsBySkusMock->method('execute')->willReturn(['productId']); + + $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class); + $this->resourceMock->method('getConnection')->willReturn($connectionMock); + + $selectMock = $this->createMock(\Magento\Framework\DB\Select::class); + $connectionMock->method('select')->willReturn($selectMock); + $selectMock->method('from')->willReturnSelf(); + $selectMock->method('where')->willReturnSelf(); + + $expectedResult = ['qty' => 100, 'is_in_stock' => 1]; + $connectionMock->method('fetchRow')->willReturn($expectedResult); + + $result = $this->stockItemDataHandler->getStockItemDataFromStockItemTable('sku', 2); + $this->assertEquals($expectedResult, $result); + } +} diff --git a/InventoryIndexer/etc/di.xml b/InventoryIndexer/etc/di.xml index f5e9f061b97e..9866fcca2186 100644 --- a/InventoryIndexer/etc/di.xml +++ b/InventoryIndexer/etc/di.xml @@ -81,6 +81,7 @@ + diff --git a/InventoryIndexer/etc/frontend/di.xml b/InventoryIndexer/etc/frontend/di.xml index 2e3ac3d91f21..76e28b8b23f4 100644 --- a/InventoryIndexer/etc/frontend/di.xml +++ b/InventoryIndexer/etc/frontend/di.xml @@ -8,4 +8,8 @@ + + + + diff --git a/InventoryIndexer/etc/graphql/di.xml b/InventoryIndexer/etc/graphql/di.xml index 2e3ac3d91f21..76e28b8b23f4 100644 --- a/InventoryIndexer/etc/graphql/di.xml +++ b/InventoryIndexer/etc/graphql/di.xml @@ -8,4 +8,8 @@ + + + + diff --git a/InventorySalesApi/Model/GetStockItemsDataInterface.php b/InventorySalesApi/Model/GetStockItemsDataInterface.php new file mode 100644 index 000000000000..c3d131d3b587 --- /dev/null +++ b/InventorySalesApi/Model/GetStockItemsDataInterface.php @@ -0,0 +1,36 @@ + + diff --git a/dev/tests/integration/_files/Magento/TestModuleInventoryStateCache/etc/frontend/di.xml b/dev/tests/integration/_files/Magento/TestModuleInventoryStateCache/etc/frontend/di.xml index 30c34c0c4002..10b4014613f1 100644 --- a/dev/tests/integration/_files/Magento/TestModuleInventoryStateCache/etc/frontend/di.xml +++ b/dev/tests/integration/_files/Magento/TestModuleInventoryStateCache/etc/frontend/di.xml @@ -7,6 +7,7 @@ --> +