| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class Doctrine_Ticket_1323_TestCase extends Doctrine_UnitTestCase { |
|---|
| 4 | public function prepareTables() { |
|---|
| 5 | $this->tables[] = "T1323User"; |
|---|
| 6 | $this->tables[] = "T1323UserReference"; |
|---|
| 7 | parent::prepareTables(); |
|---|
| 8 | } |
|---|
| 9 | |
|---|
| 10 | public function resetData() |
|---|
| 11 | { |
|---|
| 12 | $q = Doctrine_Query::create(); |
|---|
| 13 | $q->delete()->from("T1323UserReference")->execute(); |
|---|
| 14 | $q = Doctrine_Query::create(); |
|---|
| 15 | $q->delete()->from("T1323User")->execute(); |
|---|
| 16 | |
|---|
| 17 | $m = new T1323User(); |
|---|
| 18 | $m->name = "Mother"; |
|---|
| 19 | $m->save(); |
|---|
| 20 | $f = new T1323User(); |
|---|
| 21 | $f->name = "Father"; |
|---|
| 22 | $f->save(); |
|---|
| 23 | $s = new T1323User(); |
|---|
| 24 | $s->name = "Son"; |
|---|
| 25 | $s->save(); |
|---|
| 26 | $d = new T1323User(); |
|---|
| 27 | $d->name = "Daughter"; |
|---|
| 28 | $d->save(); |
|---|
| 29 | $gf = new T1323User(); |
|---|
| 30 | $gf->name = "Grandfather"; |
|---|
| 31 | $gf->save(); |
|---|
| 32 | $gm = new T1323User(); |
|---|
| 33 | $gm->name = "Grandmother"; |
|---|
| 34 | $gm->save(); |
|---|
| 35 | |
|---|
| 36 | $f->Children[] = $s; |
|---|
| 37 | $f->Children[] = $d; |
|---|
| 38 | |
|---|
| 39 | $f->Parents[] = $gf; |
|---|
| 40 | $f->Parents[] = $gm; |
|---|
| 41 | |
|---|
| 42 | $f->save(); |
|---|
| 43 | |
|---|
| 44 | $m->Children[] = $s; |
|---|
| 45 | $m->Children[] = $d; |
|---|
| 46 | |
|---|
| 47 | $m->save(); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * this test will fail |
|---|
| 52 | */ |
|---|
| 53 | public function testWithShow() { |
|---|
| 54 | $this->resetData(); |
|---|
| 55 | |
|---|
| 56 | T1323User::showAllRelations(); |
|---|
| 57 | $this->runTests(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * this test will pass |
|---|
| 62 | */ |
|---|
| 63 | public function testWithoutShow() { |
|---|
| 64 | $this->resetData(); |
|---|
| 65 | |
|---|
| 66 | $this->runTests(); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | public function runTests() { |
|---|
| 71 | // change "Father"'s name... |
|---|
| 72 | $f = Doctrine::getTable("T1323User")->findOneByName("Father"); |
|---|
| 73 | $f->name = "Dad"; |
|---|
| 74 | $f->save(); |
|---|
| 75 | |
|---|
| 76 | /* just playing; makes no difference: |
|---|
| 77 | remove "Dad"'s relation to "Son"... */ |
|---|
| 78 | //$s = Doctrine::getTable("T1323User")->findOneByName("Son"); |
|---|
| 79 | //$f->unlink("Children", array($s->id)); |
|---|
| 80 | //$f->save(); |
|---|
| 81 | |
|---|
| 82 | $relations = Doctrine::getTable("T1323UserReference")->findAll(); |
|---|
| 83 | foreach ($relations as $relation) { |
|---|
| 84 | /* never directly touched any relation; so no user should have |
|---|
| 85 | himself as parent or child */ |
|---|
| 86 | $this->assertNotEqual($relation->parent_id, $relation->child_id); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | class T1323User extends Doctrine_Record |
|---|
| 94 | { |
|---|
| 95 | public function setTableDefinition() |
|---|
| 96 | { |
|---|
| 97 | $this->hasColumn('name', 'string', 30); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | public function setUp() |
|---|
| 101 | { |
|---|
| 102 | $this->hasMany('T1323User as Parents', array('local' => 'parent_id', |
|---|
| 103 | 'foreign' => 'child_id', |
|---|
| 104 | 'refClass' => 'T1323UserReference' |
|---|
| 105 | )); |
|---|
| 106 | |
|---|
| 107 | $this->hasMany('T1323User as Children', array('local' => 'child_id', |
|---|
| 108 | 'foreign' => 'parent_id', |
|---|
| 109 | 'refClass' => 'T1323UserReference' |
|---|
| 110 | )); |
|---|
| 111 | |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * just a little function to show all users and their relations |
|---|
| 116 | */ |
|---|
| 117 | public static function showAllRelations() { |
|---|
| 118 | $users = Doctrine::getTable("T1323User")->findAll(); |
|---|
| 119 | |
|---|
| 120 | echo "=========================================\n"; |
|---|
| 121 | echo "list of all existing users and their relations: \n"; |
|---|
| 122 | echo "=========================================\n\n"; |
|---|
| 123 | |
|---|
| 124 | foreach ($users as $user) { |
|---|
| 125 | $parents = $user->Parents; |
|---|
| 126 | $children = $user->Children; |
|---|
| 127 | |
|---|
| 128 | echo "user: "; |
|---|
| 129 | echo $user->name; |
|---|
| 130 | echo "\n"; |
|---|
| 131 | |
|---|
| 132 | echo "parents:"; |
|---|
| 133 | echo "\n"; |
|---|
| 134 | foreach ($parents as $parent) { |
|---|
| 135 | echo $parent->name; |
|---|
| 136 | echo "\n"; |
|---|
| 137 | } |
|---|
| 138 | echo "\n"; |
|---|
| 139 | |
|---|
| 140 | echo "children:"; |
|---|
| 141 | echo "\n"; |
|---|
| 142 | foreach ($children as $child) { |
|---|
| 143 | echo $child->name; |
|---|
| 144 | echo "\n"; |
|---|
| 145 | } |
|---|
| 146 | echo "\n"; |
|---|
| 147 | echo "--------------\n"; |
|---|
| 148 | echo "\n"; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | class T1323UserReference extends Doctrine_Record |
|---|
| 154 | { |
|---|
| 155 | public function setTableDefinition() |
|---|
| 156 | { |
|---|
| 157 | $this->hasColumn('id', 'integer', null, array('primary' => true, 'autoincrement' => true)); |
|---|
| 158 | $this->hasColumn('parent_id', 'integer', null); |
|---|
| 159 | $this->hasColumn('child_id', 'integer', null); |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | ?> |
|---|