Skip to content

Commit 09eb76b

Browse files
author
Cody Nguyen
committed
Fix issue #29932 "only_x_left_in_stock returns null if inventory is 0"
1 parent 1c9ba3e commit 09eb76b

File tree

2 files changed

+211
-6
lines changed

2 files changed

+211
-6
lines changed

app/code/Magento/CatalogInventoryGraphQl/Model/Resolver/OnlyXLeftInStockResolver.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ private function getOnlyXLeftQty(ProductInterface $product): ?float
8686

8787
$stockLeft = $stockCurrentQty - $stockItem->getMinQty();
8888

89-
$thresholdQty = (float)$this->scopeConfig->getValue(
90-
Configuration::XML_PATH_STOCK_THRESHOLD_QTY,
91-
ScopeInterface::SCOPE_STORE
92-
);
93-
94-
if ($stockCurrentQty > 0 && $stockLeft <= $thresholdQty) {
89+
if ($stockLeft > 0 && $stockLeft <= $thresholdQty) {
9590
return (float)$stockLeft;
9691
}
9792

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Test\Unit\Model\Resolver;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use Magento\CatalogInventoryGraphQl\Model\Resolver\OnlyXLeftInStockResolver;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\GraphQl\Model\Query\ContextInterface;
15+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\CatalogInventory\Api\StockRegistryInterface;
18+
use Magento\Catalog\Model\Product;
19+
use Magento\Store\Api\Data\StoreInterface;
20+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
21+
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
22+
23+
/**
24+
* Test class for \Magento\CustomerGraphQl\Model\Resolver\GenerateCustomerToken
25+
*/
26+
class OnlyXLeftInStockResolverTest extends TestCase
27+
{
28+
/**
29+
* Object Manager Instance
30+
*
31+
* @var ObjectManager
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* Testable Object
37+
*
38+
* @var RevokeCustomerToken
39+
*/
40+
private $resolver;
41+
42+
/**
43+
* @var ContextInterface|MockObject
44+
*/
45+
private $contextMock;
46+
47+
/**
48+
* @var Field|MockObject
49+
*/
50+
private $fieldMock;
51+
52+
/**
53+
* @var ResolveInfo|MockObject
54+
*/
55+
private $resolveInfoMock;
56+
57+
/**
58+
* @var ScopeConfigInterface|MockObject
59+
*/
60+
private $scopeConfigMock;
61+
62+
/**
63+
* @var StockRegistryInterface|MockObject
64+
*/
65+
private $stockRegistryMock;
66+
67+
/**
68+
* @var Product|MockObject
69+
*/
70+
private $productModelMock;
71+
72+
/**
73+
* @var StoreInterface|MockObject
74+
*/
75+
private $storeMock;
76+
77+
/**
78+
* @var StockItemInterface|MockObject
79+
*/
80+
private $stockItemMock;
81+
82+
/**
83+
* @var StockStatusInterface|MockObject
84+
*/
85+
private $stockStatusMock;
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
91+
protected function setUp(): void
92+
{
93+
$this->objectManager = new ObjectManager($this);
94+
95+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
96+
->disableOriginalConstructor()
97+
->getMockForAbstractClass();
98+
99+
$this->fieldMock = $this->getMockBuilder(Field::class)
100+
->disableOriginalConstructor()
101+
->getMock();
102+
103+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
104+
->disableOriginalConstructor()
105+
->getMock();
106+
107+
$this->productModelMock = $this->getMockBuilder(Product::class)
108+
->disableOriginalConstructor()
109+
->getMock();
110+
111+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)->getMock();
112+
$this->stockRegistryMock = $this->getMockBuilder(StockRegistryInterface::class)->getMock();
113+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)->getMock();
114+
$this->stockItemMock = $this->getMockBuilder(StockItemInterface::class)->getMock();
115+
$this->stockStatusMock = $this->getMockBuilder(StockStatusInterface::class)->getMock();
116+
$this->productModelMock->expects($this->any())->method('getId')
117+
->willReturn(1);
118+
$this->productModelMock->expects($this->once())->method('getStore')
119+
->willReturn($this->storeMock);
120+
$this->stockRegistryMock->expects($this->once())->method('getStockStatus')
121+
->willReturn($this->stockStatusMock);
122+
$this->storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1);
123+
124+
$this->resolver = $this->objectManager->getObject(
125+
OnlyXLeftInStockResolver::class,
126+
[
127+
'scopeConfig' => $this->scopeConfigMock,
128+
'stockRegistry' => $this->stockRegistryMock
129+
]
130+
);
131+
}
132+
133+
public function testResolve()
134+
{
135+
$xLeftInstockResult = 1;
136+
$stockCurrentQty = 3;
137+
$minQty = 2;
138+
$thresholdQty = 1;
139+
140+
$this->stockItemMock->expects($this->once())->method('getMinQty')
141+
->willReturn($minQty);
142+
$this->stockStatusMock->expects($this->once())->method('getQty')
143+
->willReturn($stockCurrentQty);
144+
$this->stockRegistryMock->expects($this->once())->method('getStockItem')
145+
->willReturn($this->stockItemMock);
146+
$this->scopeConfigMock->method('getValue')->willReturn($thresholdQty);
147+
148+
$this->assertEquals(
149+
$xLeftInstockResult,
150+
$this->resolver->resolve(
151+
$this->fieldMock,
152+
$this->contextMock,
153+
$this->resolveInfoMock,
154+
['model' => $this->productModelMock]
155+
)
156+
);
157+
}
158+
159+
public function testResolveOutStock()
160+
{
161+
$xLeftInstockResult = 1;
162+
$stockCurrentQty = 0;
163+
$minQty = 2;
164+
$thresholdQty = 1;
165+
$this->stockItemMock->expects($this->once())->method('getMinQty')
166+
->willReturn($minQty);
167+
$this->stockStatusMock->expects($this->once())->method('getQty')
168+
->willReturn($stockCurrentQty);
169+
$this->stockRegistryMock->expects($this->once())->method('getStockItem')
170+
->willReturn($this->stockItemMock);
171+
$this->scopeConfigMock->method('getValue')->willReturn($thresholdQty);
172+
173+
$this->assertEquals(
174+
null,
175+
$this->resolver->resolve(
176+
$this->fieldMock,
177+
$this->contextMock,
178+
$this->resolveInfoMock,
179+
['model' => $this->productModelMock]
180+
)
181+
);
182+
}
183+
184+
185+
186+
public function testResolveNoThresholdQty()
187+
{
188+
$xLeftInstockResult = 1;
189+
$stockCurrentQty = 3;
190+
$minQty = 2;
191+
$thresholdQty = null;
192+
$this->stockItemMock->expects($this->once())->method('getMinQty')
193+
->willReturn($minQty);
194+
$this->stockStatusMock->expects($this->once())->method('getQty')
195+
->willReturn($stockCurrentQty);
196+
$this->stockRegistryMock->expects($this->once())->method('getStockItem')
197+
->willReturn($this->stockItemMock);
198+
$this->scopeConfigMock->method('getValue')->willReturn($thresholdQty);
199+
200+
$this->assertEquals(
201+
null,
202+
$this->resolver->resolve(
203+
$this->fieldMock,
204+
$this->contextMock,
205+
$this->resolveInfoMock,
206+
['model' => $this->productModelMock]
207+
)
208+
);
209+
}
210+
}

0 commit comments

Comments
 (0)