RelationTestCase.php 5.41 KB
Newer Older
1
<?php
2

zYne's avatar
zYne committed
3 4
class RelationTest extends Doctrine_Record {
    public function setTableDefinition() {
5
        $this->hasColumn('name', 'string', 200);
zYne's avatar
zYne committed
6
        $this->hasColumn("child_id", "integer");
zYne's avatar
zYne committed
7 8 9 10 11 12
    }
    public function setUp() {
        $this->ownsMany('OwnsOneToManyWithAlias as AliasO2M', 'AliasO2M.component_id');
        $this->hasMany('HasManyToManyWithAlias as AliasM2M', 'JoinTable.c1_id');
    }
}
zYne's avatar
zYne committed
13 14
class RelationTestChild extends RelationTest {
    public function setUp() {
15
        $this->hasOne('RelationTest as Parent', 'RelationTestChild.child_id');
zYne's avatar
zYne committed
16 17 18 19 20

        $this->ownsMany('RelationTestChild as Children', 'RelationTestChild.child_id');
    }
}

zYne's avatar
zYne committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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 {

}
class OwnsOneToManyWithAlias extends Doctrine_Record {
    public function setTableDefinition() {
        $this->hasColumn('component_id', 'integer');
    }
    public function setUp() {

    }
}
class HasManyToManyWithAlias extends Doctrine_Record {
45 46 47
    public function setTableDefinition() { 
        $this->hasColumn('name', 'string', 200);
    }
zYne's avatar
zYne committed
48 49 50 51
    public function setUp() {
        $this->hasMany('RelationTest as AliasM2M', 'JoinTable.c2_id');
    }
}
52 53
class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase {
    public function prepareData() { }
zYne's avatar
zYne committed
54
    public function prepareTables() {
55 56 57
        $this->tables = array('HasManyToManyWithAlias', 'RelationTest', 'JoinTable');
        
        parent::prepareTables();
zYne's avatar
zYne committed
58
    }
zYne's avatar
zYne committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72
    public function testOneToManyTreeRelationWithConcreteInheritance() {
        $component = new RelationTestChild();
        
        try {
            $rel = $component->getTable()->getRelation('Children');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
        $this->assertTrue($rel instanceof Doctrine_Relation_ForeignKey);
        
        $this->assertTrue($component->Children instanceof Doctrine_Collection);
        $this->assertTrue($component->Children[0] instanceof RelationTestChild);
    }
zYne's avatar
zYne committed
73

zYne's avatar
zYne committed
74 75 76 77 78 79 80 81 82 83 84 85

    public function testOneToOneTreeRelationWithConcreteInheritance() {
        $component = new RelationTestChild();
        
        try {
            $rel = $component->getTable()->getRelation('Parent');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }
        $this->assertTrue($rel instanceof Doctrine_Relation_LocalKey);
    }
zYne's avatar
zYne committed
86
    public function testOneToManyOwnsRelationWithAliases() {
87

zYne's avatar
zYne committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        
        $component = new RelationTest();
        
        try {
            $rel = $component->getTable()->getRelation('AliasO2M');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }

        $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);
zYne's avatar
zYne committed
110 111 112
        
        $this->assertTrue($component->AliasM2M instanceof Doctrine_Collection);

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        $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);
128
    }
zYne's avatar
zYne committed
129 130


131
    public function testManyToManyRelation() {
132
        $this->manager->setAttribute(Doctrine::ATTR_CREATE_TABLES, false);
133
        $user = new User();
zYne's avatar
zYne committed
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        // test that join table relations can be initialized even before the association have been initialized
        try {
            $user->Groupuser;
            $this->pass();
        } catch(Doctrine_Table_Exception $e) {
            $this->fail();
        }
        $this->assertTrue($user->getTable()->getRelation('Groupuser') instanceof Doctrine_Relation_ForeignKey);
        $this->assertTrue($user->getTable()->getRelation('Group') instanceof Doctrine_Relation_Association);
    }
    public function testOneToOneLocalKeyRelation() {
        $user = new User();
        
        $this->assertTrue($user->getTable()->getRelation('Email') instanceof Doctrine_Relation_LocalKey);
    }
    public function testOneToOneForeignKeyRelation() {
        $user = new User();
        
        $this->assertTrue($user->getTable()->getRelation('Account') instanceof Doctrine_Relation_ForeignKey);
    }
    public function testOneToManyForeignKeyRelation() {
        $user = new User();
        
        $this->assertTrue($user->getTable()->getRelation('Phonenumber') instanceof Doctrine_Relation_ForeignKey);
zYne's avatar
zYne committed
159
        $this->manager->setAttribute(Doctrine::ATTR_CREATE_TABLES, true);
160
    }
zYne's avatar
zYne committed
161

zYne's avatar
zYne committed
162

163
}