Esempio n. 1
0
 /**
  * @return void
  */
 public function testGetColumns()
 {
     $columns = ['column_1', 'column_2'];
     $this->rowParserMock->expects($this->once())->method('getColumns')->willReturn($columns);
     $result = $this->import->getColumns();
     $this->assertEquals($columns, $result);
 }
 /**
  * Upload table rate file and import data from it
  *
  * @param \Magento\Framework\DataObject $object
  * @throws \Magento\Framework\Exception\LocalizedException
  * @return \Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
  * @todo: this method should be refactored as soon as updated design will be provided
  * @see https://wiki.corp.x.com/display/MCOMS/Magento+Filesystem+Decisions
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function uploadAndImport(\Magento\Framework\DataObject $object)
 {
     /**
      * @var \Magento\Framework\App\Config\Value $object
      */
     if (empty($_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'])) {
         return $this;
     }
     $filePath = $_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'];
     $websiteId = $this->storeManager->getWebsite($object->getScopeId())->getId();
     $conditionName = $this->getConditionName($object);
     $file = $this->getCsvFile($filePath);
     try {
         // delete old data by website and condition name
         $condition = ['website_id = ?' => $websiteId, 'condition_name = ?' => $conditionName];
         $this->deleteByCondition($condition);
         $columns = $this->import->getColumns();
         $conditionFullName = $this->_getConditionFullName($conditionName);
         foreach ($this->import->getData($file, $websiteId, $conditionName, $conditionFullName) as $bunch) {
             $this->importData($columns, $bunch);
         }
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new \Magento\Framework\Exception\LocalizedException(__('Something went wrong while importing table rates.'));
     } finally {
         $file->close();
     }
     if ($this->import->hasErrors()) {
         $error = __('We couldn\'t import this file because of these errors: %1', implode(" \n", $this->import->getErrors()));
         throw new \Magento\Framework\Exception\LocalizedException($error);
     }
 }