diff --git a/app/code/Magento/CatalogImportExport/Model/Import/Product.php b/app/code/Magento/CatalogImportExport/Model/Import/Product.php index 57498355c51a6..2005eb49493e2 100644 --- a/app/code/Magento/CatalogImportExport/Model/Import/Product.php +++ b/app/code/Magento/CatalogImportExport/Model/Import/Product.php @@ -1963,7 +1963,7 @@ protected function _saveStockItem() $productIdsToReindex[] = $row['product_id']; $row['website_id'] = $this->stockConfiguration->getDefaultScopeId(); - $row['stock_id'] = $this->stockRegistry->getStock()->getStockId(); + $row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId(); $stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']); $existStockData = $stockItemDo->getData(); diff --git a/app/code/Magento/CatalogInventory/Api/StockRegistryInterface.php b/app/code/Magento/CatalogInventory/Api/StockRegistryInterface.php index 27b45dc677163..2566aff49b0dc 100644 --- a/app/code/Magento/CatalogInventory/Api/StockRegistryInterface.php +++ b/app/code/Magento/CatalogInventory/Api/StockRegistryInterface.php @@ -12,10 +12,10 @@ interface StockRegistryInterface { /** - * @param int|null $stockId + * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockInterface */ - public function getStock($stockId = null); + public function getStock($scopeId = null); /** * @param int $productId diff --git a/app/code/Magento/CatalogInventory/Api/StockStatusCriteriaInterface.php b/app/code/Magento/CatalogInventory/Api/StockStatusCriteriaInterface.php index 44d3c1be11fab..bb3d101e97f9f 100644 --- a/app/code/Magento/CatalogInventory/Api/StockStatusCriteriaInterface.php +++ b/app/code/Magento/CatalogInventory/Api/StockStatusCriteriaInterface.php @@ -15,7 +15,7 @@ interface StockStatusCriteriaInterface extends \Magento\Framework\Api\CriteriaIn * Add Criteria object * * @param \Magento\CatalogInventory\Api\StockStatusCriteriaInterface $criteria - * @return void + * @return bool */ public function addCriteria(\Magento\CatalogInventory\Api\StockStatusCriteriaInterface $criteria); @@ -23,7 +23,7 @@ public function addCriteria(\Magento\CatalogInventory\Api\StockStatusCriteriaInt * Filter by scope(s) * * @param int $scope - * @return void + * @return bool */ public function setScopeFilter($scope); @@ -31,7 +31,7 @@ public function setScopeFilter($scope); * Add product(s) filter * * @param int $products - * @return void + * @return bool */ public function setProductsFilter($products); @@ -39,7 +39,7 @@ public function setProductsFilter($products); * Add filter by quantity * * @param float $qty - * @return void + * @return bool */ public function setQtyFilter($qty); } diff --git a/app/code/Magento/CatalogInventory/Helper/Stock.php b/app/code/Magento/CatalogInventory/Helper/Stock.php index 4706a60bf5250..0474515b954b7 100644 --- a/app/code/Magento/CatalogInventory/Helper/Stock.php +++ b/app/code/Magento/CatalogInventory/Helper/Stock.php @@ -6,7 +6,6 @@ namespace Magento\CatalogInventory\Helper; use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface; -use Magento\CatalogInventory\Model\Spi\StockResolverInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory; @@ -48,30 +47,22 @@ class Stock */ private $stockRegistryProvider; - /** - * @var StockResolverInterface - */ - protected $stockResolver; - /** * @param StoreManagerInterface $storeManager * @param ScopeConfigInterface $scopeConfig * @param StatusFactory $stockStatusFactory * @param StockRegistryProviderInterface $stockRegistryProvider - * @param StockResolverInterface $stockResolver */ public function __construct( StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, StatusFactory $stockStatusFactory, - StockRegistryProviderInterface $stockRegistryProvider, - StockResolverInterface $stockResolver + StockRegistryProviderInterface $stockRegistryProvider ) { $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; $this->stockStatusFactory = $stockStatusFactory; $this->stockRegistryProvider = $stockRegistryProvider; - $this->stockResolver = $stockResolver; } /** @@ -84,9 +75,8 @@ public function __construct( public function assignStatusToProduct(Product $product, $stockStatus = null) { if ($stockStatus === null) { - $productId = $product->getId(); - $stockId = $this->stockResolver->getStockId($productId, $product->getStore()->getWebsiteId()); - $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $stockId); + $websiteId = $product->getStore()->getWebsiteId(); + $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $websiteId); $status = $stockStatus->getStockStatus(); } $product->setIsSalable($status); @@ -103,8 +93,7 @@ public function addStockStatusToProducts(AbstractCollection $productCollection) $websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId(); foreach ($productCollection as $product) { $productId = $product->getId(); - $stockId = $this->stockResolver->getStockId($productId, $websiteId); - $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $stockId); + $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $websiteId); $status = $stockStatus->getStockStatus(); $product->setIsSalable($status); } diff --git a/app/code/Magento/CatalogInventory/Model/Spi/StockRegistryProviderInterface.php b/app/code/Magento/CatalogInventory/Model/Spi/StockRegistryProviderInterface.php index a08aa33533301..8fb68a2c997be 100644 --- a/app/code/Magento/CatalogInventory/Model/Spi/StockRegistryProviderInterface.php +++ b/app/code/Magento/CatalogInventory/Model/Spi/StockRegistryProviderInterface.php @@ -11,22 +11,22 @@ interface StockRegistryProviderInterface { /** - * @param int|null $stockId + * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockInterface */ - public function getStock($stockId); + public function getStock($scopeId); /** * @param int $productId - * @param int $stockId + * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockItemInterface */ - public function getStockItem($productId, $stockId); + public function getStockItem($productId, $scopeId); /** * @param int $productId - * @param int $stockId + * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockStatusInterface */ - public function getStockStatus($productId, $stockId); + public function getStockStatus($productId, $scopeId); } diff --git a/app/code/Magento/CatalogInventory/Model/Spi/StockResolverInterface.php b/app/code/Magento/CatalogInventory/Model/Spi/StockResolverInterface.php deleted file mode 100644 index b4f9df8f079da..0000000000000 --- a/app/code/Magento/CatalogInventory/Model/Spi/StockResolverInterface.php +++ /dev/null @@ -1,19 +0,0 @@ -getData(static::STOCK_ID); if ($stockId === null) { - $stockId = $this->stockRegistry->getStock()->getStockId(); + $stockId = $this->stockRegistry->getStock($this->getWebsiteId())->getStockId(); } return (int) $stockId; } diff --git a/app/code/Magento/CatalogInventory/Model/StockIndex.php b/app/code/Magento/CatalogInventory/Model/StockIndex.php index d961f52ff519f..4c6f6521c9794 100644 --- a/app/code/Magento/CatalogInventory/Model/StockIndex.php +++ b/app/code/Magento/CatalogInventory/Model/StockIndex.php @@ -13,7 +13,6 @@ use Magento\Catalog\Model\ProductFactory; use Magento\CatalogInventory\Api\StockIndexInterface; use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface; -use Magento\CatalogInventory\Model\Spi\StockResolverInterface; use Magento\Catalog\Api\ProductRepositoryInterface; /** @@ -56,31 +55,22 @@ class StockIndex implements StockIndexInterface */ protected $productTypes = []; - - /** - * @var StockResolverInterface - */ - protected $stockResolver; - /** * @param StockRegistryProviderInterface $stockRegistryProvider * @param ProductRepositoryInterface $productRepository * @param ProductWebsite $productWebsite * @param ProductType $productType - * @param StockResolverInterface $stockResolver */ public function __construct( StockRegistryProviderInterface $stockRegistryProvider, ProductRepositoryInterface $productRepository, ProductWebsite $productWebsite, - ProductType $productType, - StockResolverInterface $stockResolver + ProductType $productType ) { $this->stockRegistryProvider = $stockRegistryProvider; $this->productRepository = $productRepository; $this->productWebsite = $productWebsite; $this->productType = $productType; - $this->stockResolver = $stockResolver; } /** @@ -122,8 +112,7 @@ public function rebuild($productId = null, $scopeId = null) */ public function updateProductStockStatus($productId, $websiteId) { - $stockId = $this->stockResolver->getStockId($productId, $websiteId); - $item = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $item = $this->stockRegistryProvider->getStockItem($productId, $websiteId); $status = \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK; $qty = 0; @@ -242,8 +231,7 @@ protected function processParents($productId, $websiteId) } foreach ($parentIds as $parentId) { - $stockId = $this->stockResolver->getStockId($productId, $websiteId); - $item = $this->stockRegistryProvider->getStockItem($parentId, $stockId); + $item = $this->stockRegistryProvider->getStockItem($parentId, $websiteId); $status = \Magento\CatalogInventory\Model\Stock\Status::STATUS_IN_STOCK; $qty = 0; if ($item->getItemId()) { diff --git a/app/code/Magento/CatalogInventory/Model/StockManagement.php b/app/code/Magento/CatalogInventory/Model/StockManagement.php index 7b08d2a757ebc..9415c464c18c1 100644 --- a/app/code/Magento/CatalogInventory/Model/StockManagement.php +++ b/app/code/Magento/CatalogInventory/Model/StockManagement.php @@ -10,7 +10,6 @@ use Magento\CatalogInventory\Api\StockManagementInterface; use Magento\CatalogInventory\Model\ResourceModel\QtyCounterInterface; use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface; -use Magento\CatalogInventory\Model\Spi\StockResolverInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\CatalogInventory\Model\ResourceModel\Stock as ResourceStock; @@ -49,11 +48,6 @@ class StockManagement implements StockManagementInterface */ private $qtyCounter; - /** - * @var StockResolverInterface - */ - protected $stockResolver; - /** * @param ResourceStock $stockResource * @param StockRegistryProviderInterface $stockRegistryProvider @@ -61,7 +55,6 @@ class StockManagement implements StockManagementInterface * @param StockConfigurationInterface $stockConfiguration * @param ProductRepositoryInterface $productRepository * @param QtyCounterInterface $qtyCounter - * @param StockResolverInterface $stockResolver */ public function __construct( ResourceStock $stockResource, @@ -69,8 +62,7 @@ public function __construct( StockState $stockState, StockConfigurationInterface $stockConfiguration, ProductRepositoryInterface $productRepository, - QtyCounterInterface $qtyCounter, - StockResolverInterface $stockResolver + QtyCounterInterface $qtyCounter ) { $this->stockRegistryProvider = $stockRegistryProvider; $this->stockState = $stockState; @@ -78,7 +70,6 @@ public function __construct( $this->productRepository = $productRepository; $this->qtyCounter = $qtyCounter; $this->resource = $stockResource; - $this->stockResolver = $stockResolver; } /** @@ -93,9 +84,9 @@ public function __construct( */ public function registerProductsSale($items, $websiteId = null) { - if (!$websiteId) { - $websiteId = $this->stockConfiguration->getDefaultScopeId(); - } + //if (!$websiteId) { + $websiteId = $this->stockConfiguration->getDefaultScopeId(); + //} $this->getResource()->beginTransaction(); $lockedItems = $this->getResource()->lockProductsStock(array_keys($items), $websiteId); $fullSaveItems = $registeredItems = []; @@ -103,8 +94,7 @@ public function registerProductsSale($items, $websiteId = null) $productId = $lockedItemRecord['product_id']; /** @var StockItemInterface $stockItem */ $orderedQty = $items[$productId]; - $stockId = $this->stockResolver->getStockId($productId, $websiteId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId); $canSubtractQty = $stockItem->getItemId() && $this->canSubtractQty($stockItem); if (!$canSubtractQty || !$this->stockConfiguration->isQty($lockedItemRecord['type_id'])) { continue; @@ -142,9 +132,9 @@ public function registerProductsSale($items, $websiteId = null) */ public function revertProductsSale($items, $websiteId = null) { - if (!$websiteId) { - $websiteId = $this->stockConfiguration->getDefaultScopeId(); - } + //if (!$websiteId) { + $websiteId = $this->stockConfiguration->getDefaultScopeId(); + //} $this->qtyCounter->correctItemsQty($items, $websiteId, '+'); return true; } @@ -159,11 +149,10 @@ public function revertProductsSale($items, $websiteId = null) */ public function backItemQty($productId, $qty, $scopeId = null) { - if (!$scopeId) { - $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); if ($stockItem->getItemId() && $this->stockConfiguration->isQty($this->getProductType($productId))) { if ($this->canSubtractQty($stockItem)) { $stockItem->setQty($stockItem->getQty() + $qty); diff --git a/app/code/Magento/CatalogInventory/Model/StockRegistry.php b/app/code/Magento/CatalogInventory/Model/StockRegistry.php index d72719fdb3a05..fda30523c39ab 100644 --- a/app/code/Magento/CatalogInventory/Model/StockRegistry.php +++ b/app/code/Magento/CatalogInventory/Model/StockRegistry.php @@ -11,9 +11,7 @@ use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; use Magento\CatalogInventory\Api\StockItemRepositoryInterface; use Magento\CatalogInventory\Api\StockRegistryInterface; -use Magento\CatalogInventory\Model\Spi\StockResolverInterface; use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface; -use Magento\CatalogInventory\Model\Spi\StockStateProviderInterface; /** * Class StockRegistry @@ -47,49 +45,37 @@ class StockRegistry implements StockRegistryInterface */ protected $criteriaFactory; - /** - * @var \Magento\CatalogInventory\Model\Spi\StockResolverInterface - */ - protected $stockResolver; - - /** - * @var StockStateProviderInterface - */ - protected $stockStateProvider; - /** * @param StockConfigurationInterface $stockConfiguration * @param StockRegistryProviderInterface $stockRegistryProvider * @param StockItemRepositoryInterface $stockItemRepository * @param StockItemCriteriaInterfaceFactory $criteriaFactory * @param ProductFactory $productFactory - * @param StockResolverInterface $stockResolver - * @param StockStateProviderInterface $stockStateProvider */ public function __construct( StockConfigurationInterface $stockConfiguration, StockRegistryProviderInterface $stockRegistryProvider, StockItemRepositoryInterface $stockItemRepository, StockItemCriteriaInterfaceFactory $criteriaFactory, - ProductFactory $productFactory, - StockResolverInterface $stockResolver, - StockStateProviderInterface $stockStateProvider + ProductFactory $productFactory ) { $this->stockConfiguration = $stockConfiguration; $this->stockRegistryProvider = $stockRegistryProvider; $this->stockItemRepository = $stockItemRepository; $this->criteriaFactory = $criteriaFactory; $this->productFactory = $productFactory; - $this->stockResolver = $stockResolver; - $this->stockStateProvider = $stockStateProvider; } /** - * @inheritdoc + * @param int $scopeId + * @return \Magento\CatalogInventory\Api\Data\StockInterface */ - public function getStock($stockId = null) + public function getStock($scopeId = null) { - return $this->stockRegistryProvider->getStock($stockId); + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} + return $this->stockRegistryProvider->getStock($scopeId); } /** @@ -99,12 +85,10 @@ public function getStock($stockId = null) */ public function getStockItem($productId, $scopeId = null) { - if (!$scopeId) { - $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - - return $this->stockRegistryProvider->getStockItem($productId, $stockId); + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} + return $this->stockRegistryProvider->getStockItem($productId, $scopeId); } /** @@ -115,12 +99,11 @@ public function getStockItem($productId, $scopeId = null) */ public function getStockItemBySku($productSku, $scopeId = null) { - if (!$scopeId) { - $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} $productId = $this->resolveProductId($productSku); - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - return $this->stockRegistryProvider->getStockItem($productId, $stockId); + return $this->stockRegistryProvider->getStockItem($productId, $scopeId); } /** @@ -130,11 +113,10 @@ public function getStockItemBySku($productSku, $scopeId = null) */ public function getStockStatus($productId, $scopeId = null) { - if (!$scopeId) { - $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - return $this->stockRegistryProvider->getStockStatus($productId, $stockId); + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} + return $this->stockRegistryProvider->getStockStatus($productId, $scopeId); } /** @@ -145,12 +127,11 @@ public function getStockStatus($productId, $scopeId = null) */ public function getStockStatusBySku($productSku, $scopeId = null) { - if (!$scopeId) { - $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} $productId = $this->resolveProductId($productSku); - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - return $this->getStockStatus($productId, $stockId); + return $this->getStockStatus($productId, $scopeId); } /** @@ -161,11 +142,10 @@ public function getStockStatusBySku($productSku, $scopeId = null) */ public function getProductStockStatus($productId, $scopeId = null) { - if (!$scopeId) { - $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockStatus = $this->getStockStatus($productId, $stockId); + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} + $stockStatus = $this->getStockStatus($productId, $scopeId); return $stockStatus->getStockStatus(); } @@ -177,12 +157,11 @@ public function getProductStockStatus($productId, $scopeId = null) */ public function getProductStockStatusBySku($productSku, $scopeId = null) { - if (!$scopeId) { - $scopeId = $this->stockConfiguration->getDefaultScopeId(); - } + //if (!$scopeId) { + $scopeId = $this->stockConfiguration->getDefaultScopeId(); + //} $productId = $this->resolveProductId($productSku); - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - return $this->getProductStockStatus($productId, $stockId); + return $this->getProductStockStatus($productId, $scopeId); } /** diff --git a/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php b/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php index 0e103e2cddd14..e8f7ae5cc723f 100644 --- a/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php +++ b/app/code/Magento/CatalogInventory/Model/StockRegistryProvider.php @@ -15,13 +15,11 @@ use Magento\CatalogInventory\Api\StockCriteriaInterfaceFactory; use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory; -use Magento\CatalogInventory\Api\StockConfigurationInterface; use Magento\Store\Model\StoreManagerInterface; /** * Class StockRegistryProvider * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ class StockRegistryProvider implements StockRegistryProviderInterface { @@ -70,11 +68,6 @@ class StockRegistryProvider implements StockRegistryProviderInterface */ protected $stockStatusCriteriaFactory; - /** - * @var StockConfigurationInterface - */ - protected $stockConfiguration; - /** * @var array */ @@ -100,7 +93,6 @@ class StockRegistryProvider implements StockRegistryProviderInterface * @param StockCriteriaInterfaceFactory $stockCriteriaFactory * @param StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory * @param StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory - * @param StockConfigurationInterface $stockConfiguration */ public function __construct( StockRepositoryInterface $stockRepository, @@ -111,8 +103,7 @@ public function __construct( StockStatusInterfaceFactory $stockStatusFactory, StockCriteriaInterfaceFactory $stockCriteriaFactory, StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory, - StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory, - StockConfigurationInterface $stockConfiguration + StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory ) { $this->stockRepository = $stockRepository; $this->stockFactory = $stockFactory; @@ -120,49 +111,43 @@ public function __construct( $this->stockItemFactory = $stockItemFactory; $this->stockStatusRepository = $stockStatusRepository; $this->stockStatusFactory = $stockStatusFactory; + $this->stockCriteriaFactory = $stockCriteriaFactory; $this->stockItemCriteriaFactory = $stockItemCriteriaFactory; $this->stockStatusCriteriaFactory = $stockStatusCriteriaFactory; - $this->stockConfiguration = $stockConfiguration; } /** - * @inheritdoc + * @param int|null $scopeId + * @return \Magento\CatalogInventory\Api\Data\StockInterface */ - public function getStock($stockId) + public function getStock($scopeId) { - if (!isset($this->stocks[$stockId])) { - if ($stockId !== null) { - $stock = $this->stockRepository->get($stockId); - } else { - /** @var \Magento\CatalogInventory\Api\StockCriteriaInterface $criteria */ - $criteria = $this->stockCriteriaFactory->create(); - $criteria->setScopeFilter($this->stockConfiguration->getDefaultScopeId()); - $collection = $this->stockRepository->getList($criteria); - $stock = current($collection->getItems()); - } + if (!isset($this->stocks[$scopeId])) { + $criteria = $this->stockCriteriaFactory->create(); + $criteria->setScopeFilter($scopeId); + $collection = $this->stockRepository->getList($criteria); + $stock = current($collection->getItems()); if ($stock && $stock->getStockId()) { - $this->stocks[$stockId] = $stock; + $this->stocks[$scopeId] = $stock; } else { return $this->stockFactory->create(); } } - return $this->stocks[$stockId]; + return $this->stocks[$scopeId]; } /** * @param int $productId - * @param int $stockId + * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockItemInterface */ - public function getStockItem($productId, $stockId) + public function getStockItem($productId, $scopeId) { - $key = $stockId . '/' . $productId; + $key = $scopeId . '/' . $productId; if (!isset($this->stockItems[$key])) { - /** @var \Magento\CatalogInventory\Api\StockItemCriteriaInterface $criteria */ $criteria = $this->stockItemCriteriaFactory->create(); $criteria->setProductsFilter($productId); - $criteria->setStockFilter($this->getStock($stockId)); $collection = $this->stockItemRepository->getList($criteria); $stockItem = current($collection->getItems()); if ($stockItem && $stockItem->getItemId()) { @@ -176,17 +161,16 @@ public function getStockItem($productId, $stockId) /** * @param int $productId - * @param int $stockId + * @param int $scopeId * @return \Magento\CatalogInventory\Api\Data\StockStatusInterface */ - public function getStockStatus($productId, $stockId) + public function getStockStatus($productId, $scopeId) { - $key = $stockId . '/' . $productId; + $key = $scopeId . '/' . $productId; if (!isset($this->stockStatuses[$key])) { - /** @var \Magento\CatalogInventory\Api\stockStatusCriteriaInterface $criteria */ $criteria = $this->stockStatusCriteriaFactory->create(); $criteria->setProductsFilter($productId); - $criteria->addFilter('stock', 'stock_id', $stockId); + $criteria->setScopeFilter($scopeId); $collection = $this->stockStatusRepository->getList($criteria); $stockStatus = current($collection->getItems()); if ($stockStatus && $stockStatus->getProductId()) { diff --git a/app/code/Magento/CatalogInventory/Model/StockResolver.php b/app/code/Magento/CatalogInventory/Model/StockResolver.php deleted file mode 100644 index 8e7c096d37047..0000000000000 --- a/app/code/Magento/CatalogInventory/Model/StockResolver.php +++ /dev/null @@ -1,25 +0,0 @@ -stockStateProvider = $stockStateProvider; $this->stockRegistryProvider = $stockRegistryProvider; $this->stockConfiguration = $stockConfiguration; - $this->stockResolver = $stockResolver; } /** @@ -64,8 +55,7 @@ public function verifyStock($productId, $scopeId = null) if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->verifyStock($stockItem); } @@ -79,8 +69,7 @@ public function verifyNotification($productId, $scopeId = null) if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->verifyNotification($stockItem); } @@ -98,8 +87,7 @@ public function checkQty($productId, $qty, $scopeId = null) if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->checkQty($stockItem, $qty); } @@ -117,8 +105,7 @@ public function suggestQty($productId, $qty, $scopeId = null) if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->suggestQty($stockItem, $qty); } @@ -134,8 +121,7 @@ public function getStockQty($productId, $scopeId = null) if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->getStockQty($stockItem); } @@ -150,8 +136,7 @@ public function checkQtyIncrements($productId, $qty, $websiteId = null) if ($websiteId === null) { $websiteId = $this->stockConfiguration->getDefaultScopeId(); } - $stockId = $this->stockResolver->getStockId($productId, $websiteId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $websiteId); return $this->stockStateProvider->checkQtyIncrements($stockItem, $qty); } @@ -168,8 +153,7 @@ public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $ if ($scopeId === null) { $scopeId = $this->stockConfiguration->getDefaultScopeId(); } - $stockId = $this->stockResolver->getStockId($productId, $scopeId); - $stockItem = $this->stockRegistryProvider->getStockItem($productId, $stockId); + $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId); return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty); } } diff --git a/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php b/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php index 84c6e31dbac45..08bf961fb7278 100644 --- a/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php +++ b/app/code/Magento/CatalogInventory/Observer/SaveInventoryDataObserver.php @@ -11,7 +11,6 @@ use Magento\CatalogInventory\Api\StockIndexInterface; use Magento\CatalogInventory\Api\StockItemRepositoryInterface; use Magento\CatalogInventory\Api\StockRegistryInterface; -use Magento\CatalogInventory\Model\Spi\StockResolverInterface; use Magento\Framework\Event\Observer as EventObserver; class SaveInventoryDataObserver implements ObserverInterface @@ -36,11 +35,6 @@ class SaveInventoryDataObserver implements ObserverInterface */ protected $stockItemRepository; - /** - * @var StockResolverInterface - */ - protected $stockResolver; - /** * @var array */ @@ -80,20 +74,17 @@ class SaveInventoryDataObserver implements ObserverInterface * @param StockConfigurationInterface $stockConfiguration * @param StockRegistryInterface $stockRegistry * @param StockItemRepositoryInterface $stockItemRepository - * @param StockResolverInterface $stockResolver */ public function __construct( StockIndexInterface $stockIndex, StockConfigurationInterface $stockConfiguration, StockRegistryInterface $stockRegistry, - StockItemRepositoryInterface $stockItemRepository, - StockResolverInterface $stockResolver + StockItemRepositoryInterface $stockItemRepository ) { $this->stockIndex = $stockIndex; $this->stockConfiguration = $stockConfiguration; $this->stockRegistry = $stockRegistry; $this->stockItemRepository = $stockItemRepository; - $this->stockResolver = $stockResolver; } /** @@ -134,8 +125,7 @@ protected function saveStockItemData($product) if (!isset($stockItemData['website_id'])) { $stockItemData['website_id'] = $this->stockConfiguration->getDefaultScopeId(); } - $stockItemData['stock_id'] = $this->stockResolver - ->getStockId($stockItemData['product_id'], $stockItemData['website_id']); + $stockItemData['stock_id'] = $this->stockRegistry->getStock($stockItemData['website_id'])->getStockId(); foreach ($this->paramListToCheck as $dataKey => $configPath) { if (null !== $product->getData($configPath['item']) && null === $product->getData($configPath['config'])) { diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Api/StockRegistryTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Api/StockRegistryTest.php index 435defef554e5..02c631727a4b5 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Api/StockRegistryTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Api/StockRegistryTest.php @@ -58,7 +58,6 @@ class StockRegistryTest extends \PHPUnit_Framework_TestCase protected $product; protected $productId = 111; - protected $stockId = 112; protected $productSku = 'simple'; protected $websiteId = 111; @@ -136,7 +135,7 @@ protected function tearDown() public function testGetStock() { - $this->assertEquals($this->stock, $this->stockRegistry->getStock($this->stockId)); + $this->assertEquals($this->stock, $this->stockRegistry->getStock($this->websiteId)); } public function testGetStockItem() diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php index 65173103377f4..cb4209de616e7 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Helper/StockTest.php @@ -37,11 +37,6 @@ class StockTest extends \PHPUnit_Framework_TestCase */ protected $statusFactoryMock; - /** - * @var \Magento\CatalogInventory\Model\Spi\StockResolverInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $stockResolverMock; - protected function setUp() { $this->stockRegistryProviderMock = $this->getMockBuilder( @@ -60,26 +55,18 @@ protected function setUp() ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); - $this->stockResolverMock = - $this->getMockBuilder('Magento\CatalogInventory\Model\Spi\StockResolverInterface') - ->disableOriginalConstructor() - ->setMethods(['getStockId']) - ->getMock(); $this->stock = new Stock( $this->storeManagerMock, $this->scopeConfigMock, $this->statusFactoryMock, - $this->stockRegistryProviderMock, - $this->stockResolverMock + $this->stockRegistryProviderMock ); } public function testAssignStatusToProduct() { - $websiteId = 0; - $productId = 2; - $stockId = 3; - $status = 1; + $websiteId = 1; + $status = 'test'; $stockStatusMock = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockStatusInterface') ->disableOriginalConstructor() @@ -106,23 +93,15 @@ public function testAssignStatusToProduct() $productMock->expects($this->once()) ->method('setIsSalable') ->with($status); - $productMock->expects($this->once()) - ->method('getId') - ->willReturn($productId); - $this->stockResolverMock->expects($this->once()) - ->method('getStockId') - ->with($productId, $websiteId) - ->willReturn($stockId); $this->assertNull($this->stock->assignStatusToProduct($productMock)); } public function testAddStockStatusToProducts() { $storeId = 1; - $websiteId = 0; $productId = 2; - $stockId = 3; - $status = 1; + $status = 'test'; + $productMock = $this->getMockBuilder('Magento\Catalog\Model\Product') ->disableOriginalConstructor() ->setMethods(['setIsSalable', 'getId']) @@ -158,21 +137,14 @@ public function testAddStockStatusToProducts() $storeMock = $this->getMockBuilder('Magento\Store\Model\Store') ->disableOriginalConstructor() ->getMock(); - $storeMock->expects($this->once()) - ->method('getWebsiteId') - ->willReturn($websiteId); $this->storeManagerMock->expects($this->once()) ->method('getStore') - ->with($storeId) ->willReturn($storeMock); $this->stockRegistryProviderMock->expects($this->once()) ->method('getStockStatus') ->withAnyParameters() ->willReturn($stockStatusMock); - $this->stockResolverMock->expects($this->once()) - ->method('getStockId') - ->with($productId, $websiteId) - ->willReturn($stockId); + $this->assertNull($this->stock->addStockStatusToProducts($productCollectionMock)); } diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php index 5cf08ca76f3a3..8328208440b80 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Spi/StockRegistryProviderTest.php @@ -9,20 +9,45 @@ /** * Class StockRegistryProviderTest + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.TooManyFields) */ class StockRegistryProviderTest extends \PHPUnit_Framework_TestCase { + /** @var ObjectManagerHelper */ + protected $objectManagerHelper; + /** * @var \Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $stockRegistryProvider; + /** + * @var \Magento\CatalogInventory\Api\Data\StockInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stock; + + /** + * @var \Magento\CatalogInventory\Api\Data\StockItemInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stockItem; + /** * @var \Magento\CatalogInventory\Api\Data\StockStatusInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $stockStatus; + /** + * @var \Magento\CatalogInventory\Api\Data\StockStatusInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stockStatusFactory; + + /** + * @var \Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stockItemFactory; + /** * @var \Magento\CatalogInventory\Api\Data\StockInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject */ @@ -58,6 +83,11 @@ class StockRegistryProviderTest extends \PHPUnit_Framework_TestCase */ protected $stockStatusCriteriaFactory; + /** + * @var \Magento\CatalogInventory\Api\StockCriteriaInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stockCriteria; + /** * @var \Magento\CatalogInventory\Api\StockItemCriteriaInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -68,88 +98,132 @@ class StockRegistryProviderTest extends \PHPUnit_Framework_TestCase */ protected $stockStatusCriteria; - /** - * @var \Magento\CatalogInventory\Api\StockConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $stockConfiguration; + protected $productId = 111; + protected $productSku = 'simple'; + protected $scopeId = 111; /** - * @var array + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - protected $productData = [ - 'stock_id' => 111, - 'product_id' => 112, - 'product_sku' => 'simple', - 'scope_id' => 113 - ]; - protected function setUp() { - $objectManagerHelper = new ObjectManagerHelper($this); + $this->objectManagerHelper = new ObjectManagerHelper($this); - $this->stockFactory = $this->getMockBuilder('\Magento\CatalogInventory\Api\Data\StockInterfaceFactory') - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $stockItemFactory = $this->getMockBuilder('\Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory') - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $stockStatusFactory = $this->getMockBuilder( - '\Magento\CatalogInventory\Api\Data\StockStatusInterfaceFactory' - ) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $stockStatusFactory->expects($this->any())->method('create')->willReturn($this->stockStatus); - $this->stockCriteriaFactory = $this->getMockBuilder( - 'Magento\CatalogInventory\Api\StockCriteriaInterfaceFactory' - ) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $this->stockItemCriteriaFactory = $this->getMockBuilder( - 'Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory' - ) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - $this->stockStatusCriteriaFactory = $this->getMockBuilder( - 'Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory' - ) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); + $this->stock = $this->getMockForAbstractClass( + 'Magento\CatalogInventory\Api\Data\StockInterface', + ['__wakeup', 'getStockId'], + '', + false + ); + $this->stockItem = $this->getMockForAbstractClass( + 'Magento\CatalogInventory\Api\Data\StockItemInterface', + ['__wakeup', 'getItemId'], + '', + false + ); + $this->stockStatus = $this->getMockForAbstractClass( + 'Magento\CatalogInventory\Api\Data\StockStatusInterface', + ['__wakeup', 'getProductId'], + '', + false + ); + + $this->stockFactory = $this->getMock( + '\Magento\CatalogInventory\Api\Data\StockInterfaceFactory', + ['create'], + [], + '', + false + ); + $this->stockFactory->expects($this->any())->method('create')->willReturn($this->stock); + + $this->stockItemFactory = $this->getMock( + '\Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory', + ['create'], + [], + '', + false + ); + $this->stockItemFactory->expects($this->any())->method('create')->willReturn($this->stockItem); + + $this->stockStatusFactory = $this->getMock( + '\Magento\CatalogInventory\Api\Data\StockStatusInterfaceFactory', + ['create'], + [], + '', + false + ); + $this->stockStatusFactory->expects($this->any())->method('create')->willReturn($this->stockStatus); $this->stockRepository = $this->getMockBuilder('\Magento\CatalogInventory\Api\StockRepositoryInterface') ->disableOriginalConstructor() ->getMock(); + $this->stockItemRepository = $this->getMockBuilder('\Magento\CatalogInventory\Api\StockItemRepositoryInterface') ->disableOriginalConstructor() ->getMock(); + $this->stockStatusRepository = $this->getMockBuilder( '\Magento\CatalogInventory\Api\StockStatusRepositoryInterface' ) ->disableOriginalConstructor() ->getMock(); - $this->stockConfiguration = $this->getMockBuilder('Magento\CatalogInventory\Api\StockConfigurationInterface') - ->disableOriginalConstructor() - ->setMethods(['getDefaultScopeId']) - ->getMockForAbstractClass(); - $this->stockRegistryProvider = $objectManagerHelper->getObject( + $this->stockCriteriaFactory = $this->getMock( + 'Magento\CatalogInventory\Api\StockCriteriaInterfaceFactory', + ['create'], + [], + '', + false + ); + $this->stockCriteria = $this->getMockForAbstractClass( + 'Magento\CatalogInventory\Api\StockCriteriaInterface', + ['setScopeFilter'], + '', + false + ); + + $this->stockItemCriteriaFactory = $this->getMock( + 'Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory', + ['create'], + [], + '', + false + ); + $this->stockItemCriteria = $this->getMockForAbstractClass( + 'Magento\CatalogInventory\Api\StockItemCriteriaInterface', + ['setProductsFilter', 'setScopeFilter'], + '', + false + ); + + $this->stockStatusCriteriaFactory = $this->getMock( + 'Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory', + ['create'], + [], + '', + false + ); + $this->stockStatusCriteria = $this->getMockForAbstractClass( + 'Magento\CatalogInventory\Api\StockStatusCriteriaInterface', + ['setProductsFilter', 'setScopeFilter'], + '', + false + ); + + $this->stockRegistryProvider = $this->objectManagerHelper->getObject( '\Magento\CatalogInventory\Model\StockRegistryProvider', [ 'stockRepository' => $this->stockRepository, 'stockFactory' => $this->stockFactory, 'stockItemRepository' => $this->stockItemRepository, - 'stockItemFactory' => $stockItemFactory, + 'stockItemFactory' => $this->stockItemFactory, 'stockStatusRepository' => $this->stockStatusRepository, - 'stockStatusFactory' => $stockStatusFactory, + 'stockStatusFactory' => $this->stockStatusFactory, + 'stockCriteriaFactory' => $this->stockCriteriaFactory, 'stockItemCriteriaFactory' => $this->stockItemCriteriaFactory, - 'stockStatusCriteriaFactory' => $this->stockStatusCriteriaFactory, - 'stockConfiguration' => $this->stockConfiguration + 'stockStatusCriteriaFactory' => $this->stockStatusCriteriaFactory ] ); } @@ -159,114 +233,63 @@ protected function tearDown() $this->stockRegistryProvider = null; } - public function testGetStockWithStock() + public function testGetStock() { - $stock = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockInterface') - ->disableOriginalConstructor() - ->setMethods(['getStockId']) - ->getMockForAbstractClass(); - $this->stockFactory->expects($this->any())->method('create')->willReturn($stock); - $stock->expects($this->once())->method('getStockId')->willReturn($this->productData['stock_id']); - $this->stockRepository->expects($this->once())->method('get')->willReturn($stock); - $this->assertEquals($stock, $this->stockRegistryProvider->getStock($this->productData['stock_id'])); - } - - public function testGetStockWithoutStock() - { - $this->stockConfiguration->expects($this->once())->method('getDefaultScopeId') - ->willReturn($this->productData['scope_id']); - $stock = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockInterface') - ->disableOriginalConstructor() - ->setMethods(['getStockId']) - ->getMockForAbstractClass(); - $stock->expects($this->once())->method('getStockId')->willReturn($this->productData['stock_id']); - $stockCollection = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockCollectionInterface') - ->disableOriginalConstructor() - ->setMethods(['getItems']) - ->getMockForAbstractClass(); - $stockCriteria = $this->getMockBuilder('\Magento\CatalogInventory\Api\StockCriteriaInterface') - ->disableOriginalConstructor() - ->setMethods(['getItems']) - ->getMockForAbstractClass(); - $stockCriteria->expects($this->once())->method('setScopeFilter')->with($this->productData['scope_id']) - ->willReturnSelf(); - $this->stockCriteriaFactory->expects($this->once())->method('create')->willReturn($stockCriteria); - $this->stockRepository->expects($this->once())->method('getList') - ->with($stockCriteria)->willReturn($stockCollection); - $stockCollection->expects($this->once())->method('getItems')->willReturn([$stock]); - $this->assertEquals($stock, $this->stockRegistryProvider->getStock(null)); + $this->stockCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockCriteria); + $this->stockCriteria->expects($this->once())->method('setScopeFilter')->willReturn(null); + $stockCollection = $this->getMock( + '\Magento\CatalogInventory\Model\ResourceModel\Stock\Collection', + ['getFirstItem', '__wakeup', 'getItems'], + [], + '', + false + ); + $stockCollection->expects($this->once())->method('getItems')->willReturn([$this->stock]); + $this->stockRepository->expects($this->once())->method('getList')->willReturn($stockCollection); + $this->stock->expects($this->once())->method('getStockId')->willReturn(true); + $this->assertEquals($this->stock, $this->stockRegistryProvider->getStock($this->scopeId)); } public function testGetStockItem() { - $stock = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockInterface') - ->disableOriginalConstructor() - ->setMethods(['getStockId']) - ->getMockForAbstractClass(); - $this->stockRepository->expects($this->once())->method('get')->willReturn($stock); - $stockItemCriteria = $this->getMockBuilder('Magento\CatalogInventory\Api\StockItemCriteriaInterface') - ->disableOriginalConstructor() - ->setMethods(['setProductsFilter', 'setStockFilter']) - ->getMockForAbstractClass(); - $this->stockItemCriteriaFactory->expects($this->once())->method('create')->willReturn($stockItemCriteria); - $stockItemCriteria->expects($this->once())->method('setProductsFilter')->with($this->productData['product_id']) - ->willReturnSelf(); - $stock->expects($this->once())->method('getStockId')->willReturn($this->productData['stock_id']); - $stockItemCriteria->expects($this->once())->method('setStockFilter')->with($stock) - ->willReturnSelf(); - $stockItemCollection = $this->getMockBuilder( - '\Magento\CatalogInventory\Model\ResourceModel\Stock\Item\Collection' - ) - ->disableOriginalConstructor() - ->setMethods(['getFirstItem', 'getItems']) - ->getMock(); - $stockItem = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockItemInterface') - ->disableOriginalConstructor() - ->setMethods(['getItemId']) - ->getMockForAbstractClass(); - $stockItemCollection->expects($this->once())->method('getItems')->willReturn([$stockItem]); + $this->stockItemCriteriaFactory->expects($this->once())->method('create')->willReturn($this->stockItemCriteria); + $this->stockItemCriteria->expects($this->once())->method('setProductsFilter')->willReturn(null); + $stockItemCollection = $this->getMock( + '\Magento\CatalogInventory\Model\ResourceModel\Stock\Item\Collection', + ['getFirstItem', '__wakeup', 'getItems'], + [], + '', + false + ); + $stockItemCollection->expects($this->once())->method('getItems')->willReturn([$this->stockItem]); $this->stockItemRepository->expects($this->once())->method('getList')->willReturn($stockItemCollection); - $stockItem->expects($this->once())->method('getItemId')->willReturn(true); + $this->stockItem->expects($this->once())->method('getItemId')->willReturn(true); $this->assertEquals( - $stockItem, - $this->stockRegistryProvider->getStockItem($this->productData['product_id'], $this->productData['stock_id']) + $this->stockItem, + $this->stockRegistryProvider->getStockItem($this->productId, $this->scopeId) ); } public function testGetStockStatus() { - $stockStatusCriteria = $this->getMockBuilder('Magento\CatalogInventory\Api\StockStatusCriteriaInterface') - ->disableOriginalConstructor() - ->setMethods(['setProductsFilter', 'addFilter']) - ->getMockForAbstractClass(); $this->stockStatusCriteriaFactory->expects($this->once()) ->method('create') - ->willReturn($stockStatusCriteria); - $stockStatusCriteria->expects($this->once())->method('setProductsFilter') - ->with($this->productData['product_id']) - ->willReturnSelf(); - $stockStatusCriteria->expects($this->once())->method('addFilter') - ->with('stock', 'stock_id', $this->productData['stock_id']) - ->willReturnSelf(); - $stockStatusCollection = $this->getMockBuilder( - '\Magento\CatalogInventory\Model\ResourceModel\Stock\Status\Collection' - ) - ->disableOriginalConstructor() - ->setMethods(['getFirstItem', 'getItems']) - ->getMock(); - $stockStatus = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockStatusInterface') - ->disableOriginalConstructor() - ->setMethods(['getProductId']) - ->getMockForAbstractClass(); - $stockStatusCollection->expects($this->once())->method('getItems')->willReturn([$stockStatus]); - $stockStatus->expects($this->once())->method('getProductId')->willReturn($this->productData['product_id']); + ->willReturn($this->stockStatusCriteria); + $this->stockStatusCriteria->expects($this->once())->method('setScopeFilter')->willReturn(null); + $this->stockStatusCriteria->expects($this->once())->method('setProductsFilter')->willReturn(null); + $stockStatusCollection = $this->getMock( + '\Magento\CatalogInventory\Model\ResourceModel\Stock\Status\Collection', + ['getFirstItem', '__wakeup', 'getItems'], + [], + '', + false + ); + $stockStatusCollection->expects($this->once())->method('getItems')->willReturn([$this->stockStatus]); $this->stockStatusRepository->expects($this->once())->method('getList')->willReturn($stockStatusCollection); + $this->stockStatus->expects($this->once())->method('getProductId')->willReturn($this->productId); $this->assertEquals( - $stockStatus, - $this->stockRegistryProvider->getStockStatus( - $this->productData['product_id'], - $this->productData['stock_id'] - ) + $this->stockStatus, + $this->stockRegistryProvider->getStockStatus($this->productId, $this->scopeId) ); } } diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/Stock/ItemTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/Stock/ItemTest.php index a81f930ccb6ce..27188a0efd352 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Model/Stock/ItemTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/Stock/ItemTest.php @@ -63,9 +63,9 @@ class ItemTest extends \PHPUnit_Framework_TestCase protected $resource; /** - * @var \Magento\CatalogInventory\Api\StockRegistryInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\CatalogInventory\Model\ResourceModel\Stock\Item\Collection|\PHPUnit_Framework_MockObject_MockObject */ - protected $stockRegistry; + protected $resourceCollection; /** * @var int @@ -117,8 +117,13 @@ protected function setUp() false ); - $this->stockRegistry = $this->getMockBuilder('Magento\CatalogInventory\Api\StockRegistryInterface') - ->getMockForAbstractClass(); + $this->resourceCollection = $this->getMock( + 'Magento\CatalogInventory\Model\ResourceModel\Stock\Item\Collection', + [], + [], + '', + false + ); $this->objectManagerHelper = new ObjectManagerHelper($this); @@ -132,7 +137,7 @@ protected function setUp() 'stockConfiguration' => $this->stockConfiguration, 'stockItemRepository' => $this->stockItemRepository, 'resource' => $this->resource, - 'stockRegistry' => $this->stockRegistry + 'stockItemRegistry' => $this->resourceCollection ] ); } @@ -452,14 +457,4 @@ public function getQtyIncrementsDataProvider() ], ]; } - - public function testGetStockId() - { - $stockId = 1; - $stock = $this->getMockBuilder('Magento\CatalogInventory\Api\Data\StockInterface') - ->getMockForAbstractClass(); - $this->stockRegistry->expects($this->once())->method('getStock')->willReturn($stock); - $stock->expects($this->once())->method('getStockId')->willReturn($stockId); - $this->assertEquals($stockId, $this->item->getStockId()); - } } diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Observer/SaveInventoryDataObserverTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Observer/SaveInventoryDataObserverTest.php index 17ca09b6e24fd..96604e75d9294 100644 --- a/app/code/Magento/CatalogInventory/Test/Unit/Observer/SaveInventoryDataObserverTest.php +++ b/app/code/Magento/CatalogInventory/Test/Unit/Observer/SaveInventoryDataObserverTest.php @@ -19,26 +19,6 @@ class SaveInventoryDataObserverTest extends \PHPUnit_Framework_TestCase */ protected $stockIndex; - /** - * @var \Magento\CatalogInventory\Api\StockConfigurationInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $stockConfiguration; - - /** - * @var \Magento\CatalogInventory\Model\Spi\stockResolverInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $stockResolver; - - /** - * @var \Magento\CatalogInventory\Api\StockRegistryInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $stockRegistry; - - /** - * @var \Magento\CatalogInventory\Api\StockItemRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject - */ - protected $stockItemRepository; - /** * @var \Magento\Framework\Event|\PHPUnit_Framework_MockObject_MockObject */ @@ -51,30 +31,12 @@ class SaveInventoryDataObserverTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->stockIndex = $this->getMockBuilder('Magento\CatalogInventory\Api\StockIndexInterface') - ->disableOriginalConstructor() - ->setMethods(['rebuild']) - ->getMockForAbstractClass(); - - $this->stockConfiguration = $this->getMockBuilder('Magento\CatalogInventory\Api\StockConfigurationInterface') - ->disableOriginalConstructor() - ->setMethods(['getDefaultScopeId']) - ->getMockForAbstractClass(); - - $this->stockResolver = $this->getMockBuilder('Magento\CatalogInventory\Model\Spi\StockResolverInterface') - ->disableOriginalConstructor() - ->setMethods(['getStockId']) - ->getMockForAbstractClass(); - - $this->stockRegistry = $this->getMockBuilder('Magento\CatalogInventory\Api\StockRegistryInterface') - ->disableOriginalConstructor() - ->setMethods(['getStockItem']) - ->getMockForAbstractClass(); - - $this->stockItemRepository = $this->getMockBuilder('Magento\CatalogInventory\Api\StockItemRepositoryInterface') - ->disableOriginalConstructor() - ->setMethods(['save']) - ->getMockForAbstractClass(); + $this->stockIndex = $this->getMockForAbstractClass( + 'Magento\CatalogInventory\Api\StockIndexInterface', + ['rebuild'], + '', + false + ); $this->event = $this->getMockBuilder('Magento\Framework\Event') ->disableOriginalConstructor() @@ -94,15 +56,11 @@ protected function setUp() 'Magento\CatalogInventory\Observer\SaveInventoryDataObserver', [ 'stockIndex' => $this->stockIndex, - 'stockConfiguration' => $this->stockConfiguration, - 'stockResolver' => $this->stockResolver, - 'stockRegistry' => $this->stockRegistry, - 'stockItemRepository' => $this->stockItemRepository ] ); } - public function testSaveInventoryDataWithoutStockData() + public function testSaveInventoryData() { $productId = 4; $websiteId = 5; @@ -134,49 +92,4 @@ public function testSaveInventoryDataWithoutStockData() $this->observer->execute($this->eventObserver); } - - public function testSaveInventoryDataWithStockData() - { - $stockItemData = [ - 'qty' => 4, - 'product_id' => 2, - 'website_id' => 3, - 'stock_id' => 1, - 'qty_correction' => -1 - ]; - - $product = $this->getMock( - 'Magento\Catalog\Model\Product', - ['getStockData', 'getId', 'getData'], - [], - '', - false - ); - $product->expects($this->exactly(2))->method('getStockData')->will($this->returnValue( - ['qty' => $stockItemData['qty']] - )); - $product->expects($this->once())->method('getId')->will($this->returnValue($stockItemData['product_id'])); - $product->expects($this->any())->method('getData')->willReturnMap( - [ - ['stock_data/original_inventory_qty', null, $stockItemData['qty']+1] - ] - ); - $this->stockConfiguration->expects($this->once())->method('getDefaultScopeId') - ->willReturn($stockItemData['website_id']); - $this->stockResolver->expects($this->once())->method('getStockId') - ->with($stockItemData['product_id'], $stockItemData['website_id']) - ->willReturn($stockItemData['stock_id']); - $stockItem = $this->getMockBuilder('\Magento\CatalogInventory\Api\Data\StockItemInterface') - ->disableOriginalConstructor() - ->setMethods(['addData']) - ->getMockForAbstractClass(); - $this->stockRegistry->expects($this->once())->method('getStockItem') - ->with($stockItemData['product_id'], $stockItemData['website_id']) - ->willReturn($stockItem); - $stockItem->expects($this->once())->method('addData')->with($stockItemData)->willReturnSelf(); - $this->stockItemRepository->expects($this->once())->method('save')->with($stockItem); - $this->event->expects($this->once())->method('getProduct')->will($this->returnValue($product)); - - $this->observer->execute($this->eventObserver); - } } diff --git a/app/code/Magento/CatalogInventory/etc/di.xml b/app/code/Magento/CatalogInventory/etc/di.xml index 40f9bf2d1275d..fbce94514ebff 100644 --- a/app/code/Magento/CatalogInventory/etc/di.xml +++ b/app/code/Magento/CatalogInventory/etc/di.xml @@ -30,8 +30,7 @@ - - +