Adding tests for auto-constructing hasAndBelongsToMany associations, disproves #3859

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6431 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-02-03 06:40:15 +00:00
parent afebe2d418
commit 67d7579fda

View file

@ -42,15 +42,14 @@ class Test extends Model {
var $useTable = false;
var $name = 'Test';
function schema() {
return array(
var $_schema = array(
'id'=> array('type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8', 'key'=>'primary'),
'name'=> array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'email'=> array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'notes'=> array('type' => 'text', 'null' => '1', 'default' => 'write some notes here', 'length' => ''),
'created'=> array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
'updated'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null));
}
'updated'=> array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
}
/**
@ -63,21 +62,7 @@ class TestValidate extends Model {
var $useTable = false;
var $name = 'TestValidate';
function validateNumber($value, $options) {
$options = am(array('min' => 0, 'max' => 100), $options);
$valid = ($value['number'] >= $options['min'] && $value['number'] <= $options['max']);
return $valid;
}
function validateTitle($value) {
if (!empty($value) && strpos(low($value['title']), 'title-') === 0) {
return true;
}
return false;
}
function schema() {
return array(
var $_schema = array(
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
'title' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'body' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => ''),
@ -85,9 +70,17 @@ class TestValidate extends Model {
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
'modified' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
}
function validateNumber($value, $options) {
$options = am(array('min' => 0, 'max' => 100), $options);
$valid = ($value['number'] >= $options['min'] && $value['number'] <= $options['max']);
return $valid;
}
function validateTitle($value) {
return (!empty($value) && strpos(low($value['title']), 'title-') === 0);
}
}
/**
* Short description for class.
*
@ -464,6 +457,34 @@ class JoinC extends CakeTestModel {
var $name = 'JoinC';
var $hasAndBelongsToMany = array('JoinA');
}
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model
*/
class AssociationTest1 extends CakeTestModel {
var $useTable = 'join_as';
var $name = 'AssociationTest1';
var $hasAndBelongsToMany = array('AssociationTest2' => array(
'unique' => false, 'joinTable' => 'join_as_join_bs', 'foreignKey' => false
));
}
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.model
*/
class AssociationTest2 extends CakeTestModel {
var $useTable = 'join_bs';
var $name = 'AssociationTest2';
var $hasAndBelongsToMany = array('AssociationTest1' => array(
'unique' => false, 'joinTable' => 'join_as_join_bs'
));
}
/**
* Short description for class.
*
@ -496,6 +517,21 @@ class ModelTest extends CakeTestCase {
Configure::write('debug', $this->debug);
}
function testAutoConstructAssociations() {
$this->loadFixtures('User');
$this->model =& new AssociationTest1();
$result = $this->model->hasAndBelongsToMany;
$expected = array('AssociationTest2' => array(
'unique' => false, 'joinTable' => 'join_as_join_bs', 'foreignKey' => false,
'className' => 'AssociationTest2', 'with' => 'JoinAsJoinB',
'associationForeignKey' => 'join_b_id', 'conditions' => '', 'fields' => '',
'order' => '', 'limit' => '', 'offset' => '', 'finderQuery' => '',
'deleteQuery' => '', 'insertQuery' => ''
));
$this->assertEqual($result, $expected);
}
function testMultipleBelongsToWithSameClass() {
$this->loadFixtures('DeviceType', 'DeviceTypeCategory', 'FeatureSet', 'ExteriorTypeCategory', 'Document', 'Device', 'DocumentDirectory');
$this->DeviceType =& new DeviceType();