Skip to content

Commit 1cbfd19

Browse files
committed
Cover changes by tests
1 parent 8040c7b commit 1cbfd19

File tree

5 files changed

+255
-48
lines changed

5 files changed

+255
-48
lines changed

InventoryDataExporter/Model/Query/StockStatusDeleteQuery.php

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
use Magento\DataExporter\Model\Indexer\FeedIndexMetadata;
1111
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\Serialize\SerializerInterface;
13+
use Magento\Framework\Stdlib\DateTime;
1214

1315
/**
1416
* Stock Status mark as deleted query builder
@@ -25,16 +27,32 @@ class StockStatusDeleteQuery
2527
*/
2628
private $metadata;
2729

30+
/**
31+
* @var SerializerInterface
32+
*/
33+
private $serializer;
34+
35+
/**
36+
* @var DateTime
37+
*/
38+
private $dateTime;
39+
2840
/**
2941
* @param ResourceConnection $resourceConnection
3042
* @param FeedIndexMetadata $metadata
43+
* @param SerializerInterface $serializer
44+
* @param DateTime $dateTime
3145
*/
3246
public function __construct(
3347
ResourceConnection $resourceConnection,
34-
FeedIndexMetadata $metadata
48+
FeedIndexMetadata $metadata,
49+
SerializerInterface $serializer,
50+
DateTime $dateTime
3551
) {
3652
$this->resourceConnection = $resourceConnection;
3753
$this->metadata = $metadata;
54+
$this->serializer = $serializer;
55+
$this->dateTime = $dateTime;
3856
}
3957

4058
/**
@@ -104,14 +122,52 @@ public function getStocksWithSources(array $sourceCodes): array
104122
*/
105123
public function markStockStatusesAsDeleted(array $idsToDelete): void
106124
{
125+
$records = [];
126+
foreach ($idsToDelete as $deletedItemId => $stockStatusData) {
127+
$records[] = $this->buildFeedData($deletedItemId, $stockStatusData);
128+
}
107129
$connection = $this->resourceConnection->getConnection();
108130
$feedTableName = $this->resourceConnection->getTableName($this->metadata->getFeedTableName());
109-
$connection->update(
131+
$connection->insertOnDuplicate(
110132
$feedTableName,
111-
['is_deleted' => new \Zend_Db_Expr('1')],
112-
[
113-
'id IN (?)' => $idsToDelete
114-
]
133+
$records
115134
);
116135
}
136+
137+
/**
138+
* @param string $stockStatusId
139+
* @param array $stockIdAndSku
140+
* @return array
141+
*/
142+
private function buildFeedData(string $stockStatusId, array $stockIdAndSku): array
143+
{
144+
if (!isset($stockIdAndSku['stock_id'], $stockIdAndSku['sku'])) {
145+
throw new \RuntimeException(
146+
sprintf(
147+
"inventory_data_exporter_stock_status indexer error: cannot build unique id from %s",
148+
\var_export($stockIdAndSku, true)
149+
)
150+
);
151+
}
152+
$feedData = [
153+
'id' => $stockStatusId,
154+
'stockId' => $stockIdAndSku['stock_id'],
155+
'sku' => $stockIdAndSku['sku'],
156+
'qty' => 0,
157+
'qtyForSale' => 0,
158+
'infiniteStock' => false,
159+
'isSalable' => false,
160+
'updatedAt' => $this->dateTime->formatDate(time())
161+
162+
];
163+
164+
return [
165+
'id' => $stockStatusId,
166+
'stock_id' => $stockIdAndSku['stock_id'],
167+
'sku' => $stockIdAndSku['sku'],
168+
'feed_data' => $this->serializer->serialize($feedData),
169+
'is_deleted' => 1,
170+
'modified_at' => $this->dateTime->formatDate(time())
171+
];
172+
}
117173
}

InventoryDataExporter/Plugin/BulkSourceUnassign.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ private function getStocksToDelete(
7070
$stocksToDelete = [];
7171
foreach ($affectedSkus as $deletedItemSku) {
7272
foreach (array_keys($sourcesByStocks) as $stockId) {
73-
$stocksToDelete[] = StockStatusIdBuilder::build(
73+
$stockStatusId = StockStatusIdBuilder::build(
7474
['stockId' => (string)$stockId, 'sku' => $deletedItemSku]
7575
);
76+
$stocksToDelete[$stockStatusId] = [
77+
'stock_id' => (string)$stockId,
78+
'sku' => $deletedItemSku
79+
];
7680
}
7781
if (!isset($sourcesAssignedToProducts[$deletedItemSku])) {
7882
continue ;
@@ -84,9 +88,7 @@ private function getStocksToDelete(
8488
$stockStatusId = StockStatusIdBuilder::build(
8589
['stockId' => (string)$fetchedItemStockId, 'sku' => $deletedItemSku]
8690
);
87-
if ($key = \array_search($stockStatusId, $stocksToDelete, false)) {
88-
unset($stocksToDelete[(int)$key]);
89-
}
91+
unset($stocksToDelete[$stockStatusId]);
9092
}
9193
}
9294
}

InventoryDataExporter/Plugin/MarkItemsAsDeleted.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ private function getStocksToDelete(array $deletedSourceItems, $fetchedSourceItem
6666
foreach ($deletedSourceItems as $deletedItemSku => $deletedItemSources) {
6767
foreach ($fetchedSourceItems[$deletedItemSku] as $fetchedItemStockId => $fetchedItemSources) {
6868
if ($this->getContainsAllKeys($fetchedItemSources, $deletedItemSources)) {
69-
$stocksToDelete[] = StockStatusIdBuilder::build(
69+
$stockStatusId = StockStatusIdBuilder::build(
7070
['stockId' => (string)$fetchedItemStockId, 'sku' => $deletedItemSku]
7171
);
72+
$stocksToDelete[$stockStatusId] = [
73+
'stock_id' => (string)$fetchedItemStockId,
74+
'sku' => $deletedItemSku
75+
];
7276
}
7377
}
7478
}

InventoryDataExporter/Test/Integration/PartialReindexCheckTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ public function testSourceItemQtyUpdated()
102102
*/
103103
public function testSourceBulkUnassign()
104104
{
105-
$skus = ['product_in_EU_stock_with_2_sources', 'product_in_Global_stock_with_3_sources', 'product_with_default_stock_only'];
105+
$skus = [
106+
'product_in_EU_stock_with_2_sources',
107+
'product_in_Global_stock_with_3_sources',
108+
'product_with_default_stock_only'
109+
];
106110

107111
$this->bulkSourceUnassign->execute(
108112
$skus,

0 commit comments

Comments
 (0)