Skip to content
Permalink
Browse files

Adding bake model template.

Adding tests for bake model.
  • Loading branch information...
markstory committed May 14, 2009
1 parent a7f0071 commit 3c08369e3af7d260712868e5754fd77b30e23c92
@@ -62,7 +62,7 @@ class ModelTask extends Shell {
* @var array
* @access public
*/
var $tasks = array('DbConfig', 'Fixture', 'Test');
var $tasks = array('DbConfig', 'Fixture', 'Test', 'Template');
/**
* Holds tables found on connection.
@@ -700,7 +700,12 @@ function bake($name, $associations = array(), $validate = array(), $primaryKey
$useDbConfig = $name->useDbConfig;
$name = $name->name;
}
$this->Template->set(compact('name', 'useDbConfig', 'associations', 'validate', 'primaryKey', 'useTable'));
$this->Template->set('plugin', $this->plugin);
$out = $this->Template->generate('objects', 'model');
/*
$out = "<?php\n";
$out .= "class {$name} extends {$this->plugin}AppModel {\n\n";
$out .= "\tvar \$name = '{$name}';\n";
@@ -841,9 +846,11 @@ function bake($name, $associations = array(), $validate = array(), $primaryKey
}
$out .= "}\n";
$out .= "?>";
*/
$filename = $this->path . Inflector::underscore($name) . '.php';
$this->out("\nBaking model class for $name...");
return $this->createFile($filename, $out);
$this->createFile($filename, $out);
return $out;
}
/**
@@ -0,0 +1,125 @@
<?php
/**
* Model template file.
*
* Used by bake to create new Model files.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org
* @package cake
* @subpackage cake.console.libs.templates.objects
* @since CakePHP(tm) v 1.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
echo "<?php\n"; ?>
class <?php echo $name ?> extends <?php echo $plugin; ?>AppModel {
var $name = '<?php echo $name; ?>';
<?php if ($useDbConfig != 'default'): ?>
var $useDbConfig = '<?php echo $useDbConfig; ?>';
<?php endif;?>
<?php if (($useTable && $useTable !== Inflector::tableize($name)) || $useTable === false):
$table = "'$useTable'";
if (!$useTable):
$table = 'false';
endif;
echo "\tvar \$useTable = $table;\n";
endif; ?>
<?php if ($primaryKey !== 'id'): ?>
var $primaryKey = '<?php echo $primaryKey; ?>';
<?php endif; ?>
<?php
if (!empty($validate)):
echo "\tvar \$validate = array(\n";
foreach ($validate as $field => $validations):
echo "\t\t'$field' => array(\n";
foreach ($validations as $key => $validator):
echo "\t\t\t'$key' => array('rule' => array('$validator')),\n";
endforeach;
echo "\t\t),\n";
endforeach;
echo "\t);\n";
endif;
foreach (array('hasOne', 'belongsTo') as $assocType):
if (!empty($associations[$assocType])):
$typeCount = count($associations[$assocType]);
echo "\n\tvar \$$assocType = array(";
foreach ($associations[$assocType] as $i => $relation):
$out = "\n\t\t'{$relation['alias']}' => array(\n";
$out .= "\t\t\t'className' => '{$relation['className']}',\n";
$out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n";
$out .= "\t\t\t'conditions' => '',\n";
$out .= "\t\t\t'fields' => '',\n";
$out .= "\t\t\t'order' => ''\n";
$out .= "\t\t)";
if ($i + 1 < $typeCount) {
$out .= ",";
}
echo $out;
endforeach;
echo "\n\t);\n";
endif;
endforeach;
if (!empty($associations['hasMany'])):
$belongsToCount = count($associations['hasMany']);
echo "\n\tvar \$hasMany = array(";
foreach ($associations['hasMany'] as $i => $relation):
$out = "\n\t\t'{$relation['alias']}' => array(\n";
$out .= "\t\t\t'className' => '{$relation['className']}',\n";
$out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n";
$out .= "\t\t\t'dependent' => false,\n";
$out .= "\t\t\t'conditions' => '',\n";
$out .= "\t\t\t'fields' => '',\n";
$out .= "\t\t\t'order' => '',\n";
$out .= "\t\t\t'limit' => '',\n";
$out .= "\t\t\t'offset' => '',\n";
$out .= "\t\t\t'exclusive' => '',\n";
$out .= "\t\t\t'finderQuery' => '',\n";
$out .= "\t\t\t'counterQuery' => ''\n";
$out .= "\t\t)";
if ($i + 1 < $belongsToCount) {
$out .= ",";
}
echo $out;
endforeach;
echo "\n\t);\n\n";
endif;
if (!empty($associations['hasAndBelongsToMany'])):
$habtmCount = count($associations['hasAndBelongsToMany']);
echo "\n\tvar \$hasAndBelongsToMany = array(";
foreach ($associations['hasAndBelongsToMany'] as $i => $relation):
$out = "\n\t\t'{$relation['alias']}' => array(\n";
$out .= "\t\t\t'className' => '{$relation['className']}',\n";
$out .= "\t\t\t'joinTable' => '{$relation['joinTable']}',\n";
$out .= "\t\t\t'foreignKey' => '{$relation['foreignKey']}',\n";
$out .= "\t\t\t'associationForeignKey' => '{$relation['associationForeignKey']}',\n";
$out .= "\t\t\t'unique' => true,\n";
$out .= "\t\t\t'conditions' => '',\n";
$out .= "\t\t\t'fields' => '',\n";
$out .= "\t\t\t'order' => '',\n";
$out .= "\t\t\t'limit' => '',\n";
$out .= "\t\t\t'offset' => '',\n";
$out .= "\t\t\t'finderQuery' => '',\n";
$out .= "\t\t\t'deleteQuery' => '',\n";
$out .= "\t\t\t'insertQuery' => ''\n";
$out .= "\t\t)";
if ($i + 1 < $habtmCount) {
$out .= ",";
}
echo $out;
endforeach;
echo "\n\t);\n\n";
endif;
?>
<?php echo '?>'; ?>
@@ -37,10 +37,10 @@
ob_end_clean();
}
if (!class_exists('ModelTask')) {
require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'model.php';
require CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
}
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'model.php';
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'fixture.php';
require_once CAKE . 'console' . DS . 'libs' . DS . 'tasks' . DS . 'template.php';
Mock::generatePartial(
'ShellDispatcher', 'TestModelTaskMockShellDispatcher',
@@ -82,7 +82,9 @@ class ModelTaskTest extends CakeTestCase {
function startTest() {
$this->Dispatcher =& new TestModelTaskMockShellDispatcher();
$this->Task =& new MockModelTask($this->Dispatcher);
$this->Task->Dispatch = new $this->Dispatcher;
$this->Task->Dispatch =& new $this->Dispatcher;
$this->Task->Dispatch->shellPaths = Configure::read('shellPaths');
$this->Task->Template =& new TemplateTask($this->Task->Dispatch);
}
/**
@@ -517,5 +519,86 @@ function testInOptions() {
$result = $this->Task->inOptions($options, 'Pick a number');
$this->assertEqual($result, 1);
}
/**
* test baking validation
*
* @return void
**/
function testBakeValidation() {
$validate = array(
'name' => array(
'notempty' => 'notempty'
),
'email' => array(
'email' => 'email',
),
'some_date' => array(
'date' => 'date'
),
'some_time' => array(
'time' => 'time'
)
);
$result = $this->Task->bake('Article', array(), $validate);
$this->assertPattern('/class Article extends AppModel \{/', $result);
$this->assertPattern('/\$name \= \'Article\'/', $result);
$this->assertPattern('/\$validate \= array\(/', $result);
$pattern = '/' . preg_quote("'notempty' => array('rule' => array('notempty')),", '/') . '/';
$this->assertPattern($pattern, $result);
}
/**
* test baking relations
*
* @return void
**/
function testBakeRelations() {
$associations = array(
'belongsTo' => array(
array(
'alias' => 'SomethingElse',
'className' => 'SomethingElse',
'foreignKey' => 'something_else_id',
),
array(
'alias' => 'User',
'className' => 'User',
'foreignKey' => 'user_id',
),
),
'hasOne' => array(
array(
'alias' => 'OtherModel',
'className' => 'OtherModel',
'foreignKey' => 'other_model_id',
),
),
'hasMany' => array(
array(
'alias' => 'Comment',
'className' => 'Comment',
'foreignKey' => 'parent_id',
),
),
'hasAndBelongsToMany' => array(
array(
'alias' => 'Tag',
'className' => 'Tag',
'foreignKey' => 'article_id',
'joinTable' => 'articles_tags',
'associationForeignKey' => 'tag_id',
),
)
);
$result = $this->Task->bake('Article', $associations, array());
$this->assertPattern('/\$hasAndBelongsToMany \= array\(/', $result);
$this->assertPattern('/\$hasMany \= array\(/', $result);
$this->assertPattern('/\$belongsTo \= array\(/', $result);
$this->assertPattern('/\$hasOne \= array\(/', $result);
$this->assertPattern('/Tag/', $result);
$this->assertPattern('/OtherModel/', $result);
$this->assertPattern('/SomethingElse/', $result);
$this->assertPattern('/Comment/', $result);
}
}
?>

0 comments on commit 3c08369

Please sign in to comment.
You can’t perform that action at this time.