RelationTestCase.php 4.21 KB
Newer Older
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */

/**
 * Doctrine_Relation_TestCase
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
33
class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase {
zYne's avatar
zYne committed
34 35 36 37

    public function prepareData() 
    { }
    public function prepareTables() 
zYne's avatar
zYne committed
38 39
    {
        $this->tables = array('RelationTest', 'RelationTestChild', 'Group', 'Groupuser', 'User', 'Email', 'Account', 'Phonenumber');
zYne's avatar
zYne committed
40
    }
41

zYne's avatar
zYne committed
42
    public function testOneToManyTreeRelationWithConcreteInheritance() {
zYne's avatar
zYne committed
43

zYne's avatar
zYne committed
44
        $component = new RelationTestChild();
zYne's avatar
zYne committed
45

zYne's avatar
zYne committed
46 47
        try {
            $rel = $component->getTable()->getRelation('Children');
zYne's avatar
zYne committed
48

zYne's avatar
zYne committed
49 50
            $this->pass();
        } catch(Doctrine_Exception $e) {
zYne's avatar
zYne committed
51

zYne's avatar
zYne committed
52 53 54
            $this->fail();
        }
        $this->assertTrue($rel instanceof Doctrine_Relation_ForeignKey);
zYne's avatar
zYne committed
55

zYne's avatar
zYne committed
56 57 58
        $this->assertTrue($component->Children instanceof Doctrine_Collection);
        $this->assertTrue($component->Children[0] instanceof RelationTestChild);
    }
zYne's avatar
zYne committed
59

zYne's avatar
zYne committed
60 61 62 63 64 65 66 67 68 69 70
    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);
    }
71 72
    public function testManyToManyRelation() {
        $user = new User();
zYne's avatar
zYne committed
73
         
74 75 76 77
        // test that join table relations can be initialized even before the association have been initialized
        try {
            $user->Groupuser;
            $this->pass();
zYne's avatar
zYne committed
78
        } catch(Doctrine_Exception $e) {
79 80
            $this->fail();
        }
zYne's avatar
zYne committed
81
        //$this->assertTrue($user->getTable()->getRelation('Groupuser') instanceof Doctrine_Relation_ForeignKey);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        $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
98
    }
zYne's avatar
zYne committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
}
class RelationTest extends Doctrine_Record 
{
    public function setTableDefinition() 
    {
        $this->hasColumn('name', 'string', 200);
        $this->hasColumn('child_id', 'integer');
    }
}
class RelationTestChild extends RelationTest 
{
    public function setUp() 
    {
        $this->hasOne('RelationTest as Parent', 'RelationTestChild.child_id');
zYne's avatar
zYne committed
113

zYne's avatar
zYne committed
114 115
        $this->ownsMany('RelationTestChild as Children', 'RelationTestChild.child_id');
    }
116
}