From ff85f8c66812d2f9b816052ca05f12dd14d3e98a Mon Sep 17 00:00:00 2001 From: zYne Date: Wed, 25 Oct 2006 21:12:30 +0000 Subject: [PATCH] Fixes #194, added Doctrine_Relation_ManyToMany_TestCase Ticket: 194 --- lib/Doctrine/Lib.php | 5 - lib/Doctrine/Record.php | 5 +- lib/Doctrine/Table.php | 38 +++--- tests/RelationManyToManyTestCase.php | 194 +++++++++++++++++++++++++++ tests/RelationTestCase.php | 52 +------ tests/run.php | 104 +++++++------- 6 files changed, 273 insertions(+), 125 deletions(-) create mode 100644 tests/RelationManyToManyTestCase.php diff --git a/lib/Doctrine/Lib.php b/lib/Doctrine/Lib.php index ca067aca4..694cf75b8 100644 --- a/lib/Doctrine/Lib.php +++ b/lib/Doctrine/Lib.php @@ -140,11 +140,6 @@ class Doctrine_Lib { $r[] = "
";
         $r[] = "Component   : ".$table->getComponentName();
         $r[] = "Table       : ".$table->getTableName();
-        $r[] = "Repository  : ".$table->getRepository()->count()." objects";
-        if($table->getCache() instanceof Doctrine_Cache_File) {
-            $r[] = "Cache       : ".$table->getCache()->count()." objects";
-            $r[] = "Cache hits  : ".array_sum($table->getCache()->getStats())." hits";
-        }
         $r[] = "
"; return implode("\n",$r)."
"; } diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index e25fa42f1..80bf6fd56 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -496,7 +496,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @see Doctrine_Record::STATE_* constants * @return integer */ - final public function getState() { + public function getState() { return $this->_state; } /** @@ -504,6 +504,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * returns / assigns the state of this record * * @param integer|string $state if set, this method tries to set the record state to $state + * @see Doctrine_Record::STATE_* constants * * @throws Doctrine_Record_State_Exception if trying to set an unknown state * @return null|integer @@ -514,7 +515,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } $err = false; if(is_integer($state)) { - + if($state >= 1 && $state <= 6) $this->_state = $state; else diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index fad4652c8..eb7fb94a6 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -460,7 +460,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * * @return array */ - final public function getBounds() { + public function getBounds() { return $this->bound; } /** @@ -469,7 +469,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @param string $name * @return array */ - final public function getBound($name) { + public function getBound($name) { if( ! isset($this->bound[$name])) throw new Doctrine_Table_Exception('Unknown bound '.$name); @@ -481,9 +481,11 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @param string $name * @return array */ - final public function getBoundForName($name) { + public function getBoundForName($name, $component) { foreach($this->bound as $k => $bound) { - if($bound[3] == $name) { + $e = explode('.', $bound[0]); + + if($bound[3] == $name && $e[0] == $component) { return $this->bound[$k]; } } @@ -518,7 +520,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * * @return void */ - final public function unbindAll() { + public function unbindAll() { $this->bound = array(); $this->relations = array(); $this->boundAliases = array(); @@ -530,7 +532,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @param $name * @return boolean */ - final public function unbind($name) { + public function unbind($name) { if( ! isset($this->bound[$name])) return false; @@ -551,9 +553,9 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * @param string $field * @return void */ - final public function bind($name,$field,$type,$localKey) { + final public function bind($name, $field, $type, $localKey) { if(isset($this->relations[$name])) - throw new Doctrine_Table_Exception('Relation already set for '.$name); + unset($this->relations[$name]); $e = explode(" as ",$name); $name = $e[0]; @@ -571,21 +573,15 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { * getComponentName * @return string the component name */ - final public function getComponentName() { + public function getComponentName() { return $this->options['name']; } /** * @return Doctrine_Connection */ - final public function getConnection() { + public function getConnection() { return $this->connection; } - /** - * @return Doctrine_Cache - */ - final public function getCache() { - return $this->cache; - } /** * hasRelatedComponent * @return boolean @@ -662,7 +658,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { foreach(array_reverse($classes) as $class) { try { - $bound = $table->getBoundForName($class); + $bound = $table->getBoundForName($class, $component); break; } catch(Doctrine_Table_Exception $exc) { } @@ -670,11 +666,11 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { if( ! isset($local)) $local = $this->identifier; - $e2 = explode(".",$bound[0]); - $fields = explode("-",$e2[1]); + $e2 = explode('.', $bound[0]); + $fields = explode('-', $e2[1]); if($e2[0] != $component) - throw new Doctrine_Table_Exception($e2[0]." doesn't match ".$component); + throw new Doctrine_Table_Exception($e2[0] . ' doesn\'t match ' . $component); $associationTable = $this->connection->getTable($e2[0]); @@ -973,7 +969,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { final public function enumValue($field, $index) { if ($index instanceof Doctrine_Null) return $index; - + return isset($this->options['enumMap'][$field][$index]) ? $this->options['enumMap'][$field][$index] : $index; } /** diff --git a/tests/RelationManyToManyTestCase.php b/tests/RelationManyToManyTestCase.php new file mode 100644 index 000000000..399fce662 --- /dev/null +++ b/tests/RelationManyToManyTestCase.php @@ -0,0 +1,194 @@ +hasColumn('name', 'string', 200); + $this->hasColumn('child_id', 'integer'); + } + public function setUp() { + $this->ownsMany('OwnsOneToManyWithAlias as AliasO2M', 'AliasO2M.component_id'); + $this->hasMany('RTC1 as RTC1', 'JC1.c1_id'); + $this->hasMany('RTC2 as RTC2', 'JC1.c1_id'); + $this->hasMany('RTC3 as RTC3', 'JC2.c1_id'); + $this->hasMany('RTC3 as RTC4', 'JC1.c1_id'); + } +} + +class JC1 extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn('c1_id', 'integer'); + $this->hasColumn('c2_id', 'integer'); + } +} +class JC2 extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn('c1_id', 'integer'); + $this->hasColumn('c2_id', 'integer'); + } +} +class RTC1 extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn('name', 'string', 200); + } + public function setUp() { + $this->hasMany('M2MTest as RTC1', 'JC1.c2_id'); + } +} +class RTC2 extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn('name', 'string', 200); + } + public function setUp() { + $this->hasMany('M2MTest as RTC2', 'JC1.c2_id'); + } +} +class RTC3 extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn('name', 'string', 200); + } + public function setUp() { + $this->hasMany('M2MTest as RTC3', 'JC2.c2_id'); + $this->hasMany('M2MTest as RTC4', 'JC1.c2_id'); + } +} + +class Doctrine_Relation_ManyToMany_TestCase extends Doctrine_UnitTestCase { + public function prepareData() { } + public function prepareTables() { + parent::prepareTables(); + } + public function testManyToManyHasRelationWithAliases4() { + $component = new M2MTest(); + + try { + $rel = $component->getTable()->getRelation('RTC4'); + $this->pass(); + } catch(Doctrine_Exception $e) { + $this->fail(); + } + $this->assertTrue($rel instanceof Doctrine_Relation_Association); + + $this->assertTrue($component->RTC4 instanceof Doctrine_Collection); + } + + public function testManyToManyHasRelationWithAliases3() { + $component = new M2MTest(); + + try { + $rel = $component->getTable()->getRelation('RTC3'); + $this->pass(); + } catch(Doctrine_Exception $e) { + $this->fail(); + } + $this->assertTrue($rel instanceof Doctrine_Relation_Association); + + $this->assertTrue($component->RTC3 instanceof Doctrine_Collection); + } + + + public function testManyToManyHasRelationWithAliases() { + $component = new M2MTest(); + $component->AliasO2M; + + try { + $rel = $component->getTable()->getRelation('RTC1'); + $this->pass(); + } catch(Doctrine_Exception $e) { + $this->fail(); + } + $this->assertTrue($rel instanceof Doctrine_Relation_Association); + + $this->assertTrue($component->RTC1 instanceof Doctrine_Collection); + } + + public function testManyToManyHasRelationWithAliases2() { + $component = new M2MTest(); + + try { + $rel = $component->getTable()->getRelation('RTC2'); + $this->pass(); + } catch(Doctrine_Exception $e) { + $this->fail(); + } + $this->assertTrue($rel instanceof Doctrine_Relation_Association); + + $this->assertTrue($component->RTC1 instanceof Doctrine_Collection); + } + + + public function testManyToManyRelationSaving() { + $component = new M2MTest(); + + $component->RTC1[0]->name = '1'; + $component->RTC1[1]->name = '2'; + $component->name = '2'; + + $count = $this->dbh->count(); + + $component->save(); + + $this->assertEqual($this->dbh->count(), ($count + 5)); + + $this->assertEqual($component->RTC1->count(), 2); + + $component = $component->getTable()->find($component->id); + + $this->assertEqual($component->RTC1->count(), 2); + + // check that it doesn't matter saving the other M2M components as well + + $component->RTC2[0]->name = '1'; + $component->RTC2[1]->name = '2'; + + $count = $this->dbh->count(); + + $component->save(); + + $this->assertEqual($this->dbh->count(), ($count + 4)); + + $this->assertEqual($component->RTC2->count(), 2); + + $component = $component->getTable()->find($component->id); + + $this->assertEqual($component->RTC2->count(), 2); + + } + + public function testManyToManyRelationSaving2() { + $component = new M2MTest(); + + $component->RTC2[0]->name = '1'; + $component->RTC2[1]->name = '2'; + $component->name = '2'; + + $count = $this->dbh->count(); + + $component->save(); + + $this->assertEqual($this->dbh->count(), ($count + 5)); + + $this->assertEqual($component->RTC2->count(), 2); + + $component = $component->getTable()->find($component->id); + + $this->assertEqual($component->RTC2->count(), 2); + + // check that it doesn't matter saving the other M2M components as well + + $component->RTC1[0]->name = '1'; + $component->RTC1[1]->name = '2'; + + $count = $this->dbh->count(); + + $component->save(); + + $this->assertEqual($this->dbh->count(), ($count + 4)); + + $this->assertEqual($component->RTC1->count(), 2); + + $component = $component->getTable()->find($component->id); + + $this->assertEqual($component->RTC1->count(), 2); + + } +} +?> diff --git a/tests/RelationTestCase.php b/tests/RelationTestCase.php index ee6a67be4..a9a6504f7 100644 --- a/tests/RelationTestCase.php +++ b/tests/RelationTestCase.php @@ -7,8 +7,6 @@ class RelationTest extends Doctrine_Record { } public function setUp() { $this->ownsMany('OwnsOneToManyWithAlias as AliasO2M', 'AliasO2M.component_id'); - $this->hasMany('M2M as AliasM2M', 'JoinTable.c1_id'); - // $this->hasMany('M2M as AliasM2M2', 'JoinTable.c1_id'); } } class RelationTestChild extends RelationTest { @@ -25,12 +23,7 @@ class HasOneToOne extends Doctrine_Record { class HasOneToOneWithAlias extends Doctrine_Record { } -class JoinTable extends Doctrine_Record { - public function setTableDefinition() { - $this->hasColumn('c1_id', 'integer'); - $this->hasColumn('c2_id', 'integer'); - } -} + class HasManyWithAlias extends Doctrine_Record { } @@ -42,21 +35,14 @@ class OwnsOneToManyWithAlias extends Doctrine_Record { } } -class M2M extends Doctrine_Record { - public function setTableDefinition() { - $this->hasColumn('name', 'string', 200); - } - public function setUp() { - $this->hasMany('RelationTest as AliasM2M', 'JoinTable.c2_id'); - } -} + class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase { public function prepareData() { } public function prepareTables() { - $this->tables = array('M2M', 'RelationTest', 'JoinTable'); - parent::prepareTables(); } + + public function testOneToManyTreeRelationWithConcreteInheritance() { $component = new RelationTestChild(); @@ -98,36 +84,6 @@ class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase { $this->assertTrue($rel instanceof Doctrine_Relation_ForeignKey); } - public function testManyToManyHasRelationWithAliases() { - $component = new RelationTest(); - - try { - $rel = $component->getTable()->getRelation('AliasM2M'); - $this->pass(); - } catch(Doctrine_Exception $e) { - $this->fail(); - } - $this->assertTrue($rel instanceof Doctrine_Relation_Association); - - $this->assertTrue($component->AliasM2M instanceof Doctrine_Collection); - - $component->AliasM2M[0]->name = '1'; - $component->AliasM2M[1]->name = '2'; - $component->name = '2'; - - $count = $this->dbh->count(); - - $component->save(); - - $this->assertEqual($this->dbh->count(), ($count + 5)); - - $this->assertEqual($component->AliasM2M->count(), 2); - - $component = $component->getTable()->find($component->id); - - $this->assertEqual($component->AliasM2M->count(), 2); - } - public function testManyToManyRelation() { $this->manager->setAttribute(Doctrine::ATTR_CREATE_TABLES, false); diff --git a/tests/run.php b/tests/run.php index e4e4404ca..ac7f5919d 100644 --- a/tests/run.php +++ b/tests/run.php @@ -2,64 +2,70 @@ ob_start(); -require_once("ConfigurableTestCase.php"); -require_once("ManagerTestCase.php"); -require_once("ConnectionTestCase.php"); -require_once("ConnectionTransactionTestCase.php"); -require_once("TableTestCase.php"); -require_once("EventListenerTestCase.php"); -require_once("BatchIteratorTestCase.php"); -require_once("CacheFileTestCase.php"); +require_once('ConfigurableTestCase.php'); +require_once('ManagerTestCase.php'); +require_once('ConnectionTestCase.php'); +require_once('ConnectionTransactionTestCase.php'); +require_once('TableTestCase.php'); +require_once('EventListenerTestCase.php'); +require_once('BatchIteratorTestCase.php'); +require_once('CacheFileTestCase.php'); -require_once("RecordTestCase.php"); -require_once("RecordStateTestCase.php"); -require_once("RecordFilterTestCase.php"); +require_once('RecordTestCase.php'); +require_once('RecordStateTestCase.php'); +require_once('RecordFilterTestCase.php'); -require_once("AccessTestCase.php"); -require_once("ValidatorTestCase.php"); -require_once("CollectionTestCase.php"); -require_once("PessimisticLockingTestCase.php"); -require_once("EventListenerChainTestCase.php"); -require_once("CacheSqliteTestCase.php"); -require_once("CollectionOffsetTestCase.php"); +require_once('AccessTestCase.php'); +require_once('ValidatorTestCase.php'); +require_once('CollectionTestCase.php'); +require_once('PessimisticLockingTestCase.php'); +require_once('EventListenerChainTestCase.php'); +require_once('CacheSqliteTestCase.php'); +require_once('CollectionOffsetTestCase.php'); -require_once("CacheQuerySqliteTestCase.php"); -require_once("ViewTestCase.php"); -require_once("RawSqlTestCase.php"); -require_once("CustomPrimaryKeyTestCase.php"); -require_once("FilterTestCase.php"); +require_once('CacheQuerySqliteTestCase.php'); +require_once('ViewTestCase.php'); +require_once('RawSqlTestCase.php'); +require_once('CustomPrimaryKeyTestCase.php'); +require_once('FilterTestCase.php'); -require_once("QueryTestCase.php"); -require_once("QueryLimitTestCase.php"); -require_once("QueryMultiJoinTestCase.php"); -require_once("QueryReferenceModelTestCase.php"); -require_once("QueryWhereTestCase.php"); -require_once("QueryFromTestCase.php"); -require_once("QueryConditionTestCase.php"); -require_once("QueryComponentAliasTestCase.php"); -require_once("QuerySubqueryTestCase.php"); -require_once("QuerySelectTestCase.php"); -require_once("QueryDeleteTestCase.php"); -require_once("QueryUpdateTestCase.php"); +require_once('QueryTestCase.php'); +require_once('QueryLimitTestCase.php'); +require_once('QueryMultiJoinTestCase.php'); +require_once('QueryReferenceModelTestCase.php'); +require_once('QueryWhereTestCase.php'); +require_once('QueryFromTestCase.php'); +require_once('QueryConditionTestCase.php'); +require_once('QueryComponentAliasTestCase.php'); +require_once('QuerySubqueryTestCase.php'); +require_once('QuerySelectTestCase.php'); +require_once('QueryDeleteTestCase.php'); +require_once('QueryUpdateTestCase.php'); -require_once("DBTestCase.php"); -require_once("SchemaTestCase.php"); -require_once("ImportTestCase.php"); -require_once("BooleanTestCase.php"); -require_once("EnumTestCase.php"); -require_once("RelationAccessTestCase.php"); -require_once("RelationTestCase.php"); -require_once("DataDictSqliteTestCase.php"); -require_once("CustomResultSetOrderTestCase.php"); +require_once('RelationAccessTestCase.php'); +require_once('RelationTestCase.php'); +require_once('RelationManyToManyTestCase.php'); + + +require_once('DBTestCase.php'); +require_once('SchemaTestCase.php'); +require_once('ImportTestCase.php'); +require_once('BooleanTestCase.php'); +require_once('EnumTestCase.php'); + +require_once('DataDictSqliteTestCase.php'); +require_once('CustomResultSetOrderTestCase.php'); error_reporting(E_ALL); -print "
";
+print '
';
 
-$test = new GroupTest("Doctrine Framework Unit Tests");
+$test = new GroupTest('Doctrine Framework Unit Tests');
 
 
 $test->addTestCase(new Doctrine_Relation_TestCase());
 
+$test->addTestCase(new Doctrine_Relation_ManyToMany_TestCase());
+
 $test->addTestCase(new Doctrine_Record_TestCase());
 
 $test->addTestCase(new Doctrine_Record_State_TestCase());
@@ -174,9 +180,9 @@ if (TextReporter::inCli()) {
 } else {
     if (isset($_POST))
     {
-        $dsn        = isset($_POST['dsn'])?$_POST['dsn']:null;
-        $username   = isset($_POST['username'])?$_POST['username']:null;
-        $password   = isset($_POST['password'])?$_POST['password']:null;
+        $dsn        = isset($_POST["dsn"])?$_POST["dsn"]:null;
+        $username   = isset($_POST["username"])?$_POST["username"]:null;
+        $password   = isset($_POST["password"])?$_POST["password"]:null;
     }
     $test->run(new MyReporter());
     $output = ob_get_clean();