Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

386: Adapt \Magento\CatalogInventory\Helper\Stock::addStockStatusToProducts #418

Merged
merged 19 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
eba40e4
386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStatusToP…
p-bystritsky Jan 16, 2018
a66c80d
386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStatusToP…
p-bystritsky Jan 16, 2018
564799d
386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStatusToP…
p-bystritsky Jan 16, 2018
f41f682
386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStatusToP…
p-bystritsky Jan 16, 2018
3f27de4
Merge branch 'msi-417-introduce-inventory-index-module' into 386
p-bystritsky Jan 17, 2018
845e1d9
386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStatusToP…
p-bystritsky Jan 17, 2018
42e3371
386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStatusToP…
p-bystritsky Jan 17, 2018
9ec0f8f
Merge branch 'develop' into 386
p-bystritsky Jan 17, 2018
8694b3d
386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStatusToP…
p-bystritsky Jan 19, 2018
4489860
Merge branch 'develop' into 386
p-bystritsky Jan 19, 2018
2537369
Merge remote-tracking branch 'origin/develop' into 386
Jan 23, 2018
5359a86
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
RomaKis Jan 23, 2018
a59d1c6
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
Jan 24, 2018
01a9701
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
Jan 24, 2018
6ddfc9b
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
Jan 25, 2018
164762d
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
Jan 25, 2018
79e80dc
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
Jan 25, 2018
d27decb
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
Jan 25, 2018
6ff5716
MSI: 386: Adapt `\Magento\CatalogInventory\Helper\Stock::addStockStat…
Jan 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function tearDown()
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*/
Expand All @@ -71,7 +71,7 @@ public function testGetProductQuantity()
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function setUp()
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*/
Expand All @@ -61,7 +61,7 @@ public function testProductIsInStock()
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GetAssignedSourcesForStockTest extends WebapiAbstract
/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
*/
public function testGetAssignedSourcesForStock()
{
Expand All @@ -51,7 +51,7 @@ public function testGetAssignedSourcesForStock()
/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
*/
public function testGetAssignedSourcesWithNotNumericStockId()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class UnassignSourceFromStockTest extends WebapiAbstract
*
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_link.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
*/
public function testUnassignSourceFromStock()
{
Expand Down
5 changes: 0 additions & 5 deletions app/code/Magento/InventoryApi/Test/_files/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
$productRepository->cleanCache();

/** @var IndexerInterface $indexer */
$indexer = Bootstrap::getObjectManager()->create(IndexerInterface::class);
$indexer->load(InventoryIndexer::INDEXER_ID);
$indexer->reindexAll();

$stockData = [
'SKU-1' => [
'qty' => 8.5,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Plugin\CatalogInventory\Helper\Stock;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
use Magento\CatalogInventory\Helper\Stock;
use Magento\InventoryApi\Api\IsProductInStockInterface;
use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite;

/**
* Adapt addStockStatusToProducts for multi stocks.
*/
class AdaptAddStockStatusToProductsPlugin
{
/**
* @var GetStockIdForCurrentWebsite
*/
private $getStockIdForCurrentWebsite;

/**
* @var IsProductInStockInterface
*/
private $isProductInStock;

/**
* @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
* @param IsProductInStockInterface $isProductInStock
*/
public function __construct(
GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
IsProductInStockInterface $isProductInStock
) {
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
$this->isProductInStock = $isProductInStock;
}

/**
* @param Stock $subject
* @param callable $proceed
* @param AbstractCollection $productCollection
* @return void
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundAddStockStatusToProducts(
Stock $subject,
callable $proceed,
AbstractCollection $productCollection
) {
$stockId = $this->getStockIdForCurrentWebsite->execute();

/** @var Product $product */
foreach ($productCollection as $product) {
$isSalable = (int)$this->isProductInStock->execute($product->getSku(), $stockId);
$product->setIsSalable($isSalable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@
/**
* Adapt adding is in stock filter to collection for multi stocks.
*/
class AdaptAddIsInStockFilterToCollectionToMultiStocks
class AdaptAddIsInStockFilterToCollection
{
/**
* @var GetStockIdForCurrentWebsite
*/
private $stockIdForCurrentWebsite;
private $getStockIdForCurrentWebsite;

/**
* @var AddIsInStockFilterToCollection
*/
private $adaptedAddIsInStockFilterToCollection;

/**
* @param GetStockIdForCurrentWebsite $stockIdForCurrentWebsite
* @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
* @param AddIsInStockFilterToCollection $adaptedAddIsInStockFilterToCollection
*/
public function __construct(
GetStockIdForCurrentWebsite $stockIdForCurrentWebsite,
GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
AddIsInStockFilterToCollection $adaptedAddIsInStockFilterToCollection
) {
$this->stockIdForCurrentWebsite = $stockIdForCurrentWebsite;
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
$this->adaptedAddIsInStockFilterToCollection = $adaptedAddIsInStockFilterToCollection;
}

Expand All @@ -51,7 +51,7 @@ public function aroundAddIsInStockFilterToCollection(
callable $proceed,
$collection
) {
$stockId = $this->stockIdForCurrentWebsite->execute();
$stockId = $this->getStockIdForCurrentWebsite->execute();
$this->adaptedAddIsInStockFilterToCollection->addIsInStockFilterToCollection($collection, $stockId);

return $stockStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@
/**
* Adapt adding stock data to collection for multi stocks.
*/
class AdaptAddStockDataToCollectionToMultiStocks
class AdaptAddStockDataToCollection
{
/**
* @var GetStockIdForCurrentWebsite
*/
private $stockIdForCurrentWebsite;
private $getStockIdForCurrentWebsite;

/**
* @var AddStockDataToCollection
*/
private $addStockDataToCollection;

/**
* @param GetStockIdForCurrentWebsite $stockIdForCurrentWebsite
* @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite
* @param AddStockDataToCollection $addStockDataToCollection
*/
public function __construct(
GetStockIdForCurrentWebsite $stockIdForCurrentWebsite,
GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
AddStockDataToCollection $addStockDataToCollection
) {
$this->stockIdForCurrentWebsite = $stockIdForCurrentWebsite;
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
$this->addStockDataToCollection = $addStockDataToCollection;
}

Expand All @@ -54,7 +54,7 @@ public function aroundAddStockDataToCollection(
$collection,
$isFilterInStock
) {
$stockId = $this->stockIdForCurrentWebsite->execute();
$stockId = $this->getStockIdForCurrentWebsite->execute();
$this->addStockDataToCollection->addStockDataToCollection($collection, (bool)$isFilterInStock, $stockId);

return $collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@
/**
* Adapt adding stock status to select for multi stocks.
*/
class AdaptAddStockStatusToSelectToMultiStocks
class AdaptAddStockStatusToSelect
{
/**
* @var StockResolverInterface
*/
private $stockResolver;
private $getStockIdForCurrentWebsite;

/**
* @var AddStockStatusToSelect
*/
private $adaptedAddStockStatusToSelect;

/**
* @param StockResolverInterface $stockResolver
* @param StockResolverInterface $getStockIdForCurrentWebsite
* @param AddStockStatusToSelect $adaptedAddStockStatusToSelect
*/
public function __construct(
StockResolverInterface $stockResolver,
StockResolverInterface $getStockIdForCurrentWebsite,
AddStockStatusToSelect $adaptedAddStockStatusToSelect
) {
$this->stockResolver = $stockResolver;
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
$this->adaptedAddStockStatusToSelect = $adaptedAddStockStatusToSelect;
}

Expand All @@ -62,7 +62,7 @@ public function aroundAddStockStatusToSelect(
throw new LocalizedException(__('Website code is empty'));
}

$stock = $this->stockResolver->get(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
$stock = $this->getStockIdForCurrentWebsite->get(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
$stockId = (int)$stock->getStockId();

$this->adaptedAddStockStatusToSelect->addStockStatusToSelect($select, $stockId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryCatalog\Test\Integration\CatalogInventory\Helper\Stock;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogInventory\Helper\Stock;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class AddStockStatusToProductsOnDefaultStockTest extends TestCase
{
/**
* @var Stock
*/
private $stockHelper;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$this->stockHelper = Bootstrap::getObjectManager()->get(Stock::class);
}

public function testAddStockStatusToProducts()
{
$productsData = [
'SKU-1' => 1,
'SKU-2' => 1,
'SKU-3' => 0,
];

/** @var Collection $collection */
$collection = Bootstrap::getObjectManager()->create(Collection::class);
$collection->addFieldToFilter(ProductInterface::SKU, ['in' => array_keys($productsData)]);
$collection->load();

$this->stockHelper->addStockStatusToProducts($collection);

/** @var ProductInterface $product */
foreach ($collection as $product) {
self::assertEquals($productsData[$product->getSku()], $product->isSalable());
}
}
}
Loading