Skip to content

Commit

Permalink
Merge pull request magento#770 from magento-engcom/633
Browse files Browse the repository at this point in the history
MSI-633: Investigate possible exception with $this->getProductIdsBySkus->execute([$sku])[$sku]
  • Loading branch information
Valeriy Nayda authored Mar 28, 2018
2 parents e53d730 + 39ff5d7 commit 3a50f89
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 226 deletions.
12 changes: 11 additions & 1 deletion app/code/Magento/InventoryCatalog/Model/GetProductIdsBySkus.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\InventoryCatalog\Model;

use Magento\Catalog\Model\ResourceModel\Product as ProductResourceModel;
use Magento\Framework\Exception\InputException;

/**
* @inheritdoc
Expand All @@ -33,6 +34,15 @@ public function __construct(
*/
public function execute(array $skus): array
{
return $this->productResource->getProductsIdsBySkus($skus);
$idsBySkus = $this->productResource->getProductsIdsBySkus($skus);
$notFoundedSkus = array_diff($skus, array_keys($idsBySkus));

if (!empty($notFoundedSkus)) {
throw new InputException(
__('Following products with requested skus were not found: %1', implode($notFoundedSkus, ', '))
);
}

return $idsBySkus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\InventoryCatalog\Model;

use Magento\Framework\Exception\InputException;

/**
* Provides all product SKUs by ProductIds. Key is sku, value is product id
* @api
Expand All @@ -16,6 +18,7 @@ interface GetProductIdsBySkusInterface
/**
* @param array $skus
* @return array
* @throws InputException
*/
public function execute(array $skus): array;
}
13 changes: 11 additions & 2 deletions app/code/Magento/InventoryCatalog/Model/GetSkusByProductIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResourceModel;
use Magento\Framework\Exception\InputException;

/**
* @inheritdoc
Expand All @@ -34,11 +35,19 @@ public function __construct(
*/
public function execute(array $productIds): array
{
$productsSku = array_column(
$skuByIds = array_column(
$this->productResource->getProductsSku($productIds),
ProductInterface::SKU,
'entity_id'
);
return $productsSku;
$notFoundedIds = array_diff($productIds, array_keys($skuByIds));

if (!empty($notFoundedIds)) {
throw new InputException(
__('Following products with requested ids were not found: %1', implode($notFoundedIds, ', '))
);
}

return $skuByIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\InventoryCatalog\Model;

use Magento\Framework\Exception\InputException;

/**
* Provides all product SKUs by ProductIds. Key is product id, value is sku
* @api
Expand All @@ -16,6 +18,7 @@ interface GetSkusByProductIdsInterface
/**
* @param array $productIds
* @return array
* @throws InputException
*/
public function execute(array $productIds): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Magento\InventoryCatalog\Model\GetProductIdsBySkusInterface;

/**
* Set data to legacy catalocinventory_stock_item table via plain MySql query
* Set data to legacy cataloginventory_stock_item table via plain MySql query
*/
class SetDataToLegacyStockItem
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ public function testExecute()

self::assertEquals($idBySku, $this->getProductIdsBySkus->execute(array_keys($idBySku)));
}

/**
* @magentoDataFixture Magento/Catalog/_files/products_for_search.php
*
* @expectedException \Magento\Framework\Exception\InputException
* @expectedExceptionMessage Following products with requested skus were not found: not_existed_sku
*/
public function testExecuteWithNotExistedSkus()
{
$skus = ['not_existed_sku', 'search_product_2'];

$this->getProductIdsBySkus->execute($skus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ public function testExecute()

self::assertEquals($skuById, $this->getSkusByProductIds->execute(array_keys($skuById)));
}

/**
* @magentoDataFixture Magento/Catalog/_files/products_for_search.php
*
* @expectedException \Magento\Framework\Exception\InputException
* @expectedExceptionMessage Following products with requested ids were not found: 999
*/
public function testExecuteWithNotExistedSkus()
{
$ids = [999, 102];

$this->getSkusByProductIds->execute($ids);
}
}
Loading

0 comments on commit 3a50f89

Please sign in to comment.