/**
  * @return bool
  */
 public function perform()
 {
     $sourceDocuments = $this->source->getDocumentList();
     $this->progressBar->start(count($sourceDocuments));
     foreach ($sourceDocuments as $sourceDocName) {
         $this->progressBar->advance();
         $destinationName = $this->map->getDocumentMap($sourceDocName, MapInterface::TYPE_SOURCE);
         if (!$destinationName) {
             continue;
         }
         $distinctFields = $this->helper->getFieldsUpdateOnDuplicate($destinationName);
         $sourceCount = $this->source->getRecordsCount($sourceDocName, true, $distinctFields);
         $destinationCount = $this->destination->getRecordsCount($destinationName);
         if ($sourceCount != $destinationCount) {
             $this->errors[] = 'Mismatch of entities in the document: ' . $destinationName;
         }
     }
     $this->progressBar->finish();
     return $this->checkForErrors();
 }
 /**
  * @return void
  */
 public function testPerformFailed()
 {
     $sourceDocName = 'core_config_data';
     $dstDocName = 'config_data';
     $this->source->expects($this->once())->method('getDocumentList')->willReturn([$sourceDocName]);
     $this->map->expects($this->once())->method('getDocumentMap')->willReturn($dstDocName);
     $this->source->expects($this->once())->method('getRecordsCount')->willReturn(2);
     $this->destination->expects($this->once())->method('getRecordsCount')->willReturn(3);
     $this->logger->expects($this->once())->method('addRecord')->with(Logger::WARNING, 'Mismatch of entities in the document: ' . $dstDocName);
     $this->helper->expects($this->once())->method('getFieldsUpdateOnDuplicate')->with($dstDocName)->willReturn(false);
     $this->assertFalse($this->volume->perform());
 }
示例#3
0
 /**
  * @return bool
  */
 public function perform()
 {
     $this->progressBar->start(count($this->source->getDocumentList()), LogManager::LOG_LEVEL_INFO);
     $sourceDocuments = $this->source->getDocumentList();
     $stage = 'run';
     $processedDocuments = $this->progress->getProcessedEntities($this, $stage);
     foreach (array_diff($sourceDocuments, $processedDocuments) as $sourceDocName) {
         $this->progressBar->advance(LogManager::LOG_LEVEL_INFO);
         $sourceDocument = $this->source->getDocument($sourceDocName);
         $destinationName = $this->map->getDocumentMap($sourceDocName, MapInterface::TYPE_SOURCE);
         if (!$destinationName) {
             continue;
         }
         $destDocument = $this->destination->getDocument($destinationName);
         $this->destination->clearDocument($destinationName);
         $this->logger->debug('migrating', ['table' => $sourceDocName]);
         $recordTransformer = $this->getRecordTransformer($sourceDocument, $destDocument);
         $doCopy = $recordTransformer === null && $this->copyDirectly;
         if ($doCopy && $this->isCopiedDirectly($sourceDocument, $destDocument)) {
             $this->progressBar->start(1, LogManager::LOG_LEVEL_DEBUG);
         } else {
             $pageNumber = 0;
             $this->progressBar->start(ceil($this->source->getRecordsCount($sourceDocName) / $this->source->getPageSize($sourceDocName)), LogManager::LOG_LEVEL_DEBUG);
             while (!empty($items = $this->source->getRecords($sourceDocName, $pageNumber))) {
                 $pageNumber++;
                 $destinationRecords = $destDocument->getRecords();
                 foreach ($items as $data) {
                     if ($recordTransformer) {
                         /** @var Record $record */
                         $record = $this->recordFactory->create(['document' => $sourceDocument, 'data' => $data]);
                         /** @var Record $destRecord */
                         $destRecord = $this->recordFactory->create(['document' => $destDocument]);
                         $recordTransformer->transform($record, $destRecord);
                     } else {
                         $destRecord = $this->recordFactory->create(['document' => $destDocument, 'data' => $data]);
                     }
                     $destinationRecords->addRecord($destRecord);
                 }
                 $this->source->setLastLoadedRecord($sourceDocName, end($items));
                 $this->progressBar->advance(LogManager::LOG_LEVEL_DEBUG);
                 $fieldsUpdateOnDuplicate = $this->helper->getFieldsUpdateOnDuplicate($destinationName);
                 $this->destination->saveRecords($destinationName, $destinationRecords, $fieldsUpdateOnDuplicate);
             }
         }
         $this->source->setLastLoadedRecord($sourceDocName, []);
         $this->progress->addProcessedEntity($this, $stage, $sourceDocName);
         $this->progressBar->finish(LogManager::LOG_LEVEL_DEBUG);
     }
     $this->progressBar->finish(LogManager::LOG_LEVEL_INFO);
     return true;
 }
 /**
  * @return void
  */
 public function testPerform()
 {
     $sourceDocName = 'core_config_data';
     $this->source->expects($this->any())->method('getDocumentList')->will($this->returnValue([$sourceDocName]));
     $this->source->expects($this->any())->method('getRecordsCount')->will($this->returnValue(2));
     $dstDocName = 'config_data';
     $this->progress->expects($this->once())->method('getProcessedEntities')->will($this->returnValue([]));
     $this->map->expects($this->once())->method('getDocumentMap')->will($this->returnValue($dstDocName));
     $this->map->expects($this->any())->method('getHandlerConfig')->willReturn(['class' => 'Handler\\Class']);
     $sourceDocument = $this->getMock('\\Migration\\ResourceModel\\Document', ['getRecords', 'getStructure'], [], '', false);
     $this->source->expects($this->once())->method('getDocument')->will($this->returnValue($sourceDocument));
     $destinationDocument = $this->getMockBuilder('\\Migration\\ResourceModel\\Document')->disableOriginalConstructor()->setMethods(['getStructure', 'getRecords'])->getMock();
     $this->destination->expects($this->once())->method('getDocument')->will($this->returnValue($destinationDocument));
     $structure = $this->getMockBuilder('\\Migration\\ResourceModel\\Structure')->disableOriginalConstructor()->setMethods(['getFields'])->getMock();
     $structure->expects($this->any())->method('getFields')->willReturn(['field' => []]);
     $sourceDocument->expects($this->any())->method('getStructure')->willReturn($structure);
     $destinationDocument->expects($this->any())->method('getStructure')->willReturn($structure);
     $recordTransformer = $this->getMock('Migration\\RecordTransformer', ['init', 'transform'], [], '', false);
     $this->recordTransformerFactory->expects($this->once())->method('create')->will($this->returnValue($recordTransformer));
     $recordTransformer->expects($this->once())->method('init');
     $bulk = [['id' => 4, 'name' => 'john']];
     $this->source->expects($this->any())->method('getRecords')->willReturnOnConsecutiveCalls($bulk, []);
     $this->source->expects($this->any())->method('setLastLoadedRecord')->withConsecutive([$sourceDocName, $bulk[0]], [$sourceDocName, []]);
     $this->source->expects($this->any())->method('getPageSize')->willReturn(100);
     $destinationRecords = $this->getMock('\\Migration\\ResourceModel\\Record\\Collection', [], [], '', false);
     $destinationDocument->expects($this->once())->method('getRecords')->will($this->returnValue($destinationRecords));
     $srcRecord = $this->getMock('\\Migration\\ResourceModel\\Record', [], [], '', false);
     $dstRecord = $this->getMock('\\Migration\\ResourceModel\\Record', [], [], '', false);
     $this->recordFactory->expects($this->at(0))->method('create')->will($this->returnValue($srcRecord));
     $this->recordFactory->expects($this->at(1))->method('create')->will($this->returnValue($dstRecord));
     $recordTransformer->expects($this->once())->method('transform')->with($srcRecord, $dstRecord);
     $this->destination->expects($this->once())->method('saveRecords')->with($dstDocName, $destinationRecords);
     $this->destination->expects($this->once())->method('clearDocument')->with($dstDocName);
     $this->logger->expects($this->any())->method('debug')->with('migrating', ['table' => $sourceDocName])->willReturn(true);
     $this->helper->expects($this->once())->method('getFieldsUpdateOnDuplicate')->with($dstDocName)->willReturn(false);
     $this->data->perform();
 }
 /**
  * @return void
  */
 public function testGetFieldsUpdateOnDuplicate()
 {
     $this->assertEquals($this->fieldsUpdateOnDuplicate['fields'], $this->helper->getFieldsUpdateOnDuplicate($this->fieldsUpdateOnDuplicate['document']));
 }