/** * @param $filePath Absolute file path to CSV file */ public function setFile($filePath) { if (!file_exists($filePath)) { throw new FileNotFoundException(); } // Hacky but quick fix for https://github.com/cedricblondeau/magento2-module-catalog-import-command/issues/1 $pathInfo = pathinfo($filePath); $validate = $this->importModel->validateSource($this->csvSourceFactory->create(['file' => $pathInfo['basename'], 'directory' => $this->readFactory->create($pathInfo['dirname'])])); if (!$validate) { throw new \InvalidArgumentException(); } }
/** * @inheritdoc */ public function install() { $this->eavConfig->clear(); $importModel = $this->importModel; $importModel->setData(['entity' => 'catalog_product', 'behavior' => 'append', 'import_images_file_dir' => 'pub/media/catalog/product', Import::FIELD_NAME_VALIDATION_STRATEGY => ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS]); $source = $this->csvSourceFactory->create(['file' => 'fixtures/products.csv', 'directory' => $this->readFactory->create($this->componentRegistrar->getPath(ComponentRegistrar::MODULE, 'Magento_ConfigurableSampleData'))]); $currentPath = getcwd(); chdir(BP); $importModel->validateSource($source); $importModel->importSource(); chdir($currentPath); $this->eavConfig->clear(); $this->reindex(); }
public function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->reportHelperMock = $this->getMock('\\Magento\\ImportExport\\Helper\\Report', [], [], '', false); $this->outputCsvFactoryMock = $this->getMock('\\Magento\\ImportExport\\Model\\Export\\Adapter\\CsvFactory', ['create'], [], '', false); $this->outputCsvMock = $this->getMock('\\Magento\\ImportExport\\Model\\Export\\Adapter\\Csv', [], [], '', false); $this->outputCsvFactoryMock->expects($this->any())->method('create')->willReturn($this->outputCsvMock); $this->sourceCsvFactoryMock = $this->getMock('\\Magento\\ImportExport\\Model\\Import\\Source\\CsvFactory', ['create'], [], '', false); $this->sourceCsvMock = $this->getMock('\\Magento\\ImportExport\\Model\\Import\\Source\\Csv', [], [], '', false); $this->sourceCsvMock->expects($this->any())->method('valid')->willReturnOnConsecutiveCalls(true, true, false); $this->sourceCsvMock->expects($this->any())->method('current')->willReturnOnConsecutiveCalls([23 => 'first error'], [27 => 'second error']); $this->sourceCsvFactoryMock->expects($this->any())->method('create')->willReturn($this->sourceCsvMock); $this->filesystemMock = $this->getMock('\\Magento\\Framework\\Filesystem', [], [], '', false); $this->csvModel = $objectManager->getObject('\\Magento\\ImportExport\\Model\\Report\\Csv', ['reportHelper' => $this->reportHelperMock, 'sourceCsvFactory' => $this->sourceCsvFactoryMock, 'outputCsvFactory' => $this->outputCsvFactoryMock, 'filesystem' => $this->filesystemMock]); }
/** * @inheritdoc */ public function run() { $this->logger->log('Installing configurable products:'); $importModel = $this->importModel; $importModel->setData(['entity' => 'catalog_product', 'behavior' => 'append', 'import_images_file_dir' => 'pub/media/catalog/product', Import::FIELD_NAME_VALIDATION_STRATEGY => ProcessingErrorAggregatorInterface::VALIDATION_STRATEGY_SKIP_ERRORS]); $source = $this->csvSourceFactory->create(['file' => 'Magento/SampleData/fixtures/ConfigurableProduct/import-export_products-img.csv', 'directory' => $this->filesystem->getDirectoryWrite(DirectoryList::MODULES)]); $currentPath = getcwd(); chdir($this->filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath()); $importModel->validateSource($source); $importModel->importSource(); chdir($currentPath); $this->logger->logInline('.'); $this->eavConfig->clear(); $this->reindex(); $this->logger->logInline('.'); }
/** * @param string $sourceFile * @return \Magento\ImportExport\Model\Import\Source\Csv */ protected function createSourceCsvModel($sourceFile) { return $this->sourceCsvFactory->create(['file' => $sourceFile, 'directory' => $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)]); }