RelationAccessTestCase.php 7.57 KB
Newer Older
romanb's avatar
romanb committed
1 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 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 125 126 127 128 129 130 131 132 133 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 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 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
<?php
class Doctrine_RelationAccessTestCase extends Doctrine_UnitTestCase {
    public function prepareData() {
        $o1 = new File_Owner();
        $o1->name = "owner1";
        $o2 = new File_Owner();
        $o2->name = "owner2";

		$f1 = new Data_File();
        $f1->filename = 'file1';
		$f2 = new Data_File();
        $f2->filename = 'file2';
		$f3 = new Data_File();
        $f3->filename = 'file3';
		
        $o1->Data_File->filename = 'file4';

		// multiple left join branches test
		$us = array();
		$us[1] = new MyUser();
		$us[1]->name = "user1";
        $this->connection->flush();
		// 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];
			}
		}
		*/
        $this->connection->flush();
        $this->connection->clear();
	}
	
    public function prepareTables() {
        $this->tables = array("MyUser",
							  "MyOneThing",
							  "MyUserOneThing",
  							  "MyOtherThing",
  							  "MyUserOtherThing"); 
        parent::prepareTables();
    }
    
    public function testOneToOneAggregateRelationFetching() {
        $coll = $this->connection->query("FROM File_Owner.Data_File WHERE File_Owner.name = 'owner1'");
        $this->assertTrue(count($coll) == 1);
        $this->assertTrue($coll[0] instanceof Doctrine_Record);

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

	    $check = $this->connection->query("FROM File_Owner WHERE File_Owner.name = 'owner1'");
        $owner1 = $this->connection->query("FROM File_Owner.Data_File WHERE File_Owner.name = 'owner1'");
		$owner2 = $this->connection->query("FROM File_Owner.Data_File WHERE File_Owner.name = 'owner2'");
		$this->assertTrue(count($check) == 1);

		$this->assertTrue(count($owner2) == 1);

		$check = $check[0];
		$owner1 = $owner1[0];
		$owner2 = $owner2[0];
		$this->assertEqual($owner1->name, 'owner1');
        $this->assertEqual($owner1->id, 1);

		$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'));

        $this->assertTrue(isset($owner1->Data_File));
		$this->assertFalse(isset($owner2->Data_File));
        $this->assertEqual(1, $check->get('id'));
        $this->assertEqual(1, $owner1->get('id'));
		$this->assertEqual($owner1->get('id'), $check->get('id'));
		$this->assertEqual(2, $owner2->get('id'));

    }

	public function testAccessOneToOneFromLocalSide() {
	    $check = $this->connection->query("FROM Data_File WHERE Data_File.filename = 'file4'");
        $file1 = $this->connection->query("FROM Data_File.File_Owner WHERE Data_File.filename = 'file4'");
		$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'));
		
        $this->assertTrue(isset($file1->File_Owner));
		$this->assertFalse(isset($file2->File_Owner));
        $this->assertEqual(4, $check->get('id'));
        $this->assertEqual(4, $file1->get('id'));
		$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) {
	    if(!isset($check1[$oth->other_thing_id])) {
		  $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) {
	    if(!isset($check2[$one->one_thing_id])) {
		  $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");
					}
				}
			}
		}
	  }
	  
	  $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");
				}
			}
		  }
		}
	  }
	}
	
}
?>