/** * Test successful getEntityTypeCode() */ public function testGetEntityTypeCode() { /** @var $objectManager \Magento\TestFramework\ObjectManager */ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $expectedBunches = $objectManager->get('Magento\\Framework\\Registry')->registry('_fixture/Magento_ImportExport_Import_Data'); $this->assertEquals($expectedBunches[0]['entity'], $this->_model->getEntityTypeCode()); }
/** * Test method deleteAdvancedPricing() whether _cachedSkuToDelete property is set to null. */ public function testDeleteAdvancedPricingResetCachedSkuToDelete() { $this->setPropertyValue($this->advancedPricing, '_cachedSkuToDelete', 'some value'); $this->dataSourceModel->expects($this->at(0))->method('getNextBunch')->willReturn([]); $this->advancedPricing->deleteAdvancedPricing(); $cachedSkuToDelete = $this->getPropertyValue($this->advancedPricing, '_cachedSkuToDelete'); $this->assertNull($cachedSkuToDelete); }
/** * Validate data rows and save bunches to DB. * * @return $this|void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _saveValidatedBunches() { $source = $this->_getSource(); $currentDataSize = 0; $bunchRows = []; $startNewBunch = false; $nextRowBackup = []; $maxDataSize = $this->_resourceHelper->getMaxDataSize(); $bunchSize = $this->_importExportData->getBunchSize(); $source->rewind(); $this->_dataSourceModel->cleanBunches(); while ($source->valid() || $bunchRows) { if ($startNewBunch || !$source->valid()) { $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows); $bunchRows = $nextRowBackup; $currentDataSize = strlen(serialize($bunchRows)); $startNewBunch = false; $nextRowBackup = []; } if ($source->valid()) { try { $rowData = $source->current(); } catch (\InvalidArgumentException $e) { $this->addRowError($e->getMessage(), $this->_processedRowsCount); $this->_processedRowsCount++; $source->next(); continue; } $this->_processedRowsCount++; if ($this->validateRow($rowData, $source->key())) { // add row to bunch for save $rowData = $this->_prepareRowForDb($rowData); $rowSize = strlen($this->jsonHelper->jsonEncode($rowData)); $isBunchSizeExceeded = $bunchSize > 0 && count($bunchRows) >= $bunchSize; if ($currentDataSize + $rowSize >= $maxDataSize || $isBunchSizeExceeded) { $startNewBunch = true; $nextRowBackup = [$source->key() => $rowData]; } else { $bunchRows[$source->key()] = $rowData; $currentDataSize += $rowSize; } } $source->next(); } } return $this; }
/** * Get next bunch of validated rows. * * @return array|null */ public function getNextBunch() { $bunchRows = parent::getNextBunch(); if ($bunchRows != null) { $rows = []; foreach ($bunchRows as $rowNumber => $rowData) { $rowData = $this->_prepareRow($rowData); if ($rowData !== null) { unset($rowData['_scope']); $rows[$rowNumber] = $rowData; } } return $rows; } else { return $bunchRows; } }
/** * Test importSource with expected exception * */ public function testImportSourceException() { $exceptionMock = new \Magento\Framework\Exception\AlreadyExistsException(__('URL key for specified store already exists.')); $entityTypeCode = 'code'; $this->_importData->expects($this->any())->method('getEntityTypeCode')->will($this->returnValue($entityTypeCode)); $behaviour = 'behaviour'; $this->_importData->expects($this->any())->method('getBehavior')->will($this->returnValue($behaviour)); $this->import->expects($this->any())->method('getDataSourceModel')->will($this->returnValue($this->_importData)); $this->import->expects($this->any())->method('setData')->withConsecutive(['entity', $entityTypeCode], ['behavior', $behaviour]); $phraseClass = '\\Magento\\Framework\\Phrase'; $this->import->expects($this->any())->method('addLogComment')->with($this->isInstanceOf($phraseClass)); $this->_entityAdapter->expects($this->any())->method('importData')->will($this->throwException($exceptionMock)); $this->import->expects($this->any())->method('_getEntityAdapter')->will($this->returnValue($this->_entityAdapter)); $importOnceMethodsReturnNull = ['getBehavior']; foreach ($importOnceMethodsReturnNull as $method) { $this->import->expects($this->once())->method($method)->will($this->returnValue(null)); } $this->import->importSource(); }
/** * Validate data rows and save bunches to DB * * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _saveValidatedBunches() { $source = $this->getSource(); $bunchRows = []; $startNewBunch = false; $source->rewind(); $this->_dataSourceModel->cleanBunches(); $masterAttributeCode = $this->getMasterAttributeCode(); while ($source->valid() || count($bunchRows) || isset($entityGroup)) { if ($startNewBunch || !$source->valid()) { /* If the end approached add last validated entity group to the bunch */ if (!$source->valid() && isset($entityGroup)) { foreach ($entityGroup as $key => $value) { $bunchRows[$key] = $value; } unset($entityGroup); } $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows); $bunchRows = []; $startNewBunch = false; } if ($source->valid()) { $valid = true; try { $rowData = $source->current(); foreach ($rowData as $attrName => $element) { if (!mb_check_encoding($element, 'UTF-8')) { $valid = false; $this->addRowError(AbstractEntity::ERROR_CODE_ILLEGAL_CHARACTERS, $this->_processedRowsCount, $attrName); } } } catch (\InvalidArgumentException $e) { $valid = false; $this->addRowError($e->getMessage(), $this->_processedRowsCount); } if (!$valid) { $this->_processedRowsCount++; $source->next(); continue; } if (isset($rowData[$masterAttributeCode]) && trim($rowData[$masterAttributeCode])) { /* Add entity group that passed validation to bunch */ if (isset($entityGroup)) { foreach ($entityGroup as $key => $value) { $bunchRows[$key] = $value; } $productDataSize = strlen(serialize($bunchRows)); /* Check if the new bunch should be started */ $isBunchSizeExceeded = $this->_bunchSize > 0 && count($bunchRows) >= $this->_bunchSize; $startNewBunch = $productDataSize >= $this->_maxDataSize || $isBunchSizeExceeded; } /* And start a new one */ $entityGroup = []; } if (isset($entityGroup) && $this->validateRow($rowData, $source->key())) { /* Add row to entity group */ $entityGroup[$source->key()] = $this->_prepareRowForDb($rowData); } elseif (isset($entityGroup)) { /* In case validation of one line of the group fails kill the entire group */ unset($entityGroup); } $this->_processedRowsCount++; $source->next(); } } return $this; }
/** * Import data rows * * @return boolean * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _importData() { $this->_initProductsSku(); $nextOptionId = $this->_resourceHelper->getNextAutoincrement($this->_tables['catalog_product_option']); $nextValueId = $this->_resourceHelper->getNextAutoincrement($this->_tables['catalog_product_option_type_value']); $prevOptionId = 0; while ($bunch = $this->_dataSourceModel->getNextBunch()) { $products = []; $options = []; $titles = []; $prices = []; $typeValues = []; $typePrices = []; $typeTitles = []; $parentCount = []; $childCount = []; foreach ($bunch as $rowNumber => $rowData) { $multiRowData = $this->_getMultiRowFormat($rowData); foreach ($multiRowData as $optionData) { $combinedData = array_merge($rowData, $optionData); if (!$this->isRowAllowedToImport($combinedData, $rowNumber)) { continue; } if (!$this->_parseRequiredData($combinedData)) { continue; } $optionData = $this->_collectOptionMainData($combinedData, $prevOptionId, $nextOptionId, $products, $prices); if ($optionData != null) { $options[] = $optionData; } $this->_collectOptionTypeData($combinedData, $prevOptionId, $nextValueId, $typeValues, $typePrices, $typeTitles, $parentCount, $childCount); $this->_collectOptionTitle($combinedData, $prevOptionId, $titles); } } // Save prepared custom options data !!! if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) { $this->_deleteEntities(array_keys($products)); } if ($this->_isReadyForSaving($options, $titles, $typeValues)) { if ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) { $this->_compareOptionsWithExisting($options, $titles, $prices, $typeValues); } $this->_saveOptions($options)->_saveTitles($titles)->_savePrices($prices)->_saveSpecificTypeValues($typeValues)->_saveSpecificTypePrices($typePrices)->_saveSpecificTypeTitles($typeTitles)->_updateProducts($products); } } return true; }