Skip to content

Commit

Permalink
Renaming methods, fixing issue with habtm bleed through.
Browse files Browse the repository at this point in the history
Added test cases for all relation generation.
  • Loading branch information
markstory committed May 10, 2009
1 parent 0467641 commit 4b4875e
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 15 deletions.
16 changes: 8 additions & 8 deletions cake/console/libs/tasks/model.php
Expand Up @@ -400,9 +400,9 @@ function doAssociations(&$model) {
);
$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']);
Expand Down Expand Up @@ -530,7 +530,7 @@ function doAssociations(&$model) {
* @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');
Expand All @@ -553,14 +553,14 @@ function _findBelongsTo(&$model, $associations) {
* @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(
Expand All @@ -583,7 +583,7 @@ function _findHasOneAndMany(&$model, $associations) {
* @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));
Expand Down
115 changes: 108 additions & 7 deletions cake/tests/cases/console/libs/tasks/model.test.php
Expand Up @@ -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
Expand Down Expand Up @@ -98,16 +98,21 @@ function endTest() {
**/
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);
}

Expand All @@ -128,7 +133,7 @@ function testGetName() {
$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);
Expand Down Expand Up @@ -272,5 +277,101 @@ function testNonInteractiveDoValidation() {
);
$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);
}
}
?>

0 comments on commit 4b4875e

Please sign in to comment.