/** * @medium */ public function testImportExport() { $sourceStorage = new LocalFileStorage(new \SplFileInfo(__DIR__ . '/../../../metadata/testfiles/100.csv'), new CsvFormat()); $targetStorage = new DoctrineStorage(self::$em, 'TestEntities\\Address'); $this->assertEquals(new StorageInfo(['name' => 'SELECT o FROM TestEntities\\Address o', 'count' => 0, 'type' => 'DQL Query']), $targetStorage->info()); $importer = Importer::build($targetStorage); $importConfiguration = new ImportConfiguration(); $importRun = $importConfiguration->toRun(); $import = Import::build($importer, $sourceStorage, $importRun); $eventDispatcher = new EventDispatcher(); $importRunner = new ImportRunner(new DefaultWorkflowFactory($eventDispatcher)); $importRunner->run($import); $entities = self::$em->getRepository('TestEntities\\Address')->findAll(); //import worked $this->assertEquals(100, count($entities)); $exportFile = '/tmp/doctrine_test.csv'; @unlink($exportFile); $sourceStorage = new DoctrineStorage(self::$em, null, self::$em->createQuery("SELECT A FROM TestEntities\\Address A WHERE A.zip LIKE '2%'")); $this->assertEquals(new StorageInfo(['name' => "SELECT A FROM TestEntities\\Address A WHERE A.zip LIKE '2%'", 'count' => 10, 'type' => 'DQL Query']), $sourceStorage->info()); $targetStorage = new LocalFileStorage(new \SplFileInfo($exportFile), new CsvFormat()); $importer = Importer::build($targetStorage); $importConfiguration = new ImportConfiguration(); $importRun = $importConfiguration->toRun(); $import = Import::build($importer, $sourceStorage, $importRun); $eventDispatcher = new EventDispatcher(); $importRunner = new ImportRunner(new DefaultWorkflowFactory($eventDispatcher)); $importRunner->run($import); $this->assertFileExists($exportFile); $this->assertEquals(11, count(file($exportFile))); //+header $this->assertEquals(10, $import->getRun()->toArray()['statistics']['processed']); }
/** * @medium */ public function test() { $data = array(array('foo' => 'bar', 'baz' => array('some' => 'value'))); $targetData = array(); $sourceStorage = new ArrayStorage($data); $targetStorage = new ArrayStorage($targetData); $importer = Importer::build($targetStorage); $importConfiguration = new ImportConfiguration(); $importRun = $importConfiguration->toRun(); $import = Import::build($importer, $sourceStorage, $importRun); $import->mappings()->add('foo', 'fooloo')->add('baz', array('some' => 'else')); ImportRunner::build()->run($import); $expectedData = array(array('fooloo' => 'bar', 'baz' => array('else' => 'value'))); $this->assertEquals($expectedData, $targetData); }
/** * @dataProvider getStorages * @medium */ public function testImport($sourceFile, Format $format, Format $targetFormat = null) { if (!$targetFormat) { $targetFormat = $format; } $targetFile = tempnam('/tmp', 'test'); @unlink($targetFile); $sourceStorage = new LocalFileStorage(new \SplFileInfo($sourceFile), $format); $targetStorage = new LocalFileStorage(new \SplFileInfo($targetFile), $targetFormat); $this->assertEquals(new StorageInfo(array('name' => basename($targetFile), 'hash' => null, 'format' => $targetFormat, 'size' => 0, 'count' => 0)), $targetStorage->info()); $importer = Importer::build($targetStorage); $importConfiguration = new ImportConfiguration(); $importRun = $importConfiguration->toRun(); $import = Import::build($importer, $sourceStorage, $importRun); $importRunner = new ImportRunner(); $importRunner->run($import); $this->assertFileExists($targetFile); if ($format instanceof XmlFormat) { $this->assertXmlFileEqualsXmlFile($sourceFile, $targetFile); } elseif ($format instanceof CsvFormat) { $this->assertFileEquals($sourceFile, $targetFile); } }
/** * @return Import * * @throws InvalidConfigurationException */ public function build(ImportConfiguration $importConfiguration, StorageInterface $sourceStorage = null, $createdBy = null, $requestContext = null) { $importer = $this->importerRepository->get($importConfiguration->getImporterId()); if ($importer->getSourceStorage()) { $sourceStorage = $importer->getSourceStorage(); } elseif (!$sourceStorage) { throw new InvalidConfigurationException("Either the importRequest or the importer '" . $importConfiguration->getImporterId() . "' must have a source storage set."); } $importRun = $importConfiguration->toRun($createdBy); //apply static context from importer & request $context = $requestContext; if (!is_null($importer->getContext()) && !empty($importer->getContext())) { $importerContext = $importer->getContext(); if ($importerContext && $requestContext) { $context = array_merge($importerContext, $requestContext); } else { $context = $importerContext; } } $importRun->setContext($context); $import = $this->factorImport($importer, $sourceStorage, $importRun); //after everthing was build, apply softdata from sourcestorage to importrun //dont do this any earlier, as there might be AFTER_BUILD hooks, that may change //the sourcestorage configuration $importRun->setInfo((array) $sourceStorage->info()); return $import; }