BasicFunctionalTest.php 14.4 KB
Newer Older
1 2
<?php

3 4
namespace Doctrine\Tests\ORM\Functional;

5
use Doctrine\ORM\Tools\SchemaTool;
6 7
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
8
use Doctrine\Tests\Models\CMS\CmsAddress;
9
use Doctrine\Tests\Models\CMS\CmsGroup;
10

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

13
class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
14
{
15
    protected function setUp()
16
    {
17 18
        $this->useModelSet('cms');
        parent::setUp();
19
    }
20

21 22
    public function testBasicUnitsOfWorkWithOneToManyAssociation()
    {
23 24
        // Create
        $user = new CmsUser;
25 26 27
        $user->name = 'Roman';
        $user->username = 'romanb';
        $user->status = 'developer';
romanb's avatar
romanb committed
28
        $this->_em->persist($user);
29
       
romanb's avatar
romanb committed
30 31
        $this->_em->flush();
        
32
        $this->assertTrue(is_numeric($user->id));
33
        $this->assertTrue($this->_em->contains($user));
34 35

        // Read
36
        $user2 = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
37
        $this->assertTrue($user === $user2);
romanb's avatar
romanb committed
38

39
        // Add a phonenumber
romanb's avatar
romanb committed
40 41
        $ph = new CmsPhonenumber;
        $ph->phonenumber = "12345";
42
        $user->addPhonenumber($ph);
43 44 45
        $this->_em->flush();
        $this->assertTrue($this->_em->contains($ph));
        $this->assertTrue($this->_em->contains($user));
46
        //$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
romanb's avatar
romanb committed
47

48
        // Update name
49
        $user->name = 'guilherme';
50
        $this->_em->flush();
51 52
        $this->assertEquals('guilherme', $user->name);

53 54 55 56
        // Add another phonenumber
        $ph2 = new CmsPhonenumber;
        $ph2->phonenumber = "6789";
        $user->addPhonenumber($ph2);
57 58
        $this->_em->flush();
        $this->assertTrue($this->_em->contains($ph2));
59

60
        // Delete
romanb's avatar
romanb committed
61 62 63 64
        $this->_em->remove($user);
        $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($user));
        $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
        $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
65
        $this->_em->flush();
romanb's avatar
romanb committed
66 67 68
        $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($user));
        $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
        $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
69
    }
romanb's avatar
romanb committed
70

71 72
    public function testOneToManyAssociationModification()
    {
73 74 75 76
        $user = new CmsUser;
        $user->name = 'Roman';
        $user->username = 'romanb';
        $user->status = 'developer';
77

78 79 80 81 82 83 84 85
        $ph1 = new CmsPhonenumber;
        $ph1->phonenumber = "0301234";
        $ph2 = new CmsPhonenumber;
        $ph2->phonenumber = "987654321";

        $user->addPhonenumber($ph1);
        $user->addPhonenumber($ph2);

romanb's avatar
romanb committed
86
        $this->_em->persist($user);
87 88
        $this->_em->flush();

89
        //$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
90 91 92 93

        // Remove the first element from the collection
        unset($user->phonenumbers[0]);
        $ph1->user = null; // owning side!
94

95 96 97 98 99 100 101 102 103 104 105 106
        $this->_em->flush();

        $this->assertEquals(1, count($user->phonenumbers));
        $this->assertNull($ph1->user);
    }

    public function testBasicOneToOne()
    {
        $user = new CmsUser;
        $user->name = 'Roman';
        $user->username = 'romanb';
        $user->status = 'developer';
107

108 109 110 111 112 113 114 115
        $address = new CmsAddress;
        $address->country = 'Germany';
        $address->city = 'Berlin';
        $address->zip = '12345';

        $user->address = $address; // inverse side
        $address->user = $user; // owning side!

romanb's avatar
romanb committed
116
        $this->_em->persist($user);
117
        $this->_em->flush();
118 119 120 121 122 123

        // Check that the foreign key has been set
        $userId = $this->_em->getConnection()->execute("SELECT user_id FROM cms_addresses WHERE id=?",
                array($address->id))->fetchColumn();
        $this->assertTrue(is_numeric($userId));
    }
124 125

    public function testBasicManyToMany()
126
    {        
127 128 129 130 131 132 133 134 135 136 137
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';

        $group = new CmsGroup;
        $group->name = 'Developers';

        $user->groups[] = $group;
        $group->users[] = $user;

romanb's avatar
romanb committed
138
        $this->_em->persist($user);
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

        $this->_em->flush();

        unset($group->users[0]); // inverse side
        unset($user->groups[0]); // owning side!

        $this->_em->flush();

        // Check that the link in the association table has been deleted
        $count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
                array())->fetchColumn();
        $this->assertEquals(0, $count);
    }

    public function testManyToManyCollectionClearing()
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';

        for ($i=0; $i<10; ++$i) {
            $group = new CmsGroup;
            $group->name = 'Developers_' . $i;
            $user->groups[] = $group;
            $group->users[] = $user;
        }

romanb's avatar
romanb committed
167
        $this->_em->persist($user);
168

169
        $this->_em->flush();
romanb's avatar
romanb committed
170
        
171 172 173 174
        // Check that there are indeed 10 links in the association table
        $count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
                array())->fetchColumn();
        $this->assertEquals(10, $count);
175

romanb's avatar
romanb committed
176 177
        $user->groups->clear();
        //unset($user->groups);
178 179 180 181 182 183 184 185

        $this->_em->flush();

        // Check that the links in the association table have been deleted
        $count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_users_groups",
                array())->fetchColumn();
        $this->assertEquals(0, $count);
    }
romanb's avatar
romanb committed
186
    
187
    public function testOneToManyOrphanRemoval()
romanb's avatar
romanb committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';

        for ($i=0; $i<3; ++$i) {
            $phone = new CmsPhonenumber;
            $phone->phonenumber = 100 + $i;
            $user->addPhonenumber($phone);
        }

        $this->_em->persist($user);

        $this->_em->flush();

        $user->getPhonenumbers()->remove(0);
205
        $this->assertEquals(2, count($user->getPhonenumbers()));
romanb's avatar
romanb committed
206 207 208

        $this->_em->flush();

209
        // Check that there are just 2 phonenumbers left
romanb's avatar
romanb committed
210 211 212
        $count = $this->_em->getConnection()->execute("SELECT COUNT(*) FROM cms_phonenumbers",
                array())->fetchColumn();
        $this->assertEquals(2, $count); // only 2 remaining
213
    }
214 215 216 217 218 219 220

    public function testBasicQuery()
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';
romanb's avatar
romanb committed
221
        $this->_em->persist($user);
222 223 224 225
        $this->_em->flush();

        $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");

226
        $users = $query->getResult();
227

228
        $this->assertEquals(1, count($users));
229 230 231
        $this->assertEquals('Guilherme', $users[0]->name);
        $this->assertEquals('gblanco', $users[0]->username);
        $this->assertEquals('developer', $users[0]->status);
232 233
        //$this->assertNull($users[0]->phonenumbers);
        //$this->assertNull($users[0]->articles);
234

235
        $usersArray = $query->getArrayResult();
236 237 238 239 240 241 242 243 244 245 246 247 248 249

        $this->assertTrue(is_array($usersArray));
        $this->assertEquals(1, count($usersArray));
        $this->assertEquals('Guilherme', $usersArray[0]['name']);
        $this->assertEquals('gblanco', $usersArray[0]['username']);
        $this->assertEquals('developer', $usersArray[0]['status']);

        $usersScalar = $query->getScalarResult();

        $this->assertTrue(is_array($usersScalar));
        $this->assertEquals(1, count($usersScalar));
        $this->assertEquals('Guilherme', $usersScalar[0]['u_name']);
        $this->assertEquals('gblanco', $usersScalar[0]['u_username']);
        $this->assertEquals('developer', $usersScalar[0]['u_status']);
250 251
    }

252
    public function testBasicOneToManyInnerJoin()
253 254 255 256 257
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';
romanb's avatar
romanb committed
258
        $this->_em->persist($user);
259 260 261 262
        $this->_em->flush();

        $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p");

263
        $users = $query->getResult();
264

265
        $this->assertEquals(0, count($users));
266 267
    }

268
    public function testBasicOneToManyLeftJoin()
269 270 271 272 273
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';
romanb's avatar
romanb committed
274
        $this->_em->persist($user);
275 276 277 278
        $this->_em->flush();

        $query = $this->_em->createQuery("select u,p from Doctrine\Tests\Models\CMS\CmsUser u left join u.phonenumbers p");

279
        $users = $query->getResult();
280

281
        $this->assertEquals(1, count($users));
282 283 284 285 286
        $this->assertEquals('Guilherme', $users[0]->name);
        $this->assertEquals('gblanco', $users[0]->username);
        $this->assertEquals('developer', $users[0]->status);
        $this->assertTrue($users[0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
        $this->assertEquals(0, $users[0]->phonenumbers->count());
287
        //$this->assertNull($users[0]->articles);
288
    }
289 290 291 292 293 294 295 296 297 298 299 300 301

    public function testBasicManyToManyJoin()
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';

        $group1 = new CmsGroup;
        $group1->setName('Doctrine Developers');

        $user->addGroup($group1);

romanb's avatar
romanb committed
302
        $this->_em->persist($user);
303 304 305 306 307 308 309 310
        
        $this->_em->flush();
        $this->_em->clear();

        $this->assertEquals(0, $this->_em->getUnitOfWork()->size());

        $query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u join u.groups g");

311
        $result = $query->getResult();
312 313 314 315 316 317 318
        
        $this->assertEquals(2, $this->_em->getUnitOfWork()->size());
        $this->assertTrue($result[0] instanceof CmsUser);
        $this->assertEquals('Guilherme', $result[0]->name);
        $this->assertEquals(1, $result[0]->getGroups()->count());
        $groups = $result[0]->getGroups();
        $this->assertEquals('Doctrine Developers', $groups[0]->getName());
319 320 321 322 323 324 325 326 327 328 329 330 331 332

        $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($result[0]));
        $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($groups[0]));

        $this->assertTrue($groups instanceof \Doctrine\ORM\PersistentCollection);
        $this->assertTrue($groups[0]->getUsers() instanceof \Doctrine\ORM\PersistentCollection);

        $groups[0]->getUsers()->clear();
        $groups->clear();

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

        $query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u inner join u.groups g");
333
        $this->assertEquals(0, count($query->getResult()));
334
    }
romanb's avatar
romanb committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    
    public function testBasicRefresh()
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';

        $this->_em->persist($user);
        $this->_em->flush();
        
        $user->status = 'mascot';
        
        $this->assertEquals('mascot', $user->status);
        $this->_em->refresh($user);
        $this->assertEquals('developer', $user->status);
    }
romanb's avatar
romanb committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
    
    public function testAddToCollectionDoesNotInitialize()
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';

        for ($i=0; $i<3; ++$i) {
            $phone = new CmsPhonenumber;
            $phone->phonenumber = 100 + $i;
            $user->addPhonenumber($phone);
        }

        $this->_em->persist($user);
        $this->_em->flush();
        $this->_em->clear();
        
        $this->assertEquals(3, $user->getPhonenumbers()->count());
        
        $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");

        $gblanco = $query->getSingleResult();
        
        $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
        
        $newPhone = new CmsPhonenumber;
        $phone->phonenumber = 555;
        $gblanco->addPhonenumber($phone);
        
        $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
383

romanb's avatar
romanb committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
        $this->_em->flush();
        $this->_em->clear();
        
        $query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
        $gblanco2 = $query->getSingleResult();
        $this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
    }
    
    public function testSetSetAssociationWithGetReference()
    {
        $user = new CmsUser;
        $user->name = 'Guilherme';
        $user->username = 'gblanco';
        $user->status = 'developer';        
        $this->_em->persist($user);
        
        $address = new CmsAddress;
        $address->country = 'Germany';
        $address->city = 'Berlin';
        $address->zip = '12345';
        $this->_em->persist($address);
        
        $this->_em->flush();
        $this->_em->detach($address);
        
        $this->assertFalse($this->_em->contains($address));
        $this->assertTrue($this->_em->contains($user));
        
        // Assume we only got the identifier of the address and now want to attach
        // that address to the user without actually loading it, using getReference().
        $addressRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsAddress', $address->getId());
        
416 417 418 419
        //$addressRef->getId();
        //\Doctrine\Common\Util\Debug::dump($addressRef);
        
        $user->setAddress($addressRef); // Ugh! Initializes address 'cause of $address->setUser($user)!
romanb's avatar
romanb committed
420 421 422 423 424 425 426
        
        $this->_em->flush();
        $this->_em->clear();
        
        // Check with a fresh load that the association is indeed there
        $query = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u join u.address a where u.username='gblanco'");
        $gblanco = $query->getSingleResult();
427

romanb's avatar
romanb committed
428 429 430 431 432
        $this->assertTrue($gblanco instanceof CmsUser);
        $this->assertTrue($gblanco->getAddress() instanceof CmsAddress);
        $this->assertEquals('Berlin', $gblanco->getAddress()->getCity());
        
    }
433
}