示例#1
0
 /**
  * @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']);
 }
示例#2
0
 /**
  * @medium
  */
 public function test()
 {
     $sourceStorage = new ServiceStorage(array($this, 'readData'));
     $targetStorage = new ServiceStorage(array($this, 'writeData'));
     $importer = Importer::build($targetStorage);
     $import = Import::build($importer, $sourceStorage);
     $eventDispatcher = new EventDispatcher();
     $importRunner = new ImportRunner(new DefaultWorkflowFactory($eventDispatcher));
     $importRunner->run($import);
     $this->assertEquals(array(array('data1'), array('data2')), $this->dataWritten);
 }
 /**
  * @return \Ddeboer\DataImport\Workflow
  */
 private function buildBaseWorkflow(Import $import)
 {
     $workflow = new EventDispatchableWorkflow($import->getSourceStorage()->reader());
     $workflow->setEventDispatcher($this->eventDispatcher);
     $import->importer()->filters()->apply($workflow);
     $import->mappings()->apply($workflow, $import->importer()->transformation()->converterProvider());
     $import->importer()->validation()->apply($workflow);
     return $workflow;
 }
示例#4
0
 /**
  * @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);
 }
 /**
  * @medium
  */
 public function test()
 {
     $data1 = [['foo' => 'bar1']];
     $data2 = [['foo' => 'bar2']];
     $targetData = array();
     $targetStorage = new ArrayStorage($targetData);
     $validator = Validation::createValidator();
     $validation = new ValidatorValidation($validator);
     $validation->addSourceConstraint('foo', new Choice(['choices' => ['a']]));
     $importer = Importer::build($targetStorage);
     $importer->validation($validation);
     $importRunner = ImportRunner::build();
     $import = Import::build($importer, new ArrayStorage($data1));
     $importRunner->dryRun($import);
     $this->assertEquals(1, count($importer->validation()->getViolations()['source']));
     $import = Import::build($importer, new ArrayStorage($data2));
     $importRunner->dryRun($import);
     $this->assertEquals(1, count($importer->validation()->getViolations()['source']));
 }
示例#6
0
 /**
  * @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);
     }
 }
示例#7
0
 /**
  * @return Import
  */
 private function factorImport(Importer $importer, StorageInterface $sourceStorage, ImportRun $importRun)
 {
     $import = Import::build($importer, $sourceStorage, $importRun);
     //notify system
     if ($this->eventDispatcher) {
         $this->eventDispatcher->dispatch(ImportConfigureEvent::AFTER_BUILD, new ImportConfigureEvent($import));
         $this->eventDispatcher->dispatch(ImportConfigureEvent::AFTER_BUILD . '.' . $importRun->getConfiguration()->getImporterId(), new ImportConfigureEvent($import));
     }
     return $import;
 }
 public function onAfterWrite(ImportItemEvent $event)
 {
     ++$this->statistics['written'];
     $this->import->getRun()->setStatistics($this->statistics);
 }
示例#9
0
 /**
  * @return Import
  */
 public function run(Import $import)
 {
     $this->logger->info('Starting import run.', $import->getRun()->toArray());
     $importRun = $import->getRun();
     $workflow = $this->workflowFactory->buildRunWorkflow($import, $importRun);
     $this->process($workflow, $import);
     $this->logger->info('Import run finished.', $import->getRun()->toArray());
     return $importRun;
 }