Skip to content

Commit

Permalink
Cover changes by tests
Browse files Browse the repository at this point in the history
  • Loading branch information
le0n4ik committed Nov 1, 2021
1 parent 8040c7b commit 1cbfd19
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 48 deletions.
68 changes: 62 additions & 6 deletions InventoryDataExporter/Model/Query/StockStatusDeleteQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Stdlib\DateTime;

/**
* Stock Status mark as deleted query builder
Expand All @@ -25,16 +27,32 @@ class StockStatusDeleteQuery
*/
private $metadata;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var DateTime
*/
private $dateTime;

/**
* @param ResourceConnection $resourceConnection
* @param FeedIndexMetadata $metadata
* @param SerializerInterface $serializer
* @param DateTime $dateTime
*/
public function __construct(
ResourceConnection $resourceConnection,
FeedIndexMetadata $metadata
FeedIndexMetadata $metadata,
SerializerInterface $serializer,
DateTime $dateTime
) {
$this->resourceConnection = $resourceConnection;
$this->metadata = $metadata;
$this->serializer = $serializer;
$this->dateTime = $dateTime;
}

/**
Expand Down Expand Up @@ -104,14 +122,52 @@ public function getStocksWithSources(array $sourceCodes): array
*/
public function markStockStatusesAsDeleted(array $idsToDelete): void
{
$records = [];
foreach ($idsToDelete as $deletedItemId => $stockStatusData) {
$records[] = $this->buildFeedData($deletedItemId, $stockStatusData);
}
$connection = $this->resourceConnection->getConnection();
$feedTableName = $this->resourceConnection->getTableName($this->metadata->getFeedTableName());
$connection->update(
$connection->insertOnDuplicate(
$feedTableName,
['is_deleted' => new \Zend_Db_Expr('1')],
[
'id IN (?)' => $idsToDelete
]
$records
);
}

/**
* @param string $stockStatusId
* @param array $stockIdAndSku
* @return array
*/
private function buildFeedData(string $stockStatusId, array $stockIdAndSku): array
{
if (!isset($stockIdAndSku['stock_id'], $stockIdAndSku['sku'])) {
throw new \RuntimeException(
sprintf(
"inventory_data_exporter_stock_status indexer error: cannot build unique id from %s",
\var_export($stockIdAndSku, true)
)
);
}
$feedData = [
'id' => $stockStatusId,
'stockId' => $stockIdAndSku['stock_id'],
'sku' => $stockIdAndSku['sku'],
'qty' => 0,
'qtyForSale' => 0,
'infiniteStock' => false,
'isSalable' => false,
'updatedAt' => $this->dateTime->formatDate(time())

];

return [
'id' => $stockStatusId,
'stock_id' => $stockIdAndSku['stock_id'],
'sku' => $stockIdAndSku['sku'],
'feed_data' => $this->serializer->serialize($feedData),
'is_deleted' => 1,
'modified_at' => $this->dateTime->formatDate(time())
];
}
}
10 changes: 6 additions & 4 deletions InventoryDataExporter/Plugin/BulkSourceUnassign.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ private function getStocksToDelete(
$stocksToDelete = [];
foreach ($affectedSkus as $deletedItemSku) {
foreach (array_keys($sourcesByStocks) as $stockId) {
$stocksToDelete[] = StockStatusIdBuilder::build(
$stockStatusId = StockStatusIdBuilder::build(
['stockId' => (string)$stockId, 'sku' => $deletedItemSku]
);
$stocksToDelete[$stockStatusId] = [
'stock_id' => (string)$stockId,
'sku' => $deletedItemSku
];
}
if (!isset($sourcesAssignedToProducts[$deletedItemSku])) {
continue ;
Expand All @@ -84,9 +88,7 @@ private function getStocksToDelete(
$stockStatusId = StockStatusIdBuilder::build(
['stockId' => (string)$fetchedItemStockId, 'sku' => $deletedItemSku]
);
if ($key = \array_search($stockStatusId, $stocksToDelete, false)) {
unset($stocksToDelete[(int)$key]);
}
unset($stocksToDelete[$stockStatusId]);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion InventoryDataExporter/Plugin/MarkItemsAsDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ private function getStocksToDelete(array $deletedSourceItems, $fetchedSourceItem
foreach ($deletedSourceItems as $deletedItemSku => $deletedItemSources) {
foreach ($fetchedSourceItems[$deletedItemSku] as $fetchedItemStockId => $fetchedItemSources) {
if ($this->getContainsAllKeys($fetchedItemSources, $deletedItemSources)) {
$stocksToDelete[] = StockStatusIdBuilder::build(
$stockStatusId = StockStatusIdBuilder::build(
['stockId' => (string)$fetchedItemStockId, 'sku' => $deletedItemSku]
);
$stocksToDelete[$stockStatusId] = [
'stock_id' => (string)$fetchedItemStockId,
'sku' => $deletedItemSku
];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public function testSourceItemQtyUpdated()
*/
public function testSourceBulkUnassign()
{
$skus = ['product_in_EU_stock_with_2_sources', 'product_in_Global_stock_with_3_sources', 'product_with_default_stock_only'];
$skus = [
'product_in_EU_stock_with_2_sources',
'product_in_Global_stock_with_3_sources',
'product_with_default_stock_only'
];

$this->bulkSourceUnassign->execute(
$skus,
Expand Down
Loading

0 comments on commit 1cbfd19

Please sign in to comment.