public function test() { $eventDispatcher = new EventDispatcher(); $eventDispatcher->addListener(ImportItemEvent::AFTER_READ, array($this, 'onAfterRead')); $eventDispatcher->addListener(ImportItemEvent::AFTER_FILTER, array($this, 'onAfterFilter')); $eventDispatcher->addListener(ImportItemEvent::AFTER_CONVERSION, array($this, 'onAfterConversion')); $eventDispatcher->addListener(ImportItemEvent::AFTER_CONVERSIONFILTER, array($this, 'onAfterConversionFilter')); $eventDispatcher->addListener(ImportItemEvent::AFTER_WRITE, array($this, 'onAfterWrite')); $fileDir = __DIR__ . '/../../../../../metadata/testfiles'; $finder = Finder::create()->in($fileDir)->name('*'); $lfsp = new FinderFileStorageProvider($finder); $lfsp->setStorageFactory(new FormatDiscoverLocalFileStorageFactory(new MimeTypeDiscoverStrategy(array('text/plain' => new CsvAutoDelimiterFormatFactory())))); $storageLocator = new StorageLocator(); $storageLocator->register('defaultProvider', $lfsp); $array = array(); $targetStorage = new ArrayStorage($array); $importer = Importer::build($targetStorage); $importRepository = new ImporterRepository(); $importRepository->register('defaultImporter', $importer); $importRequest = new ImportRequest($fileDir . '/100.csv', 'defaultProvider', 'defaultImporter'); $importBuilder = new ImportBuilder($importRepository, $storageLocator); $import = $importBuilder->buildFromRequest($importRequest); $import->mappings()->add('prefix', 'Anrede', 'upperCase')->add('name', 'Name', 'lowerCase'); $importRunner = new ImportRunner(new DefaultWorkflowFactory($eventDispatcher)); $expectedResult = array('Name' => 'jennie abernathy', 'Anrede' => 'MS.', 'street' => '866 Hyatt Isle Apt. 888', 'zip' => '65982', 'city' => 'East Laurie', 'phone' => '(551)436-0391', 'email' => '*****@*****.**'); $previewResult = $importRunner->preview($import, 0); $this->assertEquals($expectedResult, $previewResult['to']); }
/** * @return Import * * @throws InvalidConfigurationException */ public function buildFromRequest(ImportRequest $importRequest) { $importerId = $importRequest->getImporterId(); $storageSelection = null; $sourceStorage = null; if ($importRequest->hasSource()) { $storageSelection = $this->storageLocator->selectStorage($importRequest->getSourceProviderId(), $importRequest->getSourceId()); $sourceStorage = $this->storageLocator->getStorage($storageSelection); if (!$importRequest->hasImporterId()) { $importerId = $this->findImporterForStorage($sourceStorage); if (!$importerId) { throw new InvalidConfigurationException('No importerId was given and there is no importer that matches the storage.'); } $importRequest->setImporterId($importerId); $this->eventDispatcher->dispatch(ImportRequestEvent::DISCOVERED, new ImportRequestEvent($importRequest)); } } $importConfiguration = new ImportConfiguration($storageSelection, $importerId); return $this->build($importConfiguration, $sourceStorage, $importRequest->getCreatedBy(), $importRequest->getContext()); }