/** * @return void */ public function setUp() { $this->config = $this->getMockBuilder('Migration\\Config')->disableOriginalConstructor()->setMethods(['getSource'])->getMock(); $this->config->expects($this->any())->method('getSource')->will($this->returnValue(['type' => DatabaseStage::SOURCE_TYPE])); $this->source = $this->getMockBuilder('Migration\\ResourceModel\\Source')->disableOriginalConstructor()->setMethods(['getDocument', 'getRecordsCount', 'getAdapter', 'addDocumentPrefix', 'getRecords'])->getMock(); $this->source->expects($this->any())->method('addDocumentPrefix')->willReturnCallback(function ($name) { return 'source_suffix_' . $name; }); $this->destination = $this->getMockBuilder('Migration\\ResourceModel\\Destination')->disableOriginalConstructor()->setMethods(['getDocument', 'getRecordsCount', 'getAdapter', 'addDocumentPrefix', 'saveRecords'])->getMock(); $this->destination->expects($this->any())->method('addDocumentPrefix')->willReturnCallback(function ($name) { return 'destination_suffix_' . $name; }); $this->progress = $this->getMockBuilder('Migration\\App\\ProgressBar\\LogLevelProcessor')->disableOriginalConstructor()->setMethods(['start', 'finish', 'advance'])->getMock(); $this->progress->expects($this->any())->method('start'); $this->progress->expects($this->any())->method('finish'); $this->progress->expects($this->any())->method('advance'); $this->recordFactory = $this->getMockBuilder('Migration\\ResourceModel\\RecordFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->logger = $this->getMockBuilder('Migration\\Logger\\Logger')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->map = $this->getMockBuilder('Migration\\Reader\\Map')->disableOriginalConstructor()->setMethods(['getDocumentMap', 'init', 'getDocumentList', 'getDestDocumentsToClear'])->getMock(); /** @var \Migration\Reader\MapFactory|\PHPUnit_Framework_MockObject_MockObject $mapFactory */ $mapFactory = $this->getMockBuilder('Migration\\Reader\\MapFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $mapFactory->expects($this->any())->method('create')->with('customer_attr_map_file')->willReturn($this->map); $this->groups = $this->getMockBuilder('Migration\\Reader\\Groups')->disableOriginalConstructor()->getMock(); $this->groups->expects($this->any())->method('getGroup')->with('source_documents')->willReturn(['source_document_1' => 'entity_id']); /** @var \Migration\Reader\GroupsFactory|\PHPUnit_Framework_MockObject_MockObject $groupsFactory */ $groupsFactory = $this->getMockBuilder('Migration\\Reader\\GroupsFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $groupsFactory->expects($this->any())->method('create')->with('customer_attr_document_groups_file')->willReturn($this->groups); $this->logger = $this->getMockBuilder('Migration\\Logger\\Logger')->disableOriginalConstructor()->setMethods(['debug'])->getMock(); $this->step = new Data($this->config, $this->source, $this->destination, $this->progress, $this->recordFactory, $mapFactory, $groupsFactory, $this->logger); }
/** * Encrypt constructor. * * @param \Magento\Framework\Encryption\Encryptor $encryptor * @param \Magento\Framework\Encryption\CryptFactory $cryptFactory * @param \Migration\Config $configReader */ public function __construct(\Magento\Framework\Encryption\Encryptor $encryptor, \Magento\Framework\Encryption\CryptFactory $cryptFactory, \Migration\Config $configReader) { $this->cryptFactory = $cryptFactory; $this->encryptor = $encryptor; $this->configReader = $configReader; $this->cryptKey = $this->configReader->getOption(self::CRYPT_KEY); }
/** * @return array */ public function getSteps() { if (empty($this->data)) { $this->data = $this->config->getSteps($this->mode); $this->createInstances(); } return $this->data; }
public function testCreate() { $adapterClassName = '\\Migration\\Resource\\Adapter\\Mysql'; $data = ['config' => ['key' => 'value']]; $adapter = $this->getMock($adapterClassName, [], [], '', false); $this->config->expects($this->once())->method('getOption')->with('resource_adapter_class_name')->will($this->returnValue(null)); $this->objectManager->expects($this->once())->method('create')->with('\\Migration\\Resource\\Adapter\\Mysql', $data)->will($this->returnValue($adapter)); $this->assertInstanceOf($adapterClassName, $this->adapterFactory->create($data)); }
/** * @return void */ public function setUp() { $this->progress = $this->getMock('\\Migration\\App\\ProgressBar\\LogLevelProcessor', ['start', 'finish', 'advance'], [], '', false); $this->logger = $this->getMock('\\Migration\\Logger\\Logger', ['debug', 'error'], [], '', false); $this->config = $this->getMock('\\Migration\\Config', [], [], '', false); $this->config->expects($this->any())->method('getSource')->willReturn(['type' => 'database', 'version' => '1.14.1.0']); $this->source = $this->getMock('\\Migration\\ResourceModel\\Source', [], [], '', false); $this->destination = $this->getMock('\\Migration\\ResourceModel\\Destination', [], [], '', false); $this->recordCollectionFactory = $this->getMock('\\Migration\\ResourceModel\\Record\\CollectionFactory', ['create'], [], '', false); $this->recordFactory = $this->getMock('\\Migration\\ResourceModel\\RecordFactory', ['create'], [], '', false); }
/** * {@inheritdoc} */ public function handle(array $record) { if (!$this->isHandling($record)) { return false; } $logFile = $this->config->getOption('log_file'); $record['formatted'] = $this->getFormatter()->format($record); if ($logFile) { $filePath = $this->getFilePath($logFile); $this->file->filePutContents($filePath, $record['formatted'] . PHP_EOL, FILE_APPEND); } return false === $this->bubble; }
/** * @return void */ public function testHandleSuccessWithoutBubble() { $extra = ['mode' => 'application mode']; $record = ['message' => $this->message, 'level' => $this->recordLevel, 'extra' => $extra]; $this->fileHandler->setLevel($this->handlerLevel); $this->fileHandler->setBubble(false); $file = 'file/path/file.log'; $this->file->expects($this->any())->method('filePutContents')->willReturn(1); $this->file->expects($this->any())->method('getRealPath')->willReturnMap([[$file, false], ['file/path', false], ['file', '/existing_path/file']]); $this->file->expects($this->once())->method('createDirectory')->willReturn(true); $this->config->expects($this->any())->method('getOption')->with('log_file')->willReturn($file); $result = $this->fileHandler->handle($record); $this->assertTrue($result); }
/** * Validating xml file * * @return $this * @throws Exception */ protected function validate() { $mapFile = $this->config->getOption(self::MAP_FILE_OPTION); $rootDir = dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR; $configFile = $rootDir . $mapFile; if (!is_file($configFile)) { throw new Exception('Invalid map filename: ' . $configFile); } $xml = file_get_contents($configFile); $document = $document = new \Magento\Framework\Config\Dom($xml, new \Magento\Framework\App\Arguments\ValidationState(\Magento\Framework\App\State::MODE_DEVELOPER)); if (!$document->validate($rootDir . 'etc/' . self::CONFIGURATION_SCHEMA)) { throw new Exception('XML file is invalid.'); } $this->xml = new \DOMXPath($document->getDom()); return $this; }
public function testDeleteDocumentBackup() { $docName = 'somename'; $this->adapter->expects($this->once())->method('deleteBackup')->with('pfx_' . $docName); $this->config->expects($this->once())->method('getOption')->with('dest_prefix')->will($this->returnValue('pfx_')); $this->resourceDestination->deleteDocumentBackup($docName); }
/** * {@inheritdoc} */ protected function initialize(InputInterface $input, OutputInterface $output) { $config = $input->getArgument(self::INPUT_KEY_CONFIG); $this->config->init($config); //@todo: interactively ask for config file $reset = $input->getOption(self::INPUT_KEY_RESET); if ($reset) { $output->writeln('Reset the current position of migration to start from the beginning'); $this->progress->reset(); } if ($output->getVerbosity() > 1) { $this->logManager->process($this->verbosityLevels[$output->getVerbosity()]); } else { $this->logManager->process(); } }
/** * @return bool */ protected function optionConfig() { $config = $this->getArg('config'); if ($config) { $this->config->init($this->getArg('config')); } return $config ? true : false; }
public function setUp() { $this->progressBar = $this->getMock('\\Migration\\App\\ProgressBar\\LogLevelProcessor', ['start', 'finish', 'advance'], [], '', false); $this->source = $this->getMock('Migration\\Resource\\Source', ['getDocument', 'getDocumentList', 'getRecords', 'getRecordsCount', 'getPageSize', 'setLastLoadedRecord'], [], '', false); $this->destination = $this->getMock('Migration\\Resource\\Destination', ['getDocument', 'getDocumentList', 'saveRecords', 'clearDocument'], [], '', false); $this->recordFactory = $this->getMock('Migration\\Resource\\RecordFactory', ['create'], [], '', false); $this->recordTransformerFactory = $this->getMock('Migration\\RecordTransformerFactory', ['create'], [], '', false); $this->map = $this->getMockBuilder('Migration\\Reader\\Map')->disableOriginalConstructor()->setMethods(['getDocumentMap', 'getHandlerConfig'])->getMock(); /** @var \Migration\Config|\PHPUnit_Framework_MockObject_MockObject $config */ $this->config = $this->getMockBuilder('Migration\\Config')->setMethods(['getOption'])->disableOriginalConstructor()->getMock(); $this->config->expects($this->any())->method('getOption')->willReturnMap([['direct_document_copy', 0], ['bulk_size', 100]]); /** @var \Migration\Reader\MapFactory|\PHPUnit_Framework_MockObject_MockObject $mapFactory */ $mapFactory = $this->getMock('\\Migration\\Reader\\MapFactory', [], [], '', false); $mapFactory->expects($this->any())->method('create')->with('map_file')->willReturn($this->map); $this->progress = $this->getMock('Migration\\App\\Progress', ['getProcessedEntities', 'addProcessedEntity'], [], '', false); $this->logger = $this->getMockBuilder('Migration\\Logger\\Logger')->disableOriginalConstructor()->setMethods(['debug'])->getMock(); $this->data = new Data($this->progressBar, $this->source, $this->destination, $this->recordFactory, $this->recordTransformerFactory, $mapFactory, $this->progress, $this->logger, $this->config); }
/** * @return void */ public function setUp() { $this->logger = $this->getMock('Migration\\Logger\\Logger', ['error'], [], '', false); $this->progress = $this->getMock('\\Migration\\App\\ProgressBar\\LogLevelProcessor', ['start', 'finish', 'advance'], [], '', false); $this->logger = $this->getMock('\\Migration\\Logger\\Logger', ['debug', 'error'], [], '', false); $this->config = $this->getMock('\\Migration\\Config', [], [], '', false); $this->config->expects($this->any())->method('getSource')->willReturn(['type' => 'database', 'version' => '1.9']); $this->source = $this->getMock('\\Migration\\ResourceModel\\Source', [], [], '', false); $select = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->setMethods(['from', 'joinLeft'])->disableOriginalConstructor()->getMock(); $select->expects($this->any())->method('from')->willReturnSelf(); $select->expects($this->any())->method('joinLeft')->willReturnSelf(); $sourceAdapter = $this->getMockBuilder('Migration\\ResourceModel\\Adapter\\Mysql')->disableOriginalConstructor()->setMethods(['getSelect', 'loadDataFromSelect'])->getMock(); $sourceAdapter->expects($this->any())->method('getSelect')->willReturn($select); $this->source->expects($this->any())->method('getAdapter')->willReturn($sourceAdapter); $this->destination = $this->getMock('\\Migration\\ResourceModel\\Destination', [], [], '', false); $this->recordCollection = $this->getMock('\\Migration\\ResourceModel\\Record\\Collection', ['addRecord'], [], '', false); $this->recordFactory = $this->getMock('\\Migration\\ResourceModel\\RecordFactory', ['create'], [], '', false); }
public function testGetRecords() { $resourceName = 'core_config_data'; $pageNumber = 2; $this->config->expects($this->at(0))->method('getOption')->with('bulk_size')->will($this->returnValue(100)); $this->config->expects($this->at(1))->method('getOption')->with('dest_prefix')->will($this->returnValue(100)); $this->adapter->expects($this->once())->method('loadPage'); $this->resourceDestination->getRecords($resourceName, $pageNumber); }
/** * @param string $sourceGridDocumentName * @return \Magento\Framework\DB\Select */ protected function getEntityIdsSelect($sourceGridDocumentName) { /** @var \Migration\ResourceModel\Adapter\Mysql $adapter */ $adapter = $this->source->getAdapter(); /** @var \Magento\Framework\DB\Select $select */ $select = $adapter->getSelect(); $schema = $this->config->getSource()['database']['name']; $select->from($this->source->addDocumentPrefix($sourceGridDocumentName), 'entity_id', $schema); return $select; }
/** * @param array $step * @param string $stepName * @throws Exception * @return void */ protected function runVolume(array $step, $stepName) { if (!$this->runStage($step['volume'], $stepName, 'volume check')) { $this->rollback($step['data'], $stepName); if ($this->configReader->getStep('delta', $stepName)) { $this->logger->warning('Volume Check failed'); } else { throw new Exception('Volume Check failed'); } } }
/** * @param array $attributeData * @param array $expected * * @dataProvider dataProviderEncryptionData * @return void */ public function testHandle($attributeData, $expected) { $fieldName = 'value'; list($key, $dbValue, $initVector, $encryptedValue, $cypher, $mode) = array_values($attributeData); list($decryptedValue, $newValue) = array_values($expected); /** @var \Migration\ResourceModel\Record|\PHPUnit_Framework_MockObject_MockObject $recordToHandle */ $recordToHandle = $this->getMock('Migration\\ResourceModel\\Record', ['getValue', 'setValue', 'getFields'], [], '', false); $recordToHandle->expects($this->once())->method('getValue')->with($fieldName)->willReturn($dbValue); $recordToHandle->expects($this->once())->method('setValue')->with($fieldName, $newValue); $recordToHandle->expects($this->once())->method('getFields')->will($this->returnValue([$fieldName])); $oppositeRecord = $this->getMockBuilder('Migration\\ResourceModel\\Record')->disableOriginalConstructor()->getMock(); $crypt = $this->getMock('\\Magento\\Framework\\Encryption\\Crypt', ['decrypt'], [$key, $cypher, $mode, $initVector], '', true); $crypt->expects($this->once())->method('decrypt')->with(base64_decode((string) $encryptedValue))->willReturn($decryptedValue); $this->configReader->expects($this->once())->method('getOption')->with(self::CRYPT_KEY)->will($this->returnValue($key)); $this->cryptFactory->expects($this->once())->method('create')->with(['key' => $key, 'cipher' => $cypher, 'mode' => $mode, 'initVector' => $initVector])->will($this->returnValue($crypt)); $this->encryptor->expects($this->once())->method('encrypt')->with($decryptedValue)->willReturn($newValue); $handler = new \Migration\Handler\Settings\Encrypt($this->encryptor, $this->cryptFactory, $this->configReader); $handler->setField($fieldName); $handler->handle($recordToHandle, $oppositeRecord); }
/** * Validating xml file * * @return $this * @throws Exception */ protected function validate() { $mapFile = $this->config->getOption(self::MAP_FILE_OPTION); $rootDir = dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR; $configFile = $rootDir . $mapFile; if (!is_file($configFile)) { throw new Exception('Invalid map filename: ' . $configFile); } $xml = file_get_contents($configFile); $document = $document = new \Magento\Framework\Config\Dom($xml, new \Magento\Framework\App\Arguments\ValidationState(\Magento\Framework\App\State::MODE_DEVELOPER)); $schemaFileName = $rootDir . 'etc/' . self::CONFIGURATION_SCHEMA; if (!$document->validate($schemaFileName)) { $errors = $document->validateDomDocument($document->getDom(), $schemaFileName, $document::ERROR_FORMAT_DEFAULT); foreach ($errors as $error) { echo $error . PHP_EOL; } throw new Exception('XML file is invalid.'); } $this->xml = new \DOMXPath($document->getDom()); return $this; }
/** * @param Config $config * @param Source $source * @param string $map * @param string $attributeCode * @throws Exception */ public function __construct(Config $config, Source $source, $map, $attributeCode) { $this->source = $source; $this->attributeCode = $attributeCode; $this->canStart = $config->getSource()['type'] == DatabaseStage::SOURCE_TYPE; if ($this->canStart) { $map = rtrim($map, ']'); $map = ltrim($map, '['); $map = explode(';', $map); $resultMap = []; foreach ($map as $mapRecord) { $explodedRecord = explode(':', trim($mapRecord)); if (count($explodedRecord) != 2) { throw new Exception('Invalid map provided to convert handler'); } list($key, $value) = $explodedRecord; $resultMap[$key] = $value; } $this->map = $resultMap; } }
public function setAttributeData(Record\Collection $destinationRecords, array $recordAttributesData, array $attributeCodes) { /** @var Record $record */ foreach ($destinationRecords as $record) { if (isset($recordAttributesData[$record->getValue('entity_id')])) { if ($this->configReader->getOption(self::UPGRADE_CUSTOMER_PASSWORD_HASH)) { $recordAttributesData = $this->upgradeCustomerHash($recordAttributesData, $record->getValue('entity_id')); } $data = $record->getData(); $data = array_merge(array_fill_keys($attributeCodes, null), $data, $recordAttributesData[$record->getValue('entity_id')]); $record->setData($data); } } }
/** * Retrieving bulk size * * @param string $documentName * @return mixed */ public function getPageSize($documentName) { if (array_key_exists($documentName, $this->documentBulkSize)) { return $this->documentBulkSize[$documentName]; } $configValue = (int) $this->configReader->getOption('bulk_size'); if ($configValue === 0) { $fields = $this->getDocument($documentName)->getStructure()->getFields(); $fieldsNumber = count($fields); $memoryLimit = $this->getBytes(ini_get('memory_limit')); $pageSize = ceil($memoryLimit / (self::MEMORY_PER_FIELD * $fieldsNumber)); } else { $pageSize = $configValue > 0 ? $configValue : self::DEFAULT_BULK_SIZE; } $pageSize = $pageSize > self::MAX_BULK_SIZE ? self::MAX_BULK_SIZE : $pageSize; $this->documentBulkSize[$documentName] = $pageSize; return $this->documentBulkSize[$documentName]; }
/** * @param Document $sourceDocument * @param Document $destinationDocument * @return bool */ protected function isCopiedDirectly(Document $sourceDocument, Document $destinationDocument) { if (!$this->copyDirectly) { return; } $result = true; $schema = $this->config->getSource()['database']['name']; /** @var \Magento\Framework\DB\Select $select */ $select = $this->source->getAdapter()->getSelect(); $select->from($this->source->addDocumentPrefix($sourceDocument->getName()), '*', $schema); try { $this->destination->getAdapter()->insertFromSelect($select, $this->destination->addDocumentPrefix($destinationDocument->getName()), array_keys($sourceDocument->getStructure()->getFields())); } catch (\Exception $e) { $this->copyDirectly = false; $this->logger->error('Document ' . $sourceDocument->getName() . ' can not be copied directly because of error: ' . $e->getMessage()); $result = false; } return $result; }
/** * Create groups from file specified in config option * * @param string $configOption * @return \Migration\Reader\Groups */ public function create($configOption) { $groupsFile = $this->config->getOption($configOption); return $this->objectManager->create($this->instanceName, ['groupsFile' => $groupsFile]); }
/** * Retrieving bulk size * * @return int */ public function getPageSize() { $pageSize = (int) $this->configReader->getOption('bulk_size'); return empty($pageSize) ? self::DEFAULT_BULK_SIZE : $pageSize; }
/** * Create class instance with specified parameters * * @param array $data * @return AdapterInterface */ public function create(array $data = []) { $classNameInConfig = $this->configReader->getOption('resource_adapter_class_name'); $className = !empty($classNameInConfig) ? $classNameInConfig : $this->defaultClassName; return $this->objectManager->create($className, $data); }
/** * @return void */ protected function initProgressBar() { $this->progressBar = $this->progressBarFactory->create($this->getOutputInstance()); $this->progressBar->setFormat($this->config->getOption(self::PROGRESS_BAR_FORMAT_OPTION)); }
/** * Check Step can be started * * @return bool */ protected function canStart() { return $this->configReader->getSource()['type'] == self::SOURCE_TYPE; }
/** * @param Config $config * @param Source $source * @throws Exception */ public function __construct(Config $config, Source $source) { $this->canStart = $config->getSource()['type'] == DatabaseStage::SOURCE_TYPE; $this->source = $source; }
public function testCreateDelta() { $this->adapter->expects($this->once())->method('createDelta')->with('spfx_document', 'spfx_m2_cl_document', 'key_field'); $this->config->expects($this->any())->method('getOption')->with(Source::CONFIG_DOCUMENT_PREFIX)->willReturn('spfx_'); $this->resourceSource->createDelta('document', 'key_field'); }
public function testGetDeletedRecords() { $this->adapter->expects($this->once())->method('loadDeletedRecords')->with('m2_cl_document', 'key_field', 0, 100); $this->config->expects($this->any())->method('getOption')->willReturnMap([['source_prefix', ''], ['bulk_size', 100]]); $this->resourceSource->getDeletedRecords('document', 'key_field'); }