/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage Please correct Table Rates File Format. * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function testGetDataFromEmptyFile() { $lines = []; $file = $this->createFileMock($lines); foreach ($this->import->getData($file, 1, 'short_name', 'full_name', 2) as $bunch) { $this->assertTrue(false, 'Exception about empty header is not thrown'); } }
/** * 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); } }