Renaming methods, fixing issue with habtm bleed through.

Added test cases for all relation generation.
This commit is contained in:
mark_story 2009-05-09 21:28:51 -04:00
parent 0467641469
commit 4b4875e0a9
2 changed files with 116 additions and 15 deletions

View file

@ -400,9 +400,9 @@ class ModelTask extends Shell {
);
$possibleKeys = array();
$associations = $this->_findBelongsTo($model, $associations);
$associations = $this->_findHasOneAndMany($model, $associations);
$associations = $this->_findHasAndBelongsToMany($model, $associations);
$associations = $this->findBelongsTo($model, $associations);
$associations = $this->findHasOneAndMany($model, $associations);
$associations = $this->findHasAndBelongsToMany($model, $associations);
if ($this->interactive !== true) {
unset($associations['hasOne']);
@ -530,7 +530,7 @@ class ModelTask extends Shell {
* @param array $associations Array of inprogress associations
* @return array $associations with belongsTo added in.
**/
function _findBelongsTo(&$model, $associations) {
function findBelongsTo(&$model, $associations) {
$fields = $model->schema();
foreach ($fields as $fieldName => $field) {
$offset = strpos($fieldName, '_id');
@ -553,14 +553,14 @@ class ModelTask extends Shell {
* @param array $associations Array of inprogress associations
* @return array $associations with hasOne and hasMany added in.
**/
function _findHasOneAndMany(&$model, $associations) {
function findHasOneAndMany(&$model, $associations) {
$foreignKey = $this->_modelKey($model->name);
foreach ($this->__tables as $otherTable) {
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable));
$modelFieldsTemp = $tempOtherModel->schema();
$pattern = '/_' . preg_quote($otherTable, '/') . '|' . preg_quote($otherTable, '/') . '_/';
$possibleJoinTable = preg_match($pattern , $model->table);
$pattern = '/_' . preg_quote($model->table, '/') . '|' . preg_quote($model->table, '/') . '_/';
$possibleJoinTable = preg_match($pattern , $otherTable);
foreach ($modelFieldsTemp as $fieldName => $field) {
if ($fieldName != $model->primaryKey && $fieldName == $foreignKey && $possibleJoinTable == false) {
$assoc = array(
@ -583,7 +583,7 @@ class ModelTask extends Shell {
* @param array $associations Array of inprogress associations
* @return array $associations with hasAndBelongsToMany added in.
**/
function _findHasAndBelongsToMany(&$model, $associations) {
function findHasAndBelongsToMany(&$model, $associations) {
$foreignKey = $this->_modelKey($model->name);
foreach ($this->__tables as $otherTable) {
$tempOtherModel = $this->_getModelObject($this->_modelName($otherTable));

View file

@ -66,7 +66,7 @@ class ModelTaskTest extends CakeTestCase {
*
* @var array
**/
var $fixtures = array('core.article', 'core.comment');
var $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
/**
* setUp method
@ -98,16 +98,21 @@ class ModelTaskTest extends CakeTestCase {
**/
function testListAll() {
$this->Task->expectAt(1, 'out', array('1. Article'));
$this->Task->expectAt(2, 'out', array('2. Comment'));
$this->Task->expectAt(2, 'out', array('2. ArticlesTag'));
$this->Task->expectAt(3, 'out', array('3. Comment'));
$this->Task->expectAt(4, 'out', array('4. Tag'));
$result = $this->Task->listAll('test_suite');
$expected = array('articles', 'comments');
$expected = array('articles', 'articles_tags', 'comments', 'tags');
$this->assertEqual($result, $expected);
$this->Task->expectAt(4, 'out', array('1. Article'));
$this->Task->expectAt(5, 'out', array('2. Comment'));
$this->Task->expectAt(6, 'out', array('1. Article'));
$this->Task->expectAt(7, 'out', array('2. ArticlesTag'));
$this->Task->expectAt(8, 'out', array('3. Comment'));
$this->Task->expectAt(9, 'out', array('4. Tag'));
$this->Task->connection = 'test_suite';
$result = $this->Task->listAll();
$expected = array('articles', 'comments');
$expected = array('articles', 'articles_tags', 'comments', 'tags');
$this->assertEqual($result, $expected);
}
@ -128,7 +133,7 @@ class ModelTaskTest extends CakeTestCase {
$expected = 'Article';
$this->assertEqual($result, $expected);
$this->Task->setReturnValueAt(2, 'in', 2);
$this->Task->setReturnValueAt(2, 'in', 3);
$result = $this->Task->getName('test_suite');
$expected = 'Comment';
$this->assertEqual($result, $expected);
@ -272,5 +277,101 @@ class ModelTaskTest extends CakeTestCase {
);
$this->assertEqual($result, $expected);
}
/**
* test that finding primary key works
*
* @return void
**/
function testFindPrimaryKey() {
$fields = array(
'one' => array(),
'two' => array(),
'key' => array('key' => 'primary')
);
$this->Task->expectAt(0, 'in', array('*', null, 'key'));
$this->Task->setReturnValue('in', 'my_field');
$result = $this->Task->findPrimaryKey($fields);
$expected = 'my_field';
$this->assertEqual($result, $expected);
}
/**
* test that belongsTo generation works.
*
* @return void
**/
function testBelongsToGeneration() {
$model = new Model(array('ds' => 'test_suite', 'name' => 'Comment'));
$result = $this->Task->findBelongsTo($model, array());
$expected = array(
'belongsTo' => array(
array(
'alias' => 'Article',
'className' => 'Article',
'foreignKey' => 'article_id',
),
array(
'alias' => 'User',
'className' => 'User',
'foreignKey' => 'user_id',
),
)
);
$this->assertEqual($result, $expected);
}
/**
* test that hasOne and/or hasMany relations are generated properly.
*
* @return void
**/
function testHasManyHasOneGeneration() {
$model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
$this->Task->connection = 'test_suite';
$this->Task->listAll();
$result = $this->Task->findHasOneAndMany($model, array());
$expected = array(
'hasMany' => array(
array(
'alias' => 'Comment',
'className' => 'Comment',
'foreignKey' => 'article_id',
),
),
'hasOne' => array(
array(
'alias' => 'Comment',
'className' => 'Comment',
'foreignKey' => 'article_id',
),
),
);
$this->assertEqual($result, $expected);
}
/**
* test that habtm generation works
*
* @return void
**/
function testHasAndBelongsToManyGeneration() {
$model = new Model(array('ds' => 'test_suite', 'name' => 'Article'));
$this->Task->connection = 'test_suite';
$this->Task->listAll();
$result = $this->Task->findHasAndBelongsToMany($model, array());
$expected = array(
'hasAndBelongsToMany' => array(
array(
'alias' => 'Tag',
'className' => 'Tag',
'foreignKey' => 'article_id',
'joinTable' => 'articles_tags',
'associationForeignKey' => 'tag_id',
),
),
);
$this->assertEqual($result, $expected);
}
}
?>