Example #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']);
 }
 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']);
 }
Example #3
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);
 }
Example #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']));
 }
 /**
  * @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);
     }
 }