ManyToManyTestCase.php 8.35 KB
Newer Older
1 2 3 4 5 6 7
<?php
class M2MTest extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn('name', 'string', 200);
        $this->hasColumn('child_id', 'integer');
    }
    public function setUp() {
zYne's avatar
zYne committed
8

9 10 11 12
        $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');
13 14 15

    }
}
zYne's avatar
zYne committed
16

17 18 19 20 21 22 23 24 25
class M2MTest2 extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn('oid', 'integer', 11, array('autoincrement', 'primary'));
        $this->hasColumn('name', 'string', 20);
    }
    public function setUp() {
        $this->hasMany('RTC4 as RTC5', 'JC3.c1_id');
    }
}
zYne's avatar
zYne committed
26

27 28 29 30
class JC3 extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn('c1_id', 'integer');
        $this->hasColumn('c2_id', 'integer');
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    }
}
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');
    }
}
70 71 72 73 74 75 76 77 78
class RTC4 extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn('oid', 'integer', 11, array('autoincrement', 'primary'));  
        $this->hasColumn('name', 'string', 20);
    }
    public function setUp() {
        $this->hasMany('M2MTest2', 'JC3.c2_id');
    }
}
79 80 81 82 83 84

class Doctrine_Relation_ManyToMany_TestCase extends Doctrine_UnitTestCase {
    public function prepareData() { }
    public function prepareTables() {
        parent::prepareTables();
    }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    public function testManyToManyRelationWithAliasesAndCustomPKs() {
        $component = new M2MTest2();

        try {
            $rel = $component->getTable()->getRelation('RTC5');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
        $this->assertTrue($rel instanceof Doctrine_Relation_Association);

        $this->assertTrue($component->RTC5 instanceof Doctrine_Collection);

        try {
            $rel = $component->getTable()->getRelation('JC3');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
        $this->assertEqual($rel->getLocal(), 'oid');
    }
    public function testJoinComponent() {
        $component = new JC3();
        
        try {
            $rel = $component->getTable()->getRelation('M2MTest2');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
        $this->assertEqual($rel->getForeign(), 'oid');
        try {
            $rel = $component->getTable()->getRelation('RTC4');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
        $this->assertEqual($rel->getForeign(), 'oid');
    }
zYne's avatar
zYne committed
125

126 127
    public function testManyToManyRelationFetchingWithAliasesAndCustomPKs2() {
        $q = new Doctrine_Query();
zYne's avatar
zYne committed
128

129 130 131 132 133 134 135 136 137 138 139 140 141
        try {
            $q->from('M2MTest2 m INNER JOIN m.JC3');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
        try {
            $q->execute();
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
    }
142 143 144
    public function testManyToManyHasRelationWithAliases4() {

        try {
zYne's avatar
zYne committed
145 146
            $component = new M2MTest();

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
    }

    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();
zYne's avatar
zYne committed
170

171 172 173 174 175 176 177 178 179 180 181 182 183
        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();
zYne's avatar
zYne committed
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        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);
zYne's avatar
zYne committed
270 271 272 273 274 275 276 277 278 279 280 281
    }
    
    public function testManyToManySimpleUpdate() {
        $component = $this->connection->getTable('M2MTest')->find(1);
        
        $this->assertEqual($component->name, 2);
        
        $component->name = 'changed name';
        
        $component->save();
        
        $this->assertEqual($component->name, 'changed name');
282 283 284
    }
}
?>