ClassTableInheritanceTest.php 10.1 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\ORM\Functional;

require_once __DIR__ . '/../../TestInit.php';

romanb's avatar
romanb committed
7 8 9 10 11 12
use Doctrine\Tests\Models\Company\CompanyPerson,
    Doctrine\Tests\Models\Company\CompanyEmployee,
    Doctrine\Tests\Models\Company\CompanyManager,
    Doctrine\Tests\Models\Company\CompanyOrganization,
    Doctrine\Tests\Models\Company\CompanyEvent,
    Doctrine\Tests\Models\Company\CompanyAuction,
13 14
    Doctrine\Tests\Models\Company\CompanyRaffle,
    Doctrine\Tests\Models\Company\CompanyCar;
15 16

/**
17
 * Functional tests for the Class Table Inheritance mapping strategy.
18 19 20 21 22 23 24 25 26 27 28
 *
 * @author robo
 */
class ClassTableInheritanceTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
    protected function setUp() {
        $this->useModelSet('company');
        parent::setUp();
    }

    public function testCRUD()
29
    {        
30 31 32
        $person = new CompanyPerson;
        $person->setName('Roman S. Borschel');
        
romanb's avatar
romanb committed
33
        $this->_em->persist($person);
34 35 36 37 38 39

        $employee = new CompanyEmployee;
        $employee->setName('Roman S. Borschel');
        $employee->setSalary(100000);
        $employee->setDepartment('IT');

romanb's avatar
romanb committed
40
        $this->_em->persist($employee);
41 42 43 44 45 46

        $employee->setName('Guilherme Blanco');
        $this->_em->flush();

        $this->_em->clear();
        
47
        $query = $this->_em->createQuery("select p from Doctrine\Tests\Models\Company\CompanyPerson p order by p.name desc");
48

49
        $entities = $query->getResult();
50 51 52 53 54 55 56 57 58 59 60 61 62 63

        $this->assertEquals(2, count($entities));
        $this->assertTrue($entities[0] instanceof CompanyPerson);
        $this->assertTrue($entities[1] instanceof CompanyEmployee);
        $this->assertTrue(is_numeric($entities[0]->getId()));
        $this->assertTrue(is_numeric($entities[1]->getId()));
        $this->assertEquals('Roman S. Borschel', $entities[0]->getName());
        $this->assertEquals('Guilherme Blanco', $entities[1]->getName());
        $this->assertEquals(100000, $entities[1]->getSalary());

        $this->_em->clear();
        
        $query = $this->_em->createQuery("select p from Doctrine\Tests\Models\Company\CompanyEmployee p");

64
        $entities = $query->getResult();
65 66 67 68 69 70 71 72

        $this->assertEquals(1, count($entities));
        $this->assertTrue($entities[0] instanceof CompanyEmployee);
        $this->assertTrue(is_numeric($entities[0]->getId()));
        $this->assertEquals('Guilherme Blanco', $entities[0]->getName());
        $this->assertEquals(100000, $entities[0]->getSalary());

        $this->_em->clear();
73 74
        
        //TODO: Test bulk UPDATE
75 76 77 78 79 80 81
        $query = $this->_em->createQuery("update Doctrine\Tests\Models\Company\CompanyEmployee p set p.name = ?1, p.department = ?2 where p.name='Guilherme Blanco' and p.salary = ?3");
        $query->setParameter(1, 'NewName');
        $query->setParameter(2, 'NewDepartment');
        $query->setParameter(3, 100000);
        $query->getSql();
        $numUpdated = $query->execute();
        $this->assertEquals(1, $numUpdated);
82 83 84 85
        
        $query = $this->_em->createQuery("delete from Doctrine\Tests\Models\Company\CompanyPerson p");
        $numDeleted = $query->execute();
        $this->assertEquals(2, $numDeleted);
86
    }
87 88 89 90 91 92 93
    
    public function testMultiLevelUpdateAndFind() {
    	$manager = new CompanyManager;
        $manager->setName('Roman S. Borschel');
        $manager->setSalary(100000);
        $manager->setDepartment('IT');
        $manager->setTitle('CTO');
romanb's avatar
romanb committed
94
        $this->_em->persist($manager);
95 96 97 98 99
        $this->_em->flush();
        
        $manager->setName('Roman B.');
        $manager->setSalary(119000);
        $manager->setTitle('CEO');
romanb's avatar
romanb committed
100
        $this->_em->persist($manager);
101 102 103 104 105 106
        $this->_em->flush();
        
        $this->_em->clear();
        
        $manager = $this->_em->find('Doctrine\Tests\Models\Company\CompanyManager', $manager->getId());
        
107
        $this->assertTrue($manager instanceof CompanyManager);
108 109 110 111 112 113
        $this->assertEquals('Roman B.', $manager->getName());
        $this->assertEquals(119000, $manager->getSalary());
        $this->assertEquals('CEO', $manager->getTitle());
        $this->assertTrue(is_numeric($manager->getId()));
    }
    
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    public function testFindOnBaseClass() {
        $manager = new CompanyManager;
        $manager->setName('Roman S. Borschel');
        $manager->setSalary(100000);
        $manager->setDepartment('IT');
        $manager->setTitle('CTO');
        $this->_em->persist($manager);
        $this->_em->flush();
        
        $this->_em->clear();
        
        $person = $this->_em->find('Doctrine\Tests\Models\Company\CompanyPerson', $manager->getId());
        
        $this->assertTrue($person instanceof CompanyManager);
        $this->assertEquals('Roman S. Borschel', $person->getName());
        $this->assertEquals(100000, $person->getSalary());
        $this->assertEquals('CTO', $person->getTitle());
        $this->assertTrue(is_numeric($person->getId()));
        //$this->assertTrue($person->getCar() instanceof CompanyCar);
    }
    
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    public function testSelfReferencingOneToOne() {
    	$manager = new CompanyManager;
        $manager->setName('John Smith');
        $manager->setSalary(100000);
        $manager->setDepartment('IT');
        $manager->setTitle('CTO');
        
        $wife = new CompanyPerson;
        $wife->setName('Mary Smith');
        $wife->setSpouse($manager);
        
        $this->assertSame($manager, $wife->getSpouse());
        $this->assertSame($wife, $manager->getSpouse());
        
romanb's avatar
romanb committed
149 150
        $this->_em->persist($manager);
        $this->_em->persist($wife);
151 152 153 154 155 156 157 158 159 160 161
        
        $this->_em->flush();
        
        //var_dump($this->_em->getConnection()->fetchAll('select * from company_persons'));
        //var_dump($this->_em->getConnection()->fetchAll('select * from company_employees'));
        //var_dump($this->_em->getConnection()->fetchAll('select * from company_managers'));
        
        $this->_em->clear();
        
        $query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\Company\CompanyPerson p join p.spouse s where p.name=\'Mary Smith\'');
        
162
        $result = $query->getResult();
163 164 165 166
        $this->assertEquals(1, count($result));
        $this->assertTrue($result[0] instanceof CompanyPerson);
        $this->assertEquals('Mary Smith', $result[0]->getName());
        $this->assertTrue($result[0]->getSpouse() instanceof CompanyEmployee);
167 168
        $this->assertEquals('John Smith', $result[0]->getSpouse()->getName());
        $this->assertSame($result[0], $result[0]->getSpouse()->getSpouse());
169
    }
romanb's avatar
romanb committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    
    public function testSelfReferencingManyToMany()
    {
        $person1 = new CompanyPerson;
        $person1->setName('Roman');
        
        $person2 = new CompanyPerson;
        $person2->setName('Jonathan');
        
        $person1->addFriend($person2);
        
        $this->assertEquals(1, count($person1->getFriends()));
        $this->assertEquals(1, count($person2->getFriends()));
        
        
romanb's avatar
romanb committed
185 186
        $this->_em->persist($person1);
        $this->_em->persist($person2);
romanb's avatar
romanb committed
187 188 189 190 191 192 193 194
        
        $this->_em->flush();
        
        $this->_em->clear();
        
        $query = $this->_em->createQuery('select p, f from Doctrine\Tests\Models\Company\CompanyPerson p join p.friends f where p.name=?1');
        $query->setParameter(1, 'Roman');
        
195
        $result = $query->getResult();
romanb's avatar
romanb committed
196 197 198 199 200 201 202
        $this->assertEquals(1, count($result));
        $this->assertEquals(1, count($result[0]->getFriends()));
        $this->assertEquals('Roman', $result[0]->getName());
        
        $friends = $result[0]->getFriends();
        $this->assertEquals('Jonathan', $friends[0]->getName());
    }
romanb's avatar
romanb committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    
    public function testLazyLoading1()
    {
        $org = new CompanyOrganization;
        $event1 = new CompanyAuction;
        $event1->setData('auction');
        $org->addEvent($event1);
        $event2 = new CompanyRaffle;
        $event2->setData('raffle');
        $org->addEvent($event2);
        
        $this->_em->persist($org);
        $this->_em->flush();
        $this->_em->clear();
        
        $orgId = $org->getId();
        
        $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyOrganization a where a.id = ?1');
        $q->setParameter(1, $orgId);
        
        $result = $q->getResult();
        
        $this->assertEquals(1, count($result));
        $this->assertTrue($result[0] instanceof CompanyOrganization);
227
        $this->assertNull($result[0]->getMainEvent());
romanb's avatar
romanb committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241
        
        $events = $result[0]->getEvents();
        
        $this->assertTrue($events instanceof \Doctrine\ORM\PersistentCollection);
        $this->assertFalse($events->isInitialized());
        
        $this->assertEquals(2, count($events));
        if ($events[0] instanceof CompanyAuction) {
            $this->assertTrue($events[1] instanceof CompanyRaffle);
        } else {
            $this->assertTrue($events[0] instanceof CompanyRaffle);
            $this->assertTrue($events[1] instanceof CompanyAuction);
        }
    }
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    
    public function testLazyLoading2()
    {
        $org = new CompanyOrganization;
        $event1 = new CompanyAuction;
        $event1->setData('auction');
        $org->setMainEvent($event1);

        $this->_em->persist($org);
        $this->_em->flush();
        $this->_em->clear();

        $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyEvent a where a.id = ?1');
        $q->setParameter(1, $event1->getId());

        $result = $q->getResult();
        $this->assertEquals(1, count($result));
        $this->assertTrue($result[0] instanceof CompanyAuction, sprintf("Is of class %s",get_class($result[0])));

        $this->_em->clear();

        $q = $this->_em->createQuery('select a from Doctrine\Tests\Models\Company\CompanyOrganization a where a.id = ?1');
        $q->setParameter(1, $org->getId());

        $result = $q->getResult();

        $this->assertEquals(1, count($result));
        $this->assertTrue($result[0] instanceof CompanyOrganization);

        $mainEvent = $result[0]->getMainEvent();
        // mainEvent should have been loaded because it can't be lazy
        $this->assertTrue($mainEvent instanceof CompanyAuction);
        $this->assertFalse($mainEvent instanceof \Doctrine\ORM\Proxy\Proxy);
    }
    
278
}