"Updating controller tests created with the console.

Added associated fixtures to model tests"

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6332 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2008-01-07 13:35:14 +00:00
parent d6bcff4797
commit d754adb067
2 changed files with 42 additions and 19 deletions

View file

@ -454,23 +454,23 @@ class ControllerTask extends Shell {
* @access private
*/
function bakeTest($className) {
$out = '<?php '."\n\n";
$out .= "App::import('Controller', '$className');\n\n";
$out .= "class {$className}ControllerTestCase extends CakeTestCase {\n";
$out .= "\tvar \$TestObject = null;\n\n";
$out .= "\tfunction setUp() {\n\t\t\$this->TestObject = new {$className}Controller();\n";
$out .= "\t}\n\n\tfunction tearDown() {\n\t\tunset(\$this->TestObject);\n\t}\n";
$out .= "\n\t/*\n\tfunction testMe() {\n";
$out .= "\t\t\$result = \$this->TestObject->index();\n";
$out .= "\t\t\$expected = 1;\n";
$out .= "\t\t\$this->assertEqual(\$result, \$expected);\n\t}\n\t*/\n}";
$out .= "\n?>";
$out = "App::import('Controller', '$className');\n\n";
$out .= "class Test{$className} extends {$className}Controller {\n";
$out .= "\tvar \$autoRender = false;\n}\n\n";
$out .= "class {$className}ControllerTest extends CakeTestCase {\n";
$out .= "\tvar \${$className} = null;\n\n";
$out .= "\tfunction setUp() {\n\t\t\$this->{$className} = new Test{$className}();\n\t}\n\n";
$out .= "\tfunction test{$className}ControllerInstance() {\n";
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}Controller'));\n\t}\n\n";
$out .= "\tfunction tearDown() {\n\t\tunset(\$this->{$className});\n\t}\n}\n";
$path = CONTROLLER_TESTS;
$filename = Inflector::underscore($className).'_controller.test.php';
$this->out("\nBaking unit test for $className...");
$this->out("Baking unit test for $className...");
return $this->createFile($path . $filename, $out);
$header = '$Id';
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ."Controller Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
return $this->createFile($path . $filename, $content);
}
/**
* Outputs and gets the list of possible models or controllers from database

View file

@ -181,7 +181,7 @@ class ModelTask extends Shell {
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
if ($this->bake($currentModelName, $associations, $validate, $primaryKey, $useTable, $useDbConfig)) {
if ($this->_checkUnitTest()) {
$this->bakeTest($currentModelName, $useTable);
$this->bakeTest($currentModelName, $useTable, $associations);
}
}
} else {
@ -630,20 +630,36 @@ class ModelTask extends Shell {
* @param string $className Model class name
* @access private
*/
function bakeTest($className, $useTable = null) {
function bakeTest($className, $useTable = null, $associations = array()) {
$results = $this->fixture($className, $useTable);
if ($results) {
$fixture = Inflector::underscore($className);
$fixture[] = "'app." . Inflector::underscore($className) ."'";
if (!empty($associations)) {
$assoc[] = Set::extract($associations, 'belongsTo.{n}.className');
$assoc[] = Set::extract($associations, 'hasOne.{n}.className');
$assoc[] = Set::extract($associations, 'hasMany.{n}.className');
foreach ($assoc as $key => $value) {
if (is_array($value)) {
foreach ($value as $class) {
$fixture[] = "'app." . Inflector::underscore($class) ."'";
}
}
}
}
$fixture = join(", ", $fixture);
$out = "App::import('Model', '$className');\n\n";
$out .= "class Test{$className} extends {$className} {\n";
$out .= "\tvar \$cacheSources = false;\n}\n\n";
$out .= "class {$className}TestCase extends CakeTestCase {\n";
$out .= "\tvar \$fixtures = array('app.$fixture');\n\n";
$out .= "\tvar \${$className} = null;\n";
$out .= "\tvar \$fixtures = array($fixture);\n\n";
$out .= "\tfunction start() {\n\t\tparent::start();\n\t\t\$this->{$className} = new Test{$className}();\n\t}\n\n";
$out .= "\tfunction test{$className}Instance() {\n";
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}'));\n\t}\n\n";
$out .= "\tfunction test{$className}Find() {\n";
$out .= "\t\t\$results = \$this->{$className}->recursive = -1;\n";
$out .= "\t\t\$results = \$this->{$className}->find('first');\n\t\t\$this->assertTrue(!empty(\$results));\n\n";
$out .= "\t\t\$expected = array('$className' => array(\n$results\n\t\t\t));\n";
$out .= "\t\t\$this->assertEqual(\$results, \$expected);\n\t}\n}\n";
@ -744,14 +760,21 @@ class ModelTask extends Shell {
if (!class_exists('CakeSchema')) {
App::import('Model', 'Schema');
}
$out = "\nclass {$model}Fixture extends CakeTestFixture {\n";
$out .= "\tvar \$name = '$model';\n";
if (!$useTable) {
$useTable = Inflector::tableize($model);
} else {
$out .= "\tvar \$table = '$useTable';\n";
}
$schema = new CakeSchema();
$data = $schema->read(array('models' => false));
if (!isset($data['tables']['missing'][$useTable])) {
return false;
}
$tables[$model] = $data['tables']['missing'][$useTable];
$out = "\nclass {$model}Fixture extends CakeTestFixture {\n";
$out .= "\tvar \$name = '$model';\n";
foreach ($tables as $table => $fields) {
if (!is_numeric($table) && $table !== 'missing') {