Skip to content

Commit

Permalink
Merge pull request #10 from Strategery-Inc/v/stockbase-item-check-imp…
Browse files Browse the repository at this point in the history
…rovements

Stockbase product check fool-proof and small refactorings
  • Loading branch information
gsomoza authored Nov 26, 2018
2 parents f8a83a6 + 07188f7 commit 9a4ef29
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
3 changes: 1 addition & 2 deletions Helper/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public function __construct(
/**
* Saves images array from stockbase for given $ean
*
* @param array $images
*
* @param array $images
* @param StockbaseClient $client
* @return bool
*/
Expand Down
51 changes: 30 additions & 21 deletions Model/Inventory/StockbaseStockManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace Stockbase\Integration\Model\Inventory;

use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Quote\Model\Quote\Item as QuoteItem;
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
Expand All @@ -26,9 +28,9 @@ class StockbaseStockManagement
*/
private $config;
/**
* @var ProductFactory
* @var ProductRepositoryInterface
*/
private $productFactory;
private $productRepository;
/**
* @var StockItemResource
*/
Expand All @@ -40,23 +42,23 @@ class StockbaseStockManagement

/**
* StockbaseStockManagement constructor.
* @param StockRegistryInterface $stockRegistry
* @param StockbaseConfiguration $config
* @param ProductFactory $productFactory
* @param StockItemResource $stockItemResource
* @param ObjectManagerInterface $objectManager
* @param StockRegistryInterface $stockRegistry
* @param StockbaseConfiguration $config
* @param ProductRepositoryInterface $productRepository
* @param StockItemResource $stockItemResource
* @param ObjectManagerInterface $objectManager
*/
public function __construct(
StockRegistryInterface $stockRegistry,
StockbaseConfiguration $config,
ProductFactory $productFactory,
ProductRepositoryInterface $productRepository,
StockItemResource $stockItemResource,
ObjectManagerInterface $objectManager
) {

$this->stockRegistry = $stockRegistry;
$this->config = $config;
$this->productFactory = $productFactory;
$this->productRepository = $productRepository;
$this->stockItemResource = $stockItemResource;
$this->objectManager = $objectManager;
}
Expand Down Expand Up @@ -87,17 +89,24 @@ public function getStockbaseStockAmount($productId)
*/
public function getStockbaseEan($productId)
{
$stockItem = $this->stockRegistry->getStockItem($productId);
if ($this->config->isModuleEnabled() &&
$stockItem->getManageStock() &&
$stockItem->getBackorders() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
) {
$product = $this->productFactory->create();
$product->load($productId);
$ean = $product->getData($this->config->getEanFieldName());

if ($product->getData('stockbase_product') && !empty($ean)) {
return $ean;
if ($this->config->isModuleEnabled()) {
$stockItem = $this->stockRegistry->getStockItem($productId);
if ($stockItem->getManageStock() &&
$stockItem->getBackorders() == \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
) {
try {
/** @var Product $product */
$product = $this->productRepository->getById($productId);

if (!$product->isComposite() && !$product->isVirtual()) {
$ean = $product->getData($this->config->getEanFieldName());
if (!empty($ean) && $product->getData('stockbase_product')) {
return $ean;
}
}
} catch (NoSuchEntityException $e) {
return null;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion StockbaseApi/Client/StockbaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public function getImages(array $eans)
/**
* Downloads a file using current client configuration and saves it at the specified destination.
*
* @param string|\GuzzleHttp\Url $uri File URI.
* @param string|\GuzzleHttp\Url $uri File URI.
* @param string|resource|\GuzzleHttp\Stream\StreamInterface $destination Destination where the file should be saved to.
* @return null
*/
public function downloadImage($uri, $destination)
{
Expand Down
17 changes: 8 additions & 9 deletions Test/Unit/Model/Inventory/StockbaseStockManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Stockbase\Integration\Test\Unit\Model\Inventory;

use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
Expand All @@ -27,8 +27,8 @@ class StockbaseStockManagementTest extends TestCase
/** @var StockbaseConfiguration|\PHPUnit_Framework_MockObject_MockObject */
private $config;

/** @var ProductFactory|\PHPUnit_Framework_MockObject_MockObject */
private $productFactory;
/** @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
private $productRepository;

/** @var StockItemResource|\PHPUnit_Framework_MockObject_MockObject */
private $stockItemResource;
Expand All @@ -55,10 +55,7 @@ public function setUp()

$this->product = $this->createMock(\Magento\Catalog\Model\Product::class);

$this->productFactory = $this->getMockBuilder('\Magento\Catalog\Model\ProductFactory')
->setMethods(['create'])
->getMock();
$this->productFactory->method('create')->willReturn($this->product);
$this->productRepository = $this->createMock(ProductRepositoryInterface::class);

$this->stockItemResource = $this->createMock(StockItemResource::class);

Expand Down Expand Up @@ -246,7 +243,7 @@ protected function createModel()
return new StockbaseStockManagement(
$this->stockRegistry,
$this->config,
$this->productFactory,
$this->productRepository,
$this->stockItemResource,
$this->objectManager
);
Expand All @@ -262,7 +259,9 @@ protected function configureStockbaseEan($productId, $ean)
$this->stockItem->expects($this->once())->method('getBackorders')
->willReturn(\Magento\CatalogInventory\Model\Stock::BACKORDERS_NO);

$this->product->expects($this->once())->method('load')->with($productId);
$this->productRepository->expects($this->once())->method('getById')->with($productId)
->willReturn($this->product);

$this->product->expects(new MethodInvokedAtIndex(0))->method('getData')->with('ean')->willReturn($ean);
$this->product->expects(new MethodInvokedAtIndex(1))->method('getData')->with('stockbase_product')
->willReturn(true);
Expand Down

0 comments on commit 9a4ef29

Please sign in to comment.