コード例 #1
0
 public function testConfigs()
 {
     $configPaths = glob(dirname(__FILE__) . '/test_configs/test_config*/');
     foreach ($configPaths as $configPath) {
         // 1. Setup DB
         $this->executeSqlFile($configPath . 'db_setup/db_setup.sql');
         // 2. Generate
         // Which config to use?
         $schemaGeneratorConfigFile = $configPath . 'generator_config/database_schema_generator.inc.php';
         $coughGeneratorConfigFile = $configPath . 'generator_config/cough_generator.inc.php';
         // Get the database config
         $schemaGeneratorConfig = DatabaseSchemaGeneratorConfig::constructFromFile($schemaGeneratorConfigFile);
         // Load the schema into memory
         $schemaGenerator = new DatabaseSchemaGenerator($schemaGeneratorConfig);
         $schema = $schemaGenerator->generateSchema();
         // Manipulate the schema (to add any missed relationships, e.g.)
         $manipulator = new SchemaManipulator($schemaGeneratorConfig);
         $manipulator->manipulateSchema($schema);
         // Get the cough generator config
         $outputDir = $configPath . 'output/';
         include $coughGeneratorConfigFile;
         $coughGeneratorConfig = new CoughGeneratorConfig($config);
         // Generate files into memory
         $coughGenerator = new CoughGenerator($coughGeneratorConfig);
         $classes = $coughGenerator->generateCoughClasses($schema);
         // Write files to disk
         $coughWriter = new CoughWriter($coughGeneratorConfig);
         $this->assertTrue($coughWriter->writeClasses($classes), 'Unable to write classes to disk.');
         // 3. Perform comparison
         $diffCommand = 'diff -r ' . escapeshellarg($configPath . 'expected_output') . ' ' . escapeshellarg($configPath . 'output');
         $diffOutput = shell_exec($diffCommand);
         $message = "Generated output does not match; diff files using:\n" . $diffCommand . "\n\n" . "DIFF OUTPUT:\n" . "==============================================\n" . $diffOutput . "\n" . "==============================================\n\n";
         if (!empty($diffOutput)) {
             $message .= "<: " . substr_count($diffOutput, "\n<") . "\n";
             $message .= ">: " . substr_count($diffOutput, "\n>") . "\n\n";
         }
         $this->assertTrue(empty($diffOutput), $message);
         // 4. Clean up files
         if (empty($diffOutput)) {
             $this->removeGeneratedClasses($configPath . 'output/');
         }
         // 5. Clean up DB
         $this->executeSqlFile($configPath . 'db_setup/db_teardown.sql');
         $this->dropAllTables();
     }
 }
コード例 #2
0
 protected function generateFromConfigPath($configPath)
 {
     try {
         if (!file_exists($configPath)) {
             echo 'Config path does not exist: ' . $configPath . "\n";
             return;
         }
         if (!is_dir($configPath)) {
             echo 'Config path is not a directory: ' . $configPath . "\n";
             return;
         }
         // Which config to use?
         $schemaGeneratorConfigFile = $configPath . 'database_schema_generator.inc.php';
         $coughGeneratorConfigFile = $configPath . 'cough_generator.inc.php';
         // Get the database config
         $schemaGeneratorConfig = DatabaseSchemaGeneratorConfig::constructFromFile($schemaGeneratorConfigFile);
         // Load the schema into memory
         $schemaGenerator = new DatabaseSchemaGenerator($schemaGeneratorConfig);
         if ($this->verbose) {
             $schemaGenerator->enableVerbose();
         }
         $schema = $schemaGenerator->generateSchema();
         // Dump some verbose messages.
         // echo "\n";
         // $schema->outputRelationshipCounts();
         // echo "\n" . 'About to run the SchemaManipulator and re-output the relationships.' . "\n";
         // Manipulate the schema (to add any missed relationships, e.g.)
         $manipulator = new SchemaManipulator($schemaGeneratorConfig);
         if ($this->verbose) {
             $manipulator->enableVerbose();
         }
         $manipulator->manipulateSchema($schema);
         // Dump some verbose messages again to see if the manipulator added anything.
         // echo "\n";
         // $schema->outputRelationshipCounts();
         // Get the cough generator config
         $coughGeneratorConfig = CoughGeneratorConfig::constructFromFile($coughGeneratorConfigFile);
         // Generate files into memory
         $coughGenerator = new CoughGenerator($coughGeneratorConfig);
         $classes = $coughGenerator->generateCoughClasses($schema);
         // Add some spacing if verbose mode is on
         if ($this->verbose) {
             echo "\n";
         }
         // Write files to disk
         $coughWriter = new CoughWriter($coughGeneratorConfig);
         if (!$coughWriter->writeClasses($classes)) {
             echo 'Trouble writing classes:' . "\n";
             echo '------------------------' . "\n";
             foreach ($coughWriter->getErrorMessages() as $message) {
                 echo $message . "\n";
             }
             echo "\n";
         } else {
             $lineCount = 0;
             foreach ($classes as $class) {
                 $lineCount += substr_count($class->getContents(), "\n");
             }
             echo 'Success writing ' . count($classes) . ' classes (' . number_format($lineCount) . ' lines) with ' . $schema->getNumberOfHasOneRelationships() . ' one-to-one relationships and ' . $schema->getNumberOfHasManyRelationships() . ' one-to-many relationships!' . "\n";
         }
         if ($this->verbose) {
             echo "\n" . 'PHP memory usage:' . "\n";
             echo number_format(memory_get_usage()) . " used\n";
             if (version_compare(phpversion(), '5.2.0', '>=')) {
                 echo number_format(memory_get_usage(true)) . " allocated\n";
             }
         }
     } catch (Exception $e) {
         echo $e->getMessage() . "\n";
     }
 }