RelationTestCase.php 3.83 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);
6
        $this->hasColumn('child_id', 'integer');
zYne's avatar
zYne committed
7 8 9 10 11
    }
    public function setUp() {
        $this->ownsMany('OwnsOneToManyWithAlias as AliasO2M', 'AliasO2M.component_id');
    }
}
zYne's avatar
zYne committed
12 13
class RelationTestChild extends RelationTest {
    public function setUp() {
14
        $this->hasOne('RelationTest as Parent', 'RelationTestChild.child_id');
zYne's avatar
zYne committed
15 16 17 18 19

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

zYne's avatar
zYne committed
20 21 22 23 24 25
class HasOneToOne extends Doctrine_Record {

}
class HasOneToOneWithAlias extends Doctrine_Record {

}
26

zYne's avatar
zYne committed
27 28 29 30 31 32 33 34 35 36 37
class HasManyWithAlias extends Doctrine_Record {

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

    }
}
38

39 40
class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase {
    public function prepareData() { }
zYne's avatar
zYne committed
41
    public function prepareTables() {
42
        parent::prepareTables();
zYne's avatar
zYne committed
43
    }
44 45


zYne's avatar
zYne committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    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
60

zYne's avatar
zYne committed
61 62 63 64 65 66 67 68 69 70 71 72

    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
73
    public function testOneToManyOwnsRelationWithAliases() {
74

zYne's avatar
zYne committed
75 76 77 78 79 80 81 82 83 84 85 86
        
        $component = new RelationTest();
        
        try {
            $rel = $component->getTable()->getRelation('AliasO2M');
            $this->pass();
        } catch(Doctrine_Exception $e) {
            $this->fail();
        }

        $this->assertTrue($rel instanceof Doctrine_Relation_ForeignKey);
    }
zYne's avatar
zYne committed
87

88
    public function testManyToManyRelation() {
89
        $this->manager->setAttribute(Doctrine::ATTR_CREATE_TABLES, false);
90
        $user = new User();
zYne's avatar
zYne committed
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
        // 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
116
        $this->manager->setAttribute(Doctrine::ATTR_CREATE_TABLES, true);
117
    }
zYne's avatar
zYne committed
118

zYne's avatar
zYne committed
119

120
}