public function testSave()
 {
     $productId = 1;
     $this->stockItemMock->expects($this->any())->method('getProductId')->willReturn($productId);
     $this->productMock->expects($this->once())->method('load')->with($productId)->willReturnSelf();
     $this->productMock->expects($this->once())->method('getId')->willReturn($productId);
     $this->productMock->expects($this->once())->method('getTypeId')->willReturn('typeId');
     $this->stockConfigurationMock->expects($this->once())->method('isQty')->with('typeId')->willReturn(true);
     $this->stockStateProviderMock->expects($this->once())->method('verifyStock')->with($this->stockItemMock)->willReturn(false);
     $this->stockItemMock->expects($this->once())->method('getManageStock')->willReturn(true);
     $this->stockItemMock->expects($this->once())->method('setIsInStock')->with(false)->willReturnSelf();
     $this->stockItemMock->expects($this->once())->method('setStockStatusChangedAutomaticallyFlag')->with(true)->willReturnSelf();
     $this->stockItemMock->expects($this->any())->method('setLowStockDate')->willReturnSelf();
     $this->stockStateProviderMock->expects($this->once())->method('verifyNotification')->with($this->stockItemMock)->willReturn(true);
     $this->stockItemMock->expects($this->atLeastOnce())->method('setStockStatusChangedAuto')->willReturnSelf();
     $this->stockItemMock->expects($this->once())->method('hasStockStatusChangedAutomaticallyFlag')->willReturn(true);
     $this->stockItemMock->expects($this->once())->method('getStockStatusChangedAutomaticallyFlag')->willReturn(true);
     $this->stockItemMock->expects($this->once())->method('getWebsiteId')->willReturn(1);
     $this->stockItemMock->expects($this->once())->method('setWebsiteId')->with(1)->willReturnSelf();
     $this->stockItemMock->expects($this->once())->method('getStockId')->willReturn(1);
     $this->stockItemMock->expects($this->once())->method('setStockId')->with(1)->willReturnSelf();
     $this->stockItemResourceMock->expects($this->once())->method('save')->with($this->stockItemMock)->willReturnSelf();
     $this->indexProcessorMock->expects($this->once())->method('reindexRow')->with($productId);
     $this->assertEquals($this->stockItemMock, $this->model->save($this->stockItemMock));
 }
 /**
  * @param int $productId
  * @param float $itemQty
  * @param float $qtyToCheck
  * @param float $origQty
  * @param int $scopeId
  * @return int
  */
 public function checkQuoteItemQty($productId, $itemQty, $qtyToCheck, $origQty, $scopeId = null)
 {
     if ($scopeId === null) {
         $scopeId = $this->stockConfiguration->getDefaultScopeId();
     }
     $stockItem = $this->stockRegistryProvider->getStockItem($productId, $scopeId);
     return $this->stockStateProvider->checkQuoteItemQty($stockItem, $itemQty, $qtyToCheck, $origQty);
 }
 /**
  * @param bool $isChildItem
  * @param string $expectedMsg
  * @dataProvider checkQtyIncrementsMsgDataProvider
  */
 public function testCheckQtyIncrementsMsg($isChildItem, $expectedMsg)
 {
     $qty = 1;
     $qtyIncrements = 5;
     $stockItem = $this->getMockBuilder('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface')->disableOriginalConstructor()->setMethods($this->stockItemMethods)->getMockForAbstractClass();
     $stockItem->expects($this->any())->method('getSuppressCheckQtyIncrements')->willReturn(false);
     $stockItem->expects($this->any())->method('getQtyIncrements')->willReturn($qtyIncrements);
     $stockItem->expects($this->any())->method('getIsChildItem')->willReturn($isChildItem);
     $stockItem->expects($this->any())->method('getProductName')->willReturn('Simple Product');
     $this->mathDivision->expects($this->any())->method('getExactDivision')->willReturn(1);
     $result = $this->stockStateProvider->checkQtyIncrements($stockItem, $qty);
     $this->assertTrue($result->getHasError());
     $this->assertEquals($expectedMsg, $result->getMessage()->render());
 }
 protected function setUp()
 {
     $this->objectManagerHelper = new ObjectManagerHelper($this);
     $this->stock = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockInterface', [], [], '', false);
     $this->stockItem = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], [], '', false);
     $this->stockStatus = $this->getMock('\\Magento\\CatalogInventory\\Api\\Data\\StockStatusInterface', [], [], '', false);
     $this->objectResult = $this->getMock('\\Magento\\Framework\\Object', [], [], '', false);
     $this->stockStateProvider = $this->getMock('Magento\\CatalogInventory\\Model\\Spi\\StockStateProviderInterface', ['verifyStock', 'verifyNotification', 'checkQty', 'suggestQty', 'getStockQty', 'checkQtyIncrements', 'checkQuoteItemQty'], [], '', false);
     $this->stockStateProvider->expects($this->any())->method('verifyStock')->willReturn(true);
     $this->stockStateProvider->expects($this->any())->method('verifyNotification')->willReturn(true);
     $this->stockStateProvider->expects($this->any())->method('checkQty')->willReturn(true);
     $this->stockStateProvider->expects($this->any())->method('suggestQty')->willReturn($this->qty);
     $this->stockStateProvider->expects($this->any())->method('getStockQty')->willReturn($this->qty);
     $this->stockStateProvider->expects($this->any())->method('checkQtyIncrements')->willReturn($this->objectResult);
     $this->stockStateProvider->expects($this->any())->method('checkQuoteItemQty')->willReturn($this->objectResult);
     $this->stockRegistryProvider = $this->getMock('Magento\\CatalogInventory\\Model\\Spi\\StockRegistryProviderInterface', ['getStock', 'getStockItem', 'getStockStatus'], [], '', false);
     $this->stockRegistryProvider->expects($this->any())->method('getStock')->will($this->returnValue($this->stock));
     $this->stockRegistryProvider->expects($this->any())->method('getStockItem')->will($this->returnValue($this->stockItem));
     $this->stockRegistryProvider->expects($this->any())->method('getStockStatus')->will($this->returnValue($this->stockStatus));
     $this->stockState = $this->objectManagerHelper->getObject('\\Magento\\CatalogInventory\\Model\\StockState', ['stockStateProvider' => $this->stockStateProvider, 'stockRegistryProvider' => $this->stockRegistryProvider]);
 }
Beispiel #5
0
 /**
  * Tests that an import will still work with an invalid import line and
  * SKU data.
  *
  * In this case, the second product data has an invalid attribute set.
  *
  * @magentoDbIsolation enabled
  */
 public function testInvalidSkuLink()
 {
     $this->_stockStateProvider->method('verifyStock')->willReturn(true);
     // import data from CSV file
     $pathToFile = __DIR__ . '/_files/products_to_import_invalid_attribute_set.csv';
     $filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Framework\\Filesystem');
     $directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
     $source = new \Magento\ImportExport\Model\Import\Source\Csv($pathToFile, $directory);
     $this->_model->setSource($source)->setParameters(['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND])->isDataValid();
     $this->_model->importData();
     $productCollection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Resource\\Product\\Collection');
     $products = [];
     /** @var $product \Magento\Catalog\Model\Product */
     foreach ($productCollection as $product) {
         $products[$product->getSku()] = $product;
     }
     $this->assertArrayHasKey("simple1", $products, "Simple Product should have been imported");
     $this->assertArrayHasKey("simple3", $products, "Simple Product 3 should have been imported");
     $this->assertArrayNotHasKey("simple2", $products, "Simple Product2 should not have been imported");
     $upsellProductIds = $products["simple3"]->getUpSellProductIds();
     $this->assertEquals(0, count($upsellProductIds), "There should not be any linked upsell SKUs. The original" . " product SKU linked does not import cleanly.");
 }
 /**
  * @inheritdoc
  */
 public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem)
 {
     try {
         /** @var \Magento\Catalog\Model\Product $product */
         $product = $this->productFactory->create();
         $product->load($stockItem->getProductId());
         if (!$product->getId()) {
             return $stockItem;
         }
         $typeId = $product->getTypeId() ?: $product->getTypeInstance()->getTypeId();
         $isQty = $this->stockConfiguration->isQty($typeId);
         if ($isQty) {
             $isInStock = $this->stockStateProvider->verifyStock($stockItem);
             if ($stockItem->getManageStock() && !$isInStock) {
                 $stockItem->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true);
             }
             // if qty is below notify qty, update the low stock date to today date otherwise set null
             $stockItem->setLowStockDate(null);
             if ($this->stockStateProvider->verifyNotification($stockItem)) {
                 $stockItem->setLowStockDate((new \DateTime())->format('Y-m-d H:i:s'));
             }
             $stockItem->setStockStatusChangedAuto(0);
             if ($stockItem->hasStockStatusChangedAutomaticallyFlag()) {
                 $stockItem->setStockStatusChangedAuto((int) $stockItem->getStockStatusChangedAutomaticallyFlag());
             }
         } else {
             $stockItem->setQty(0);
         }
         $stockItem->setWebsiteId($stockItem->getWebsiteId());
         $stockItem->setStockId($stockItem->getStockId());
         $this->resource->save($stockItem);
         $this->indexProcessor->reindexRow($stockItem->getProductId());
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__($exception->getMessage()));
     }
     return $stockItem;
 }
Beispiel #7
0
 /**
  * Stock item saving.
  *
  * @return $this
  */
 protected function _saveStockItem()
 {
     $indexer = $this->indexerRegistry->get('catalog_product_category');
     /** @var $stockResource \Magento\CatalogInventory\Model\ResourceModel\Stock\Item */
     $stockResource = $this->_stockResItemFac->create();
     $entityTable = $stockResource->getMainTable();
     while ($bunch = $this->_dataSourceModel->getNextBunch()) {
         $stockData = [];
         $productIdsToReindex = [];
         // Format bunch to stock data rows
         foreach ($bunch as $rowNum => $rowData) {
             if (!$this->isRowAllowedToImport($rowData, $rowNum)) {
                 continue;
             }
             $row = [];
             $row['product_id'] = $this->skuProcessor->getNewSku($rowData[self::COL_SKU])['entity_id'];
             $productIdsToReindex[] = $row['product_id'];
             $row['website_id'] = $this->stockConfiguration->getDefaultWebsiteId();
             $row['stock_id'] = $this->stockRegistry->getStock($row['website_id'])->getStockId();
             $stockItemDo = $this->stockRegistry->getStockItem($row['product_id'], $row['website_id']);
             $existStockData = $stockItemDo->getData();
             $row = array_merge($this->defaultStockData, array_intersect_key($existStockData, $this->defaultStockData), array_intersect_key($rowData, $this->defaultStockData), $row);
             if ($this->stockConfiguration->isQty($this->skuProcessor->getNewSku($rowData[self::COL_SKU])['type_id'])) {
                 $stockItemDo->setData($row);
                 $row['is_in_stock'] = $this->stockStateProvider->verifyStock($stockItemDo);
                 if ($this->stockStateProvider->verifyNotification($stockItemDo)) {
                     $row['low_stock_date'] = $this->_localeDate->date(null, null, false)->format('Y-m-d H:i:s');
                 }
                 $row['stock_status_changed_auto'] = (int) (!$this->stockStateProvider->verifyStock($stockItemDo));
             } else {
                 $row['qty'] = 0;
             }
             $stockData[] = $row;
         }
         // Insert rows
         if (!empty($stockData)) {
             $this->_connection->insertOnDuplicate($entityTable, $stockData);
         }
         if ($productIdsToReindex) {
             $indexer->reindexList($productIdsToReindex);
         }
     }
     return $this;
 }
Beispiel #8
0
 /**
  * @param StockItemInterface $stockItem
  * @param mixed $expectedResult
  * @dataProvider checkQuoteItemQtyDataProvider
  */
 public function testCheckQuoteItemQty(StockItemInterface $stockItem, $expectedResult)
 {
     $this->assertEquals($expectedResult, $this->stockStateProvider->checkQuoteItemQty($stockItem, $this->qty, $this->qty, $this->qty)->getHasError());
 }