1<?php 2 3class Doctrine_Ticket_1323_TestCase extends Doctrine_UnitTestCase { 4 public function prepareTables() { 5 $this->tables = array(); 6 $this->tables[] = "T1323User"; 7 $this->tables[] = "T1323UserReference"; 8 parent::prepareTables(); 9 } 10 11 public function prepareData() {} 12 13 public function resetData() 14 { 15 $q = Doctrine_Query::create(); 16 $q->delete()->from("T1323UserReference")->execute(); 17 $q = Doctrine_Query::create(); 18 $q->delete()->from("T1323User")->execute(); 19 20 $m = new T1323User(); 21 $m->name = "Mother"; 22 $m->save(); 23 $f = new T1323User(); 24 $f->name = "Father"; 25 $f->save(); 26 $s = new T1323User(); 27 $s->name = "Son"; 28 $s->save(); 29 $d = new T1323User(); 30 $d->name = "Daughter"; 31 $d->save(); 32 $gf = new T1323User(); 33 $gf->name = "Grandfather"; 34 $gf->save(); 35 $gm = new T1323User(); 36 $gm->name = "Grandmother"; 37 $gm->save(); 38 39 $f->Children[] = $s; 40 $f->Children[] = $d; 41 42 $f->Parents[] = $gf; 43 $f->Parents[] = $gm; 44 45 $f->save(); 46 47 $m->Children[] = $s; 48 $m->Children[] = $d; 49 50 $m->save(); 51 52 } 53 54 public function testRelationsAreCorrect() { 55 $this->resetData(); 56 57 $f = Doctrine_Core::getTable("T1323User")->findOneByName("Father"); 58 $childLinks = $f->childLinks; 59 $this->assertEqual(2, count($childLinks)); 60 $this->assertEqual($f->id, $childLinks[0]->parent_id); 61 $this->assertEqual($f->id, $childLinks[1]->parent_id); 62 63 $parentLinks = $f->parentLinks; 64 $this->assertEqual(2, count($parentLinks)); 65 $this->assertEqual($f->id, $parentLinks[0]->child_id); 66 $this->assertEqual($f->id, $parentLinks[1]->child_id); 67 68 $m = Doctrine_Core::getTable("T1323User")->findOneByName("Mother"); 69 $childLinks = $m->childLinks; 70 $this->assertEqual(2, count($childLinks)); 71 $this->assertEqual($m->id, $childLinks[0]->parent_id); 72 $this->assertEqual($m->id, $childLinks[1]->parent_id); 73 74 $parentLinks = $m->parentLinks; 75 $this->assertEqual(0, count($parentLinks)); 76 77 $s = Doctrine_Core::getTable("T1323User")->findOneByName("Son"); 78 $childLinks = $s->childLinks; 79 $this->assertEqual(0, count($childLinks)); 80 $parentLinks = $s->parentLinks; 81 $this->assertEqual(2, count($parentLinks)); 82 $this->assertEqual($s->id, $parentLinks[0]->child_id); 83 $this->assertEqual($s->id, $parentLinks[1]->child_id); 84 85 $d = Doctrine_Core::getTable("T1323User")->findOneByName("Daughter"); 86 $childLinks = $d->childLinks; 87 $this->assertEqual(0, count($childLinks)); 88 $parentLinks = $d->parentLinks; 89 $this->assertEqual(2, count($parentLinks)); 90 $this->assertEqual($d->id, $parentLinks[0]->child_id); 91 $this->assertEqual($d->id, $parentLinks[1]->child_id); 92 93 $gm = Doctrine_Core::getTable("T1323User")->findOneByName("Grandmother"); 94 $childLinks = $gm->childLinks; 95 $this->assertEqual(1, count($childLinks)); 96 $this->assertEqual($gm->id, $childLinks[0]->parent_id); 97 $parentLinks = $gm->parentLinks; 98 $this->assertEqual(0, count($parentLinks)); 99 100 $gf = Doctrine_Core::getTable("T1323User")->findOneByName("Grandfather"); 101 $childLinks = $gf->childLinks; 102 $this->assertEqual(1, count($childLinks)); 103 $this->assertEqual($gf->id, $childLinks[0]->parent_id); 104 $parentLinks = $gf->parentLinks; 105 $this->assertEqual(0, count($parentLinks)); 106 } 107 108 /** 109 * this test will fail 110 */ 111 public function testWithShow() { 112 $this->resetData(); 113 114 T1323User::showAllRelations(); 115 $this->runTests(); 116 } 117 118 /** 119 * this test will pass 120 */ 121 public function testWithoutShow() { 122 $this->resetData(); 123 124 $this->runTests(); 125 } 126 127 128 public function runTests() { 129 130 // change "Father"'s name... 131 $f = Doctrine_Core::getTable("T1323User")->findOneByName("Father"); 132 $f->name = "Dad"; 133 $f->save(); 134 135 /* just playing; makes no difference: 136 remove "Dad"'s relation to "Son"... */ 137 //$s = Doctrine_Core::getTable("T1323User")->findOneByName("Son"); 138 //$f->unlink("Children", array($s->id)); 139 //$f->save(); 140 141 $relations = Doctrine_Core::getTable("T1323UserReference")->findAll(); 142 foreach ($relations as $relation) { 143 /* never directly touched any relation; so no user should have 144 himself as parent or child */ 145 $this->assertNotEqual($relation->parent_id, $relation->child_id); 146 } 147 } 148} 149 150 151 152class T1323User extends Doctrine_Record 153{ 154 public function setTableDefinition() 155 { 156 $this->hasColumn('name', 'string', 30); 157 } 158 159 public function setUp() 160 { 161 $this->hasMany('T1323User as Parents', array('local' => 'child_id', 162 'foreign' => 'parent_id', 163 'refClass' => 'T1323UserReference', 164 'refClassRelationAlias' => 'childLinks' 165 )); 166 167 $this->hasMany('T1323User as Children', array('local' => 'parent_id', 168 'foreign' => 'child_id', 169 'refClass' => 'T1323UserReference', 170 'refClassRelationAlias' => 'parentLinks' 171 )); 172 } 173 174 /** 175 * just a little function to show all users and their relations 176 */ 177 public static function showAllRelations() { 178 $users = Doctrine_Core::getTable("T1323User")->findAll(); 179 180 //echo "=========================================<br/>".PHP_EOL; 181 //echo "list of all existing users and their relations:<br/> ".PHP_EOL; 182 //echo "=========================================<br/><br/>".PHP_EOL.PHP_EOL; 183 184 foreach ($users as $user) { 185 $parents = $user->Parents; 186 $children = $user->Children; 187 188 /*echo "user: "; 189 echo $user->name; 190 echo PHP_EOL."<br/>"; 191 192 echo "parents:"; 193 echo PHP_EOL."<br/>"; 194 foreach ($parents as $parent) { 195 echo $parent->name; 196 echo PHP_EOL."<br/>"; 197 } 198 echo PHP_EOL."<br/>"; 199 200 echo "children:"; 201 echo PHP_EOL."<br/>"; 202 foreach ($children as $child) { 203 echo $child->name; 204 echo PHP_EOL."<br/>"; 205 } 206 echo PHP_EOL."<br/>"; 207 echo "--------------".PHP_EOL."<br/>"; 208 echo PHP_EOL."<br/>";*/ 209 } 210 } 211} 212 213class T1323UserReference extends Doctrine_Record 214{ 215 public function setTableDefinition() 216 { 217 //$this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true)); 218 $this->hasColumn('parent_id', 'integer', null, array('primary' => true)); 219 $this->hasColumn('child_id', 'integer', null, array('primary' => true)); 220 } 221} 222 223 224 225?> 226