From cba5846b6aa1f8bd9a5567922572b46217343d21 Mon Sep 17 00:00:00 2001 From: Tanken Date: Sat, 30 Sep 2006 11:36:14 +0000 Subject: [PATCH] Added unit test for queries on multiple left join branches [ticket #139] Ticket: 139 --- tests/RelationAccessTestCase.php | 130 ++++++++++++++++++++++++++++++- tests/classes.php | 39 ++++++++++ 2 files changed, 167 insertions(+), 2 deletions(-) diff --git a/tests/RelationAccessTestCase.php b/tests/RelationAccessTestCase.php index c6ca60392..262d268a8 100644 --- a/tests/RelationAccessTestCase.php +++ b/tests/RelationAccessTestCase.php @@ -15,12 +15,65 @@ class Doctrine_RelationAccessTestCase extends Doctrine_UnitTestCase { $o1->Data_File->filename = 'file4'; + // multiple left join branches test + $us = array(); + $us[1] = new MyUser(); + $us[1]->name = "user1"; + $this->connection->flush(); + // OneThings + $onethings_gs = array( + array(6,1) + ); + $count = 1; + foreach($onethings_gs as $onething_g) { + for($i=$count;$i<$count+$onething_g[0];$i++) { + $d = new MyOneThing(); + $d->name = "onething".$i; + if($onething_g[1]) { + $us[$onething_g[1]]->MyOneThing->add($d); + } + } + $count += $onething_g[0]; + } + // OtherThings + for($i=0;$i<6;$i++) { + $o = new MyOtherThing(); + $o->name = "otherthing".$i; + $us[1]->MyOtherThing->add($o); + } + // UserOneThings + $one_id_gs = array( + array(array(2,3,6,5,1), 1) + ); + foreach($one_id_gs as $one_ids) { + foreach($one_ids[0] as $oid) { + $od = new MyUserOneThing(); + $od->one_thing_id = $oid; + $od->user_id = $one_ids[1]; + } + } + // UserOtherThings + $oth_id_gs = array( + array(array(5,4), 1) + ); + foreach($oth_id_gs as $oth_ids) { + foreach($oth_ids[0] as $oid) { + $uo = new MyUserOtherThing(); + $uo->other_thing_id = $oid; + $uo->user_id = $oth_ids[1]; + } + } + $this->connection->flush(); $this->connection->clear(); } public function prepareTables() { - $this->tables = array("File_Owner", "Data_File"); + $this->tables = array("MyUser", + "MyOneThing", + "MyUserOneThing", + "MyOtherThing", + "MyUserOtherThing"); parent::prepareTables(); } public function testOneToOneAggregateRelationFetching() { @@ -84,6 +137,79 @@ class Doctrine_RelationAccessTestCase extends Doctrine_UnitTestCase { $this->assertEqual(1, $file2->get('id')); } - + + public function testMultipleLeftJoinBranches() { + $query = "FROM MyUserOtherThing"; + $other = $this->connection->query($query); + $check1 = array(); + foreach($other as $oth) { + if(!isset($check1[$oth->other_thing_id])) { + $check1[$oth->other_thing_id] = array(); + } + $check1[$oth->other_thing_id][$oth->id] = $oth; + } + $query = "FROM MyUserOneThing"; + $ones = $this->connection->query($query); + $check2 = array(); + foreach($ones as $one) { + if(!isset($check2[$one->one_thing_id])) { + $check2[$one->one_thing_id] = array(); + } + $check2[$one->one_thing_id][$one->id] = $one; + } + + $query = "FROM MyUser, + MyUser.MyOneThing, + MyUser.MyOneThing.MyUserOneThing, + MyUser.MyOtherThing, + MyUser.MyOtherThing.MyUserOtherThing"; + $users = $this->connection->query($query); + foreach($users as $u) { + $this->assertEqual($u->MyOtherThing->count(), 6, "incorrect count of MyOtherThing"); + foreach($u->MyOtherThing as $o) { + $in_check = array_key_exists($o->id, $check1); + $wanted_user_thing_count = $in_check ? count($check1[$o->id]) : 0; + $this->assertEqual($o->MyUserOtherThing->count(), $wanted_user_thing_count, "incorrect count of MyUserOtherThing on MyOtherThing"); + foreach($o->MyUserOtherThing as $uo) { + $this->assertEqual($uo->other_thing_id, $o->id, "incorrectly assigned MyOtherThing.id on MyUserOtherThing"); + if($in_check) { + $wanted_user_thing_exists = array_key_exists($uo->id, $check1[$o->id]); + $this->assertTrue($wanted_user_thing_exists, "MyUserOtherThing incorrectly assigned to MyOtherThing."); + if($wanted_user_thing_exists) { + $this->assertEqual($uo->other_thing_id, $check1[$o->id][$uo->id]->user_id, "incorrect value of MyUserOtherThing.user_id"); + $this->assertEqual($uo->other_thing_id, $check1[$o->id][$uo->id]->other_thing_id, "incorrect value of MyUserOtherThing.other_thing_id"); + } + } + } + } + } + + $query = "FROM MyUser, + MyUser.MyOtherThing, + MyUser.MyOtherThing.MyUserOtherThing, + MyUser.MyOneThing, + MyUser.MyOneThing.MyUserOneThing"; + $users = $this->connection->query($query); + foreach($users as $u) { + $this->assertEqual($u->MyOneThing->count(), 6, "incorrect count of MyOneThing"); + foreach($u->MyOneThing as $o) { + $in_check = array_key_exists($o->id, $check2); + $wanted_user_thing_count = $in_check ? count($check2[$o->id]) : 0; + $this->assertEqual($o->MyUserOneThing->count(), $wanted_user_thing_count, "incorrect count of MyUserOneThing on MyOneThing"); + foreach($o->MyUserOneThing as $uo) { + $this->assertEqual($uo->one_thing_id, $o->id, "incorrectly assigned MyOneThing.id on MyUserOneThing"); + if($in_check) { + $wanted_user_thing_exists = array_key_exists($uo->id, $check2[$o->id]); + $this->assertTrue($wanted_user_thing_exists, "MyUserOneThing incorrectly assigned to MyOneThing."); + if($wanted_user_thing_exists) { + $this->assertEqual($uo->one_thing_id, $check2[$o->id][$uo->id]->user_id, "incorrect value of MyUserOneThing.user_id"); + $this->assertEqual($uo->one_thing_id, $check2[$o->id][$uo->id]->one_thing_id, "incorrect value of MyUserOneThing.one_thing_id"); + } + } + } + } + } + } + } ?> diff --git a/tests/classes.php b/tests/classes.php index e7de9841a..53ae0574e 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -467,4 +467,43 @@ class File_Owner extends Doctrine_Record { $this->hasOne("Data_File", "Data_File.file_owner_id"); } } +class MyUser extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("name", "string"); + } + public function setUp() { + $this->hasMany("MyOneThing", "MyOneThing.user_id"); + $this->hasMany("MyOtherThing", "MyOtherThing.user_id"); + } +} +class MyOneThing extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("name", "string"); + $this->hasColumn("user_id", "integer"); + } + public function setUp() { + $this->hasMany("MyUserOneThing", "MyUserOneThing.one_thing_id"); + } +} +class MyOtherThing extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("name", "string"); + $this->hasColumn("user_id", "integer"); + } + public function setUp() { + $this->hasMany("MyUserOtherThing", "MyUserOtherThing.other_thing_id"); + } +} +class MyUserOneThing extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("user_id", "integer"); + $this->hasColumn("one_thing_id", "integer"); + } +} +class MyUserOtherThing extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("user_id", "integer"); + $this->hasColumn("other_thing_id", "integer"); + } +} ?>