From d754adb0679c84a079c0c3cb736af4e3c0f09526 Mon Sep 17 00:00:00 2001 From: phpnut Date: Mon, 7 Jan 2008 13:35:14 +0000 Subject: [PATCH] "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 --- cake/console/libs/tasks/controller.php | 26 +++++++++---------- cake/console/libs/tasks/model.php | 35 +++++++++++++++++++++----- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/cake/console/libs/tasks/controller.php b/cake/console/libs/tasks/controller.php index 37865db96..19827db4d 100644 --- a/cake/console/libs/tasks/controller.php +++ b/cake/console/libs/tasks/controller.php @@ -454,23 +454,23 @@ class ControllerTask extends Shell { * @access private */ function bakeTest($className) { - $out = '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 = ""; + return $this->createFile($path . $filename, $content); } /** * Outputs and gets the list of possible models or controllers from database diff --git a/cake/console/libs/tasks/model.php b/cake/console/libs/tasks/model.php index 4384e8ebd..d2c35087c 100644 --- a/cake/console/libs/tasks/model.php +++ b/cake/console/libs/tasks/model.php @@ -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') {