Skip to content

Commit 964105c

Browse files
committed
Another refactor, optimal way for receiving the object
1 parent 9a1b781 commit 964105c

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

app/code/Magento/Wishlist/Model/Wishlist.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Catalog\Api\ProductRepositoryInterface;
1313
use Magento\Catalog\Model\Product;
1414
use Magento\Catalog\Model\ProductFactory;
15+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
16+
use Magento\CatalogInventory\Api\StockRegistryInterface;
1517
use Magento\CatalogInventory\Model\Configuration;
1618
use Magento\Framework\App\Config\ScopeConfigInterface;
1719
use Magento\Framework\App\ObjectManager;
@@ -152,6 +154,11 @@ class Wishlist extends AbstractModel implements IdentityInterface
152154
*/
153155
private $scopeConfig;
154156

157+
/**
158+
* @var StockRegistryInterface|null
159+
*/
160+
private $stockRegistry;
161+
155162
/**
156163
* Constructor
157164
*
@@ -172,6 +179,7 @@ class Wishlist extends AbstractModel implements IdentityInterface
172179
* @param bool $useCurrentWebsite
173180
* @param array $data
174181
* @param Json|null $serializer
182+
* @param StockRegistryInterface|null $stockRegistry
175183
* @param ScopeConfigInterface|null $scopeConfig
176184
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
177185
*/
@@ -193,6 +201,7 @@ public function __construct(
193201
$useCurrentWebsite = true,
194202
array $data = [],
195203
Json $serializer = null,
204+
StockRegistryInterface $stockRegistry = null,
196205
ScopeConfigInterface $scopeConfig = null
197206
) {
198207
$this->_useCurrentWebsite = $useCurrentWebsite;
@@ -209,6 +218,7 @@ public function __construct(
209218
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
210219
$this->productRepository = $productRepository;
211220
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
221+
$this->stockRegistry = $stockRegistry ?: ObjectManager::getInstance()->get(StockRegistryInterface::class);
212222
}
213223

214224
/**
@@ -447,7 +457,7 @@ public function addNewItem($product, $buyRequest = null, $forciblySetQty = false
447457
throw new LocalizedException(__('Cannot specify product.'));
448458
}
449459

450-
if ($this->isInStock($product)) {
460+
if ($this->isInStock($productId)) {
451461
throw new LocalizedException(__('Cannot add product without stock to wishlist.'));
452462
}
453463

@@ -642,13 +652,13 @@ public function isSalable()
642652
/**
643653
* Retrieve if product has stock or config is set for showing out of stock products
644654
*
645-
* @param \Magento\Catalog\Api\Data\ProductInterface $product
655+
* @param int $productId
646656
* @return bool
647657
*/
648-
public function isInStock(\Magento\Catalog\Api\Data\ProductInterface $product)
658+
private function isInStock($productId)
649659
{
650-
/** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
651-
$stockItem = $product->getExtensionAttributes()->getStockItem();
660+
/** @var StockItemInterface $stockItem */
661+
$stockItem = $this->stockRegistry->getStockItem($productId);
652662
$showOutOfStock = $this->scopeConfig->isSetFlag(
653663
Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
654664
ScopeInterface::SCOPE_STORE

app/code/Magento/Wishlist/Test/Unit/Model/WishlistTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Catalog\Model\Product\Type\AbstractType;
1313
use Magento\Catalog\Model\ProductFactory;
14+
use Magento\CatalogInventory\Api\StockRegistryInterface;
1415
use Magento\CatalogInventory\Model\Stock\Item as StockItem;
1516
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
1617
use Magento\Framework\App\Config\ScopeConfigInterface;
@@ -123,12 +124,12 @@ class WishlistTest extends TestCase
123124
/**
124125
* @var StockItemRepository|PHPUnit_Framework_MockObject_MockObject
125126
*/
126-
private $stockItemRepository;
127+
private $scopeConfig;
127128

128129
/**
129-
* @var StockItemRepository|PHPUnit_Framework_MockObject_MockObject
130+
* @var StockRegistryInterface|PHPUnit_Framework_MockObject_MockObject
130131
*/
131-
private $scopeConfig;
132+
private $stockRegistry;
132133

133134
protected function setUp()
134135
{
@@ -177,7 +178,7 @@ protected function setUp()
177178
->disableOriginalConstructor()
178179
->getMock();
179180
$this->productRepository = $this->createMock(ProductRepositoryInterface::class);
180-
$this->stockItemRepository = $this->createMock(StockItemRepository::class);
181+
$this->stockRegistry = $this->createMock(StockRegistryInterface::class);
181182
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
182183

183184
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)
@@ -209,7 +210,7 @@ protected function setUp()
209210
false,
210211
[],
211212
$this->serializer,
212-
$this->stockItemRepository,
213+
$this->stockRegistry,
213214
$this->scopeConfig
214215
);
215216
}
@@ -280,7 +281,9 @@ public function testUpdateItem($itemId, $buyRequest, $param)
280281

281282
$stockItem = $this->getMockBuilder(StockItem::class)->disableOriginalConstructor()->getMock();
282283
$stockItem->expects($this->any())->method('getIsInStock')->will($this->returnValue(true));
283-
$this->stockItemRepository->expects($this->any())->method('get')->will($this->returnValue($stockItem));
284+
$this->stockRegistry->expects($this->any())
285+
->method('getStockItem')
286+
->will($this->returnValue($stockItem));
284287

285288
$instanceType = $this->getMockBuilder(AbstractType::class)
286289
->disableOriginalConstructor()
@@ -289,9 +292,7 @@ public function testUpdateItem($itemId, $buyRequest, $param)
289292
->method('processConfiguration')
290293
->will(
291294
$this->returnValue(
292-
$this->getMockBuilder(
293-
Product::class
294-
)->disableOriginalConstructor()->getMock()
295+
$this->getMockBuilder(Product::class)->disableOriginalConstructor()->getMock()
295296
)
296297
);
297298

@@ -413,7 +414,10 @@ function ($value) {
413414
StockItem::class
414415
)->disableOriginalConstructor()->getMock();
415416
$stockItem->expects($this->any())->method('getIsInStock')->will($this->returnValue(true));
416-
$this->stockItemRepository->expects($this->any())->method('get')->will($this->returnValue($stockItem));
417+
418+
$this->stockRegistry->expects($this->any())
419+
->method('getStockItem')
420+
->will($this->returnValue($stockItem));
417421

418422
$this->assertEquals($result, $this->wishlist->addNewItem($productMock, $buyRequest));
419423
}

0 commit comments

Comments
 (0)