AccessTestCase.php 8.91 KB
Newer Older
romanb's avatar
romanb committed
1
<?php
zYne's avatar
zYne committed
2
class Doctrine_Relation_Access_TestCase extends Doctrine_UnitTestCase {
romanb's avatar
romanb committed
3 4 5 6 7 8
    public function prepareData() {
        $o1 = new File_Owner();
        $o1->name = "owner1";
        $o2 = new File_Owner();
        $o2->name = "owner2";

meus's avatar
meus committed
9
        $f1 = new Data_File();
romanb's avatar
romanb committed
10
        $f1->filename = 'file1';
meus's avatar
meus committed
11
        $f2 = new Data_File();
romanb's avatar
romanb committed
12
        $f2->filename = 'file2';
meus's avatar
meus committed
13
        $f3 = new Data_File();
romanb's avatar
romanb committed
14
        $f3->filename = 'file3';
meus's avatar
meus committed
15

romanb's avatar
romanb committed
16 17
        $o1->Data_File->filename = 'file4';

meus's avatar
meus committed
18 19 20 21
        // multiple left join branches test
        $us = array();
        $us[1] = new MyUser();
        $us[1]->name = "user1";
22
        $this->connection->unitOfWork->saveAll();
meus's avatar
meus committed
23 24 25 26 27 28 29 30 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
        // OneThings
        $onethings_gs = array(
            array(6,1)
        );
        $count = 1;
        foreach($onethings_gs as $onething_g) {
            for($i=$count;$i<$count+$onething_g[0];$i++) {
                $d = new MyOneThing();
                $d->name = "onething".$i;
                if($onething_g[1]) {
                    $us[$onething_g[1]]->MyOneThing->add($d);
                }
            }
            $count += $onething_g[0];
        }
        // OtherThings
        for($i=0;$i<6;$i++) {
            $o = new MyOtherThing();
            $o->name = "otherthing".$i;
            $us[1]->MyOtherThing->add($o);
        }
        // UserOneThings
        /* Doctrine assigns the foreign keys automatically
        $one_id_gs = array(
                    array(array(2,3,6,5,1), 1)
                    );
        foreach($one_id_gs as $one_ids) {
            foreach($one_ids[0] as $oid) {
                $od = new MyUserOneThing();
                $od->one_thing_id = $oid;
                $od->user_id = $one_ids[1];
            }
        }
        // UserOtherThings
        $oth_id_gs = array(
                    array(array(5,4), 1)
                    );
        foreach($oth_id_gs as $oth_ids) {
            foreach($oth_ids[0] as $oid) {
                $uo = new MyUserOtherThing();
                $uo->other_thing_id = $oid;
                $uo->user_id = $oth_ids[1];
            }
        }
         */
68
        $this->connection->unitOfWork->saveAll();
romanb's avatar
romanb committed
69
        $this->connection->clear();
meus's avatar
meus committed
70 71
    }

romanb's avatar
romanb committed
72
    public function prepareTables() {
73
        $this->tables += array("MyUser",
meus's avatar
meus committed
74 75 76 77
            "MyOneThing",
            "MyUserOneThing",
            "MyOtherThing",
            "MyUserOtherThing"); 
romanb's avatar
romanb committed
78 79
        parent::prepareTables();
    }
meus's avatar
meus committed
80

romanb's avatar
romanb committed
81 82 83
    public function testOneToOneAggregateRelationFetching() {
        $coll = $this->connection->query("FROM File_Owner.Data_File WHERE File_Owner.name = 'owner1'");
        $this->assertTrue(count($coll) == 1);
84
        $this->assertTrue($coll[0] instanceof Doctrine_Entity);
romanb's avatar
romanb committed
85 86 87 88 89

        $this->assertEqual($coll[0]->id, 1);
    }
    public function testAccessOneToOneFromForeignSide() {

meus's avatar
meus committed
90
        $check = $this->connection->query("FROM File_Owner WHERE File_Owner.name = 'owner1'");
romanb's avatar
romanb committed
91
        $owner1 = $this->connection->query("FROM File_Owner.Data_File WHERE File_Owner.name = 'owner1'");
meus's avatar
meus committed
92 93
        $owner2 = $this->connection->query("FROM File_Owner.Data_File WHERE File_Owner.name = 'owner2'");
        $this->assertTrue(count($check) == 1);
romanb's avatar
romanb committed
94

meus's avatar
meus committed
95
        $this->assertTrue(count($owner2) == 1);
romanb's avatar
romanb committed
96

meus's avatar
meus committed
97 98 99 100
        $check = $check[0];
        $owner1 = $owner1[0];
        $owner2 = $owner2[0];
        $this->assertEqual($owner1->name, 'owner1');
romanb's avatar
romanb committed
101 102
        $this->assertEqual($owner1->id, 1);

meus's avatar
meus committed
103 104 105 106
        $check2 = $this->connection->query("FROM File_Owner WHERE File_Owner.id = ".$owner1->get('id'));
        $this->assertEqual(1, count($check2));
        $check2 = $check2[0];
        $this->assertEqual('owner1', $check2->get('name'));
romanb's avatar
romanb committed
107 108

        $this->assertTrue(isset($owner1->Data_File));
meus's avatar
meus committed
109
        $this->assertFalse(isset($owner2->Data_File));
romanb's avatar
romanb committed
110 111
        $this->assertEqual(1, $check->get('id'));
        $this->assertEqual(1, $owner1->get('id'));
meus's avatar
meus committed
112 113
        $this->assertEqual($owner1->get('id'), $check->get('id'));
        $this->assertEqual(2, $owner2->get('id'));
romanb's avatar
romanb committed
114 115 116

    }

meus's avatar
meus committed
117 118
    public function testAccessOneToOneFromLocalSide() {
        $check = $this->connection->query("FROM Data_File WHERE Data_File.filename = 'file4'");
romanb's avatar
romanb committed
119
        $file1 = $this->connection->query("FROM Data_File.File_Owner WHERE Data_File.filename = 'file4'");
meus's avatar
meus committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133
        $file2 = $this->connection->query("FROM Data_File.File_Owner WHERE Data_File.filename = 'file1'");
        $this->assertTrue(count($check) == 1);
        $this->assertTrue(count($file1) == 1);
        $this->assertTrue(count($file2) == 1);

        $check = $check[0];
        $file1 = $file1[0];
        $file2 = $file2[0];

        $check2 = $this->connection->query("FROM Data_File WHERE Data_File.id = ".$file1->get('id'));
        $this->assertEqual(1, count($check2));
        $check2 = $check2[0];
        $this->assertEqual('file4', $check2->get('filename'));

romanb's avatar
romanb committed
134
        $this->assertTrue(isset($file1->File_Owner));
meus's avatar
meus committed
135
        $this->assertFalse(isset($file2->File_Owner));
romanb's avatar
romanb committed
136 137
        $this->assertEqual(4, $check->get('id'));
        $this->assertEqual(4, $file1->get('id'));
meus's avatar
meus committed
138 139 140 141 142 143 144 145 146 147
        $this->assertEqual($file1->get('id'), $check->get('id'));
        $this->assertEqual(1, $file2->get('id'));

    }

    public function testMultipleLeftJoinBranches() {
        $query = "FROM MyUserOtherThing";
        $other = $this->connection->query($query);
        $check1 = array();
        foreach($other as $oth) {
148
            if( ! isset($check1[$oth->other_thing_id])) {
meus's avatar
meus committed
149 150 151 152 153 154 155 156
                $check1[$oth->other_thing_id] = array();
            }
            $check1[$oth->other_thing_id][$oth->id] = $oth;
        }
        $query = "FROM MyUserOneThing";
        $ones = $this->connection->query($query);
        $check2 = array();
        foreach($ones as $one) {
157
            if( ! isset($check2[$one->one_thing_id])) {
meus's avatar
meus committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                $check2[$one->one_thing_id] = array();
            }
            $check2[$one->one_thing_id][$one->id] = $one;
        }

        $query = "FROM MyUser,
            MyUser.MyOneThing,
            MyUser.MyOneThing.MyUserOneThing,
            MyUser.MyOtherThing,
            MyUser.MyOtherThing.MyUserOtherThing";
        $users = $this->connection->query($query);
        foreach($users as $u) {
            $this->assertEqual($u->MyOtherThing->count(), 6, "incorrect count of MyOtherThing");
            foreach($u->MyOtherThing as $o) {
                $in_check = array_key_exists($o->id, $check1);
                $wanted_user_thing_count = $in_check ? count($check1[$o->id]) : 0;
                $this->assertEqual($o->MyUserOtherThing->count(), $wanted_user_thing_count, "incorrect count of MyUserOtherThing on MyOtherThing");
                foreach($o->MyUserOtherThing as $uo) {
                    $this->assertEqual($uo->other_thing_id, $o->id, "incorrectly assigned MyOtherThing.id on MyUserOtherThing");
                    if($in_check) {
                        $wanted_user_thing_exists = array_key_exists($uo->id, $check1[$o->id]);
                        $this->assertTrue($wanted_user_thing_exists, "MyUserOtherThing incorrectly assigned to MyOtherThing.");
                        if($wanted_user_thing_exists) {
                            $this->assertEqual($uo->other_thing_id, $check1[$o->id][$uo->id]->user_id, "incorrect value of MyUserOtherThing.user_id");
                            $this->assertEqual($uo->other_thing_id, $check1[$o->id][$uo->id]->other_thing_id, "incorrect value of MyUserOtherThing.other_thing_id");
                        }
                    }
                }
            }
        }
romanb's avatar
romanb committed
188

meus's avatar
meus committed
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
        $query = "FROM MyUser,
            MyUser.MyOtherThing,
            MyUser.MyOtherThing.MyUserOtherThing,
            MyUser.MyOneThing,
            MyUser.MyOneThing.MyUserOneThing";
        $users = $this->connection->query($query);
        foreach($users as $u) {
            $this->assertEqual($u->MyOneThing->count(), 6, "incorrect count of MyOneThing");
            foreach($u->MyOneThing as $o) {
                $in_check = array_key_exists($o->id, $check2);
                $wanted_user_thing_count = $in_check ? count($check2[$o->id]) : 0;
                $this->assertEqual($o->MyUserOneThing->count(), $wanted_user_thing_count, "incorrect count of MyUserOneThing on MyOneThing");
                foreach($o->MyUserOneThing as $uo) {
                    $this->assertEqual($uo->one_thing_id, $o->id, "incorrectly assigned MyOneThing.id on MyUserOneThing");
                    if($in_check) {
                        $wanted_user_thing_exists = array_key_exists($uo->id, $check2[$o->id]);
                        $this->assertTrue($wanted_user_thing_exists, "MyUserOneThing incorrectly assigned to MyOneThing.");
                        if($wanted_user_thing_exists) {
                            $this->assertEqual($uo->one_thing_id, $check2[$o->id][$uo->id]->user_id, "incorrect value of MyUserOneThing.user_id");
                            $this->assertEqual($uo->one_thing_id, $check2[$o->id][$uo->id]->one_thing_id, "incorrect value of MyUserOneThing.one_thing_id");
                        }
                    }
                }
            }
        }
romanb's avatar
romanb committed
214 215
    }
}