*/ include '../configuration.inc'; $dir = APPLICATION_HOME.'/scripts/stubs/tests'; if (!is_dir($dir)) { mkdir($dir,0770,true); } $dir = APPLICATION_HOME.'/scripts/stubs/tests/DatabaseTests'; if (!is_dir($dir)) { mkdir($dir,0770,true); } $dir = APPLICATION_HOME.'/scripts/stubs/tests/UnitTests'; if (!is_dir($dir)) { mkdir($dir,0770,true); } $dir = APPLICATION_HOME.'/scripts/stubs/tests'; $zend_db = Database::getConnection(); $classes = array(); foreach ($zend_db->listTables() as $tableName) { $fields = array(); $primary_keys = array(); foreach ($zend_db->describeTable($tableName) as $row) { $type = preg_replace("/[^a-z]/","",strtolower($row['DATA_TYPE'])); // Translate database datatypes into PHP datatypes if (preg_match('/int/',$type)) { $type = 'int'; } if (preg_match('/enum/',$type) || preg_match('/varchar/',$type)) { $type = 'string'; } $fields[] = array('field'=>$row['COLUMN_NAME'],'type'=>$type); if ($row['PRIMARY']) { $primary_keys[] = $row['COLUMN_NAME']; } } // Only generate code for tables that have a single-column primary key // Code for other tables will need to be created by hand if (count($primary_keys) != 1) { continue; } $key = $primary_keys[0]; $tableName = strtolower($tableName); $className = Inflector::classify($tableName); $classes[] = $className; $variable = strtolower($className); //------------------------------------------------------------------------------ // Generate the Unit Tests //------------------------------------------------------------------------------ $contents = "validate(); \$this->fail('Missing name failed to throw exception'); } catch (Exception \$e) { } \${$variable}->setName('Test {$className}'); \${$variable}->validate(); } } "; file_put_contents("$dir/UnitTests/{$className}UnitTest.php",$contents); //------------------------------------------------------------------------------ // Generate the Database Tests //------------------------------------------------------------------------------ $contents = "setName('Test {$className}'); try { \${$variable}->save(); \$id = \${$variable}->getId(); \$this->assertGreaterThan(0,\$id); } catch (Exception \$e) { \$this->fail(\$e->getMessage()); } \${$variable} = new {$className}(\$id); \$this->assertEquals(\${$variable}->getName(),'Test {$className}'); \${$variable}->setName('Test'); \${$variable}->save(); \${$variable} = new {$className}(\$id); \$this->assertEquals(\${$variable}->getName(),'Test'); } } "; file_put_contents("$dir/DatabaseTests/{$className}DbTest.php",$contents); //------------------------------------------------------------------------------ // Generate the Database List Tests //------------------------------------------------------------------------------ $contents = "query('select id from $tableName order by id'); \$result = \$query->fetchAll(); \$list = new {$className}List(); \$list->find(); \$this->assertEquals(\$list->getSort(),'id'); foreach (\$list as \$i=>\${$variable}) { \$this->assertEquals(\${$variable}->getId(),\$result[\$i]['id']); } } } "; file_put_contents("$dir/DatabaseTests/{$className}ListDbTest.php",$contents); echo "$className\n"; } //------------------------------------------------------------------------------ // Generate the All Tests Suite //------------------------------------------------------------------------------ $contents = "addTest(UnitTests::suite()); \$suite->addTest(DatabaseTests::suite()); return \$suite; } } "; file_put_contents("$dir/AllTests.php",$contents); //------------------------------------------------------------------------------ // Generate the All Tests Suite //------------------------------------------------------------------------------ $contents = "addTestSuite('{$className}DbTest');\n"; $contents.= "\t\t\$suite->addTestSuite('{$className}ListDbTest');\n"; } $contents.= " return \$suite; } } "; file_put_contents("$dir/DatabaseTests.php",$contents); //------------------------------------------------------------------------------ // Generate the Unit Tests Suite //------------------------------------------------------------------------------ $contents = "addTestSuite('{$className}UnitTest');\n"; } $contents.= " return \$suite; } } "; file_put_contents("$dir/UnitTests.php",$contents);