/** * calling from application will trigger execution */ public function testCallingFromApplicationWillTriggerExecution() { $application = new \MySQLExtractor\Application(); $application->processServer($this->mysqlCredentials); $helper = new \PHPUnitProtectedHelper($application); $extractor = $helper->getValue('extractor'); $databases = $extractor->databases(); $this->assertEquals(1, count($databases)); // will filter based on the mysqlCredentials->dbname $this->assertInstanceOf('\\MySQLExtractor\\Presentation\\Database', $databases['database1']); }
protected function execute(InputInterface $input, OutputInterface $output) { $notFound = false; $source = $input->getArgument('source'); $destination = $input->getArgument('destination'); $worker1 = new \MySQLExtractor\Application(); $worker2 = new \MySQLExtractor\Application(); preg_match('/(.*):(.*)@(.*)\\/(.*)/', $source, $serverSourceDetails); preg_match('/(.*):(.*)@(.*)\\/(.*)/', $destination, $serverDestinationDetails); if (is_array($serverSourceDetails) && count($serverSourceDetails) === 5) { $mysqlCredentials = new \stdClass(); $mysqlCredentials->dbuser = $serverSourceDetails[1]; $mysqlCredentials->dbpass = $serverSourceDetails[2]; $mysqlCredentials->host = $serverSourceDetails[3]; $mysqlCredentials->dbname = $serverSourceDetails[4]; $worker1->processServer($mysqlCredentials); } else { // error, path not found $notFound = true; } if (is_array($serverDestinationDetails) && count($serverDestinationDetails) === 5) { $mysqlCredentials = new \stdClass(); $mysqlCredentials->dbuser = $serverDestinationDetails[1]; $mysqlCredentials->dbpass = $serverDestinationDetails[2]; $mysqlCredentials->host = $serverDestinationDetails[3]; $mysqlCredentials->dbname = $serverDestinationDetails[4]; $worker2->processServer($mysqlCredentials); } else { // error, path not found $notFound = true; } if ($notFound) { $output->write('Error: invalid source format, check if the source path exists and the format is valid.'); } else { $sourceDBs = json_decode(json_encode($worker1->getDatabases()), true); $destinationDBs = json_decode(json_encode($worker2->getDatabases()), true); $sourceDatabase = end($sourceDBs); $destinationDatabase = end($destinationDBs); $databaseDiffs = SnapshotCompareCommand::appendDifferencesDatabases($sourceDatabase, $destinationDatabase); $output->writeln('Done. ' . PHP_EOL . print_r(json_encode($databaseDiffs, JSON_PRETTY_PRINT), 2)); } }
/** * when the input is a file then process contents */ public function testWhenTheInputIsAFileThenProcessContents() { $inputPath = '/valid/input_db_1.sql'; $contents = "\n CREATE TABLE `companii` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `nume` varchar(255) DEFAULT NULL,\n `url` varchar(100) DEFAULT NULL,\n `domeniu` varchar(100) DEFAULT NULL,\n `email` varchar(255) DEFAULT NULL,\n `telefon` varchar(100) DEFAULT NULL,\n `judet` varchar(100) DEFAULT NULL,\n `localitate` varchar(100) DEFAULT NULL,\n `strada` varchar(255) DEFAULT NULL,\n `nr` varchar(20) DEFAULT NULL,\n `reprezentant` varchar(255) DEFAULT NULL,\n `username` varchar(255) DEFAULT NULL,\n `parola` varchar(60) DEFAULT NULL,\n `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',\n PRIMARY KEY (`id`),\n KEY `adresa` (`judet`,`localitate`,`strada`,`nr`)\n ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;\n "; $systemMock = \Mockery::mock('\\MySQLExtractor\\Common\\SystemMock')->makePartial(); $systemMock->shouldReceive('file_exists')->with($inputPath)->andReturn(true); $systemMock->shouldReceive('is_dir')->with($inputPath)->andReturn(false); $systemMock->shouldReceive('file_get_contents')->with($inputPath)->andReturn($contents); $system = new \MySQLExtractor\Common\System(); $refObject = new \ReflectionObject($system); $refProperty = $refObject->getProperty('mock'); $refProperty->setAccessible(true); $refProperty->setValue($system, $systemMock); $worker = new \MySQLExtractor\Application(); $worker->processDisk($inputPath); $extractor = \PHPUnit_Framework_Assert::readAttribute($worker, 'extractor'); $databases = $extractor->databases(); $this->assertEquals(1, count($databases)); $this->assertEquals('input_db_1', $databases['input_db_1']->Name); $this->assertEquals(1, count($databases['input_db_1']->Tables)); }
protected function execute(InputInterface $input, OutputInterface $output) { $notFound = false; $source = $input->getArgument('source'); $outputPath = $input->getArgument('output'); $worker = new \MySQLExtractor\Application(); $scanPathname = realpath($source); preg_match('/(.*):(.*)@(.*)\\/(.*)/', $source, $serverDetails); if (is_array($serverDetails) && count($serverDetails) === 5) { $mysqlCredentials = new \stdClass(); $mysqlCredentials->dbuser = $serverDetails[1]; $mysqlCredentials->dbpass = $serverDetails[2]; $mysqlCredentials->host = $serverDetails[3]; $mysqlCredentials->dbname = $serverDetails[4]; $worker->processServer($mysqlCredentials); } else { if ($scanPathname) { $worker->processDisk($scanPathname); } else { // error, path not found $notFound = true; } } if ($notFound) { $output->write('Error: invalid source format, check if the source path exists and the format is valid.'); } else { $worker->output($outputPath, $results = $worker->getDatabases()); // will output .json files for each database $output->write('Finished. Check the output folder.' . PHP_EOL . implode(PHP_EOL, $worker->statistics()) . PHP_EOL); } }
<?php $path = dirname(__FILE__); require_once realpath($path . '/../vendor/autoload.php'); $worker = new \MySQLExtractor\Application(); //$mysqlCredentials = new stdClass(); //$mysqlCredentials->host = '127.0.0.1'; //$mysqlCredentials->dbuser = '******'; //$mysqlCredentials->dbpass = '******'; //$mysqlCredentials->dbname = 'redmine'; // //$worker->processServer($mysqlCredentials); // // or: $worker->processDisk($path . '/test_sql/', 'project_mooc'); // folder containing .sql files (format: dbname.sql) $worker->output($path . '/output/', $worker->getDatabases()); // will output .json files for each database echo "Finished. Check the output folder.\n\n" . implode("\n", $worker->statistics()) . "\n\n";