From 4b4875e0a91a7929f3d8c9b8e12a0240b12d9a6d Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 9 May 2009 21:28:51 -0400 Subject: [PATCH] Renaming methods, fixing issue with habtm bleed through. Added test cases for all relation generation. --- cake/console/libs/tasks/model.php | 16 +-- .../cases/console/libs/tasks/model.test.php | 115 ++++++++++++++++-- 2 files changed, 116 insertions(+), 15 deletions(-) diff --git a/cake/console/libs/tasks/model.php b/cake/console/libs/tasks/model.php index 61c27fa8d93..a982cfb281c 100644 --- a/cake/console/libs/tasks/model.php +++ b/cake/console/libs/tasks/model.php @@ -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']); @@ -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'); @@ -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( @@ -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)); diff --git a/cake/tests/cases/console/libs/tasks/model.test.php b/cake/tests/cases/console/libs/tasks/model.test.php index 07dcabed05f..c5f74d89b48 100644 --- a/cake/tests/cases/console/libs/tasks/model.test.php +++ b/cake/tests/cases/console/libs/tasks/model.test.php @@ -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 @@ 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); } @@ -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); @@ -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); + } } ?> \ No newline at end of file