Beispiel #1
0
 /**
  * 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());
 }
Beispiel #2
0
 /**
  * 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);
 }
Beispiel #3
0
 /**
  * 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()) {
             // errors limit check
             if ($this->_errorsCount >= $this->_errorsLimit) {
                 return $this;
             }
             $rowData = $source->current();
             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;
 }
Beispiel #4
0
 public function testGetAffectedProducts()
 {
     $testProduct = 'test_product';
     $rowData = ['data'];
     $rowNum = 666;
     $importProduct = $this->getMockBuilder('\\Magento\\CatalogImportExport\\Model\\Import\\Product')->disableOriginalConstructor()->setMethods(['isRowAllowedToImport', '_populateToUrlGeneration'])->getMock();
     $this->_dataSourceModel->expects($this->exactly(2))->method('getNextBunch')->willReturnOnConsecutiveCalls([$rowNum => $rowData], null);
     $this->setPropertyValue($importProduct, '_dataSourceModel', $this->_dataSourceModel);
     $importProduct->expects($this->once())->method('isRowAllowedToImport')->with($rowData, $rowNum)->willReturn(true);
     $importProduct->expects($this->once())->method('_populateToUrlGeneration')->with($rowData)->willReturn($testProduct);
     $this->assertEquals([$testProduct], $importProduct->getAffectedProducts());
 }
Beispiel #5
0
 /**
  * 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;
     }
 }
Beispiel #6
0
 public function testImportSource()
 {
     $entityTypeCode = 'code';
     $this->_importData->expects($this->any())->method('getEntityTypeCode')->will($this->returnValue($entityTypeCode));
     $behaviour = 'behaviour';
     $this->_importData->expects($this->once())->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->once())->method('importData')->will($this->returnSelf());
     $this->import->expects($this->once())->method('_getEntityAdapter')->will($this->returnValue($this->_entityAdapter));
     $importOnceMethodsReturnNull = ['getEntity', 'getBehavior', 'getProcessedRowsCount', 'getProcessedEntitiesCount', 'getInvalidRowsCount', 'getErrorsCount'];
     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|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()) {
             if ($this->_errorsCount >= $this->_errorsLimit) {
                 // errors limit check
                 return;
             }
             $rowData = $source->current();
             $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;
 }
Beispiel #8
0
 /**
  * Test importSource with expected exception
  *
  * @expectedException \Magento\Framework\Exception\AlreadyExistsException
  * @expectedExceptionMessage URL key for specified store already exists.
  */
 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->once())->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->once())->method('importData')->will($this->throwException($exceptionMock));
     $this->import->expects($this->once())->method('_getEntityAdapter')->will($this->returnValue($this->_entityAdapter));
     $importOnceMethodsReturnNull = ['getEntity', 'getBehavior'];
     foreach ($importOnceMethodsReturnNull as $method) {
         $this->import->expects($this->once())->method($method)->will($this->returnValue(null));
     }
     $this->import->importSource();
 }
Beispiel #9
0
 /**
  * Import data rows
  *
  * @return boolean
  */
 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 = array();
         $options = array();
         $titles = array();
         $prices = array();
         $typeValues = array();
         $typePrices = array();
         $typeTitles = array();
         foreach ($bunch as $rowNumber => $rowData) {
             if (!$this->isRowAllowedToImport($rowData, $rowNumber)) {
                 continue;
             }
             if (!$this->_parseRequiredData($rowData)) {
                 continue;
             }
             $optionData = $this->_collectOptionMainData($rowData, $prevOptionId, $nextOptionId, $products, $prices);
             if ($optionData != null) {
                 $options[] = $optionData;
             }
             $this->_collectOptionTypeData($rowData, $prevOptionId, $nextValueId, $typeValues, $typePrices, $typeTitles);
             $this->_collectOptionTitle($rowData, $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;
 }