Skip to content

Commit 42d9406

Browse files
committed
Code review changes
1 parent 7afb893 commit 42d9406

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

app/code/Magento/CatalogInventory/Block/Plugin/ProductView.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Catalog\Block\Product\View;
99
use Magento\CatalogInventory\Model\Product\QuantityValidator;
10+
use Magento\Framework\App\ObjectManager;
1011

1112
class ProductView
1213
{
@@ -16,12 +17,14 @@ class ProductView
1617
private $productQuantityValidator;
1718

1819
/**
19-
* @param QuantityValidator $productQuantityValidator
20+
* @param QuantityValidator|null $productQuantityValidator
2021
*/
2122
public function __construct(
22-
QuantityValidator $productQuantityValidator
23+
QuantityValidator $productQuantityValidator = null
2324
) {
24-
$this->productQuantityValidator = $productQuantityValidator;
25+
$this->productQuantityValidator = $productQuantityValidator ?? ObjectManager::getInstance()->get(
26+
QuantityValidator::class
27+
);
2528
}
2629

2730
/**

app/code/Magento/CatalogInventory/Model/Product/QuantityValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public function getData(int $productId, int|null $websiteId): array
3131
{
3232
$stockItem = $this->stockRegistry->getStockItem($productId, $websiteId);
3333

34+
if (!$stockItem) {
35+
return [];
36+
}
37+
3438
$params = [];
3539
$validators = [];
3640
$params['minAllowed'] = $stockItem->getMinSaleQty();

app/code/Magento/CatalogInventory/Test/Unit/Model/Product/QuantityValidatorTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,64 @@ public function testGetDataWithMinMaxAndIncrements(): void
6969
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
7070
$this->assertEquals($expected, $result);
7171
}
72+
73+
public function testReturnsEmptyArrayForNonExistentProductOrWebsite(): void
74+
{
75+
$this->stockRegistry->expects($this->once())
76+
->method('getStockItem')
77+
->with(self::PRODUCT_ID, self::WEBSITE_ID)
78+
->willReturn(null);
79+
80+
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
81+
$this->assertSame([], $result, 'Should return empty array when StockItem is not found');
82+
}
83+
84+
public function testHandlesNullValuesFromStockItem(): void
85+
{
86+
$stockItem = $this->createMock(StockItemInterface::class);
87+
$stockItem->method('getMinSaleQty')
88+
->willReturn(null);
89+
$stockItem->method('getMaxSaleQty')
90+
->willReturn(null);
91+
$stockItem->method('getQtyIncrements')
92+
->willReturn(null);
93+
94+
$this->stockRegistry->expects($this->once())
95+
->method('getStockItem')
96+
->with(self::PRODUCT_ID, self::WEBSITE_ID)
97+
->willReturn($stockItem);
98+
99+
$expected = [
100+
'validate-item-quantity' => [
101+
'minAllowed' => null
102+
],
103+
];
104+
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
105+
$this->assertEquals($expected, $result);
106+
}
107+
108+
public function testHandlesInvalidValuesFromStockItem(): void
109+
{
110+
$stockItem = $this->createMock(StockItemInterface::class);
111+
$stockItem->method('getMinSaleQty')
112+
->willReturn('not-a-number');
113+
$stockItem->method('getMaxSaleQty')
114+
->willReturn(-5);
115+
$stockItem->method('getQtyIncrements')
116+
->willReturn(false);
117+
118+
$this->stockRegistry->expects($this->once())
119+
->method('getStockItem')
120+
->with(self::PRODUCT_ID, self::WEBSITE_ID)
121+
->willReturn($stockItem);
122+
123+
$expected = [
124+
'validate-item-quantity' => [
125+
'minAllowed' => 'not-a-number',
126+
'maxAllowed' => -5
127+
],
128+
];
129+
$result = $this->quantityValidator->getData(self::PRODUCT_ID, self::WEBSITE_ID);
130+
$this->assertEquals($expected, $result);
131+
}
72132
}

0 commit comments

Comments
 (0)