Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / BasicFunctionalTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\ORM\Tools\SchemaTool;
6 use Doctrine\ORM\Query;
7 use Doctrine\Tests\Models\CMS\CmsUser;
8 use Doctrine\Tests\Models\CMS\CmsPhonenumber;
9 use Doctrine\Tests\Models\CMS\CmsAddress;
10 use Doctrine\Tests\Models\CMS\CmsGroup;
11 use Doctrine\Tests\Models\CMS\CmsArticle;
12 use Doctrine\Tests\Models\CMS\CmsComment;
13
14 require_once __DIR__ . '/../../TestInit.php';
15
16 class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
17 {
18     protected function setUp()
19     {
20         $this->useModelSet('cms');
21         parent::setUp();
22     }
23
24     public function testBasicUnitsOfWorkWithOneToManyAssociation()
25     {
26         // Create
27         $user = new CmsUser;
28         $user->name = 'Roman';
29         $user->username = 'romanb';
30         $user->status = 'developer';
31         $this->_em->persist($user);
32
33         $this->_em->flush();
34
35         $this->assertTrue(is_numeric($user->id));
36         $this->assertTrue($this->_em->contains($user));
37
38         // Read
39         $user2 = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
40         $this->assertTrue($user === $user2);
41
42         // Add a phonenumber
43         $ph = new CmsPhonenumber;
44         $ph->phonenumber = "12345";
45         $user->addPhonenumber($ph);
46         $this->_em->flush();
47         $this->assertTrue($this->_em->contains($ph));
48         $this->assertTrue($this->_em->contains($user));
49         //$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->phonenumbers);
50
51         // Update name
52         $user->name = 'guilherme';
53         $this->_em->flush();
54         $this->assertEquals('guilherme', $user->name);
55
56         // Add another phonenumber
57         $ph2 = new CmsPhonenumber;
58         $ph2->phonenumber = "6789";
59         $user->addPhonenumber($ph2);
60         $this->_em->flush();
61         $this->assertTrue($this->_em->contains($ph2));
62
63         // Delete
64         $this->_em->remove($user);
65         $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($user));
66         $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
67         $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
68         $this->_em->flush();
69         $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($user));
70         $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
71         $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
72         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user));
73         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph));
74         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph2));
75     }
76
77     public function testOneToManyAssociationModification()
78     {
79         $user = new CmsUser;
80         $user->name = 'Roman';
81         $user->username = 'romanb';
82         $user->status = 'developer';
83
84         $ph1 = new CmsPhonenumber;
85         $ph1->phonenumber = "0301234";
86         $ph2 = new CmsPhonenumber;
87         $ph2->phonenumber = "987654321";
88
89         $user->addPhonenumber($ph1);
90         $user->addPhonenumber($ph2);
91
92         $this->_em->persist($user);
93         $this->_em->flush();
94
95         //$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->phonenumbers);
96
97         // Remove the first element from the collection
98         unset($user->phonenumbers[0]);
99         $ph1->user = null; // owning side!
100
101         $this->_em->flush();
102
103         $this->assertEquals(1, count($user->phonenumbers));
104         $this->assertNull($ph1->user);
105     }
106
107     public function testBasicOneToOne()
108     {
109         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
110         $user = new CmsUser;
111         $user->name = 'Roman';
112         $user->username = 'romanb';
113         $user->status = 'developer';
114
115         $address = new CmsAddress;
116         $address->country = 'Germany';
117         $address->city = 'Berlin';
118         $address->zip = '12345';
119
120         $user->address = $address; // inverse side
121         $address->user = $user; // owning side!
122
123         $this->_em->persist($user);
124         $this->_em->flush();
125
126         // Check that the foreign key has been set
127         $userId = $this->_em->getConnection()->executeQuery(
128             "SELECT user_id FROM cms_addresses WHERE id=?", array($address->id)
129         )->fetchColumn();
130         $this->assertTrue(is_numeric($userId));
131
132         $this->_em->clear();
133
134         $user2 = $this->_em->createQuery('select u from \Doctrine\Tests\Models\CMS\CmsUser u where u.id=?1')
135                 ->setParameter(1, $userId)
136                 ->getSingleResult();
137
138         // Address has been eager-loaded because it cant be lazy
139         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $user2->address);
140         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $user2->address);
141     }
142
143     /**
144      * @group DDC-1230
145      */
146     public function testRemove()
147     {
148         $user = new CmsUser;
149         $user->name = 'Guilherme';
150         $user->username = 'gblanco';
151         $user->status = 'developer';
152
153         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
154
155         $this->_em->persist($user);
156
157         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_MANAGED");
158
159         $this->_em->remove($user);
160
161         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
162
163         $this->_em->persist($user);
164         $this->_em->flush();
165         $id = $user->getId();
166
167         $this->_em->remove($user);
168
169         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_REMOVED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_REMOVED");
170         $this->_em->flush();
171
172         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
173
174         $this->assertNull($this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $id));
175     }
176
177     public function testOneToManyOrphanRemoval()
178     {
179         $user = new CmsUser;
180         $user->name = 'Guilherme';
181         $user->username = 'gblanco';
182         $user->status = 'developer';
183
184         for ($i=0; $i<3; ++$i) {
185             $phone = new CmsPhonenumber;
186             $phone->phonenumber = 100 + $i;
187             $user->addPhonenumber($phone);
188         }
189
190         $this->_em->persist($user);
191
192         $this->_em->flush();
193
194         $user->getPhonenumbers()->remove(0);
195         $this->assertEquals(2, count($user->getPhonenumbers()));
196
197         $this->_em->flush();
198
199         // Check that there are just 2 phonenumbers left
200         $count = $this->_em->getConnection()->fetchColumn("SELECT COUNT(*) FROM cms_phonenumbers");
201         $this->assertEquals(2, $count); // only 2 remaining
202
203         // check that clear() removes the others via orphan removal
204         $user->getPhonenumbers()->clear();
205         $this->_em->flush();
206         $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_phonenumbers"));
207     }
208
209     public function testBasicQuery()
210     {
211         $user = new CmsUser;
212         $user->name = 'Guilherme';
213         $user->username = 'gblanco';
214         $user->status = 'developer';
215         $this->_em->persist($user);
216         $this->_em->flush();
217
218         $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
219
220         $users = $query->getResult();
221
222         $this->assertEquals(1, count($users));
223         $this->assertEquals('Guilherme', $users[0]->name);
224         $this->assertEquals('gblanco', $users[0]->username);
225         $this->assertEquals('developer', $users[0]->status);
226         //$this->assertNull($users[0]->phonenumbers);
227         //$this->assertNull($users[0]->articles);
228
229         $usersArray = $query->getArrayResult();
230
231         $this->assertTrue(is_array($usersArray));
232         $this->assertEquals(1, count($usersArray));
233         $this->assertEquals('Guilherme', $usersArray[0]['name']);
234         $this->assertEquals('gblanco', $usersArray[0]['username']);
235         $this->assertEquals('developer', $usersArray[0]['status']);
236
237         $usersScalar = $query->getScalarResult();
238
239         $this->assertTrue(is_array($usersScalar));
240         $this->assertEquals(1, count($usersScalar));
241         $this->assertEquals('Guilherme', $usersScalar[0]['u_name']);
242         $this->assertEquals('gblanco', $usersScalar[0]['u_username']);
243         $this->assertEquals('developer', $usersScalar[0]['u_status']);
244     }
245
246     public function testBasicOneToManyInnerJoin()
247     {
248         $user = new CmsUser;
249         $user->name = 'Guilherme';
250         $user->username = 'gblanco';
251         $user->status = 'developer';
252         $this->_em->persist($user);
253         $this->_em->flush();
254
255         $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p");
256
257         $users = $query->getResult();
258
259         $this->assertEquals(0, count($users));
260     }
261
262     public function testBasicOneToManyLeftJoin()
263     {
264         $user = new CmsUser;
265         $user->name = 'Guilherme';
266         $user->username = 'gblanco';
267         $user->status = 'developer';
268         $this->_em->persist($user);
269         $this->_em->flush();
270
271         $query = $this->_em->createQuery("select u,p from Doctrine\Tests\Models\CMS\CmsUser u left join u.phonenumbers p");
272
273         $users = $query->getResult();
274
275         $this->assertEquals(1, count($users));
276         $this->assertEquals('Guilherme', $users[0]->name);
277         $this->assertEquals('gblanco', $users[0]->username);
278         $this->assertEquals('developer', $users[0]->status);
279         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $users[0]->phonenumbers);
280         $this->assertTrue($users[0]->phonenumbers->isInitialized());
281         $this->assertEquals(0, $users[0]->phonenumbers->count());
282         //$this->assertNull($users[0]->articles);
283     }
284
285     public function testBasicRefresh()
286     {
287         $user = new CmsUser;
288         $user->name = 'Guilherme';
289         $user->username = 'gblanco';
290         $user->status = 'developer';
291
292         $this->_em->persist($user);
293         $this->_em->flush();
294
295         $user->status = 'mascot';
296
297         $this->assertEquals('mascot', $user->status);
298         $this->_em->refresh($user);
299         $this->assertEquals('developer', $user->status);
300     }
301
302     /**
303      * @group DDC-833
304      */
305     public function testRefreshResetsCollection()
306     {
307         $user = new CmsUser;
308         $user->name = 'Guilherme';
309         $user->username = 'gblanco';
310         $user->status = 'developer';
311
312         // Add a phonenumber
313         $ph1 = new CmsPhonenumber;
314         $ph1->phonenumber = "12345";
315         $user->addPhonenumber($ph1);
316
317         // Add a phonenumber
318         $ph2 = new CmsPhonenumber;
319         $ph2->phonenumber = "54321";
320
321         $this->_em->persist($user);
322         $this->_em->persist($ph1);
323         $this->_em->persist($ph2);
324         $this->_em->flush();
325
326         $user->addPhonenumber($ph2);
327
328         $this->assertEquals(2, count($user->phonenumbers));
329         $this->_em->refresh($user);
330
331         $this->assertEquals(1, count($user->phonenumbers));
332     }
333
334     /**
335      * @group DDC-833
336      */
337     public function testDqlRefreshResetsCollection()
338     {
339         $user = new CmsUser;
340         $user->name = 'Guilherme';
341         $user->username = 'gblanco';
342         $user->status = 'developer';
343
344         // Add a phonenumber
345         $ph1 = new CmsPhonenumber;
346         $ph1->phonenumber = "12345";
347         $user->addPhonenumber($ph1);
348
349         // Add a phonenumber
350         $ph2 = new CmsPhonenumber;
351         $ph2->phonenumber = "54321";
352
353         $this->_em->persist($user);
354         $this->_em->persist($ph1);
355         $this->_em->persist($ph2);
356         $this->_em->flush();
357
358         $user->addPhonenumber($ph2);
359
360         $this->assertEquals(2, count($user->phonenumbers));
361         $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
362         $user = $this->_em->createQuery($dql)
363                           ->setParameter(1, $user->id)
364                           ->setHint(Query::HINT_REFRESH, true)
365                           ->getSingleResult();
366
367         $this->assertEquals(1, count($user->phonenumbers));
368     }
369
370     /**
371      * @group DDC-833
372      */
373     public function testCreateEntityOfProxy()
374     {
375         $user = new CmsUser;
376         $user->name = 'Guilherme';
377         $user->username = 'gblanco';
378         $user->status = 'developer';
379
380         // Add a phonenumber
381         $ph1 = new CmsPhonenumber;
382         $ph1->phonenumber = "12345";
383         $user->addPhonenumber($ph1);
384
385         // Add a phonenumber
386         $ph2 = new CmsPhonenumber;
387         $ph2->phonenumber = "54321";
388
389         $this->_em->persist($user);
390         $this->_em->persist($ph1);
391         $this->_em->persist($ph2);
392         $this->_em->flush();
393         $this->_em->clear();
394
395         $userId = $user->id;
396         $user = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
397
398         $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
399         $user = $this->_em->createQuery($dql)
400                           ->setParameter(1, $userId)
401                           ->getSingleResult();
402
403         $this->assertEquals(1, count($user->phonenumbers));
404     }
405
406     public function testAddToCollectionDoesNotInitialize()
407     {
408         $user = new CmsUser;
409         $user->name = 'Guilherme';
410         $user->username = 'gblanco';
411         $user->status = 'developer';
412
413         for ($i=0; $i<3; ++$i) {
414             $phone = new CmsPhonenumber;
415             $phone->phonenumber = 100 + $i;
416             $user->addPhonenumber($phone);
417         }
418
419         $this->_em->persist($user);
420         $this->_em->flush();
421         $this->_em->clear();
422
423         $this->assertEquals(3, $user->getPhonenumbers()->count());
424
425         $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
426
427         $gblanco = $query->getSingleResult();
428
429         $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
430
431         $newPhone = new CmsPhonenumber;
432         $newPhone->phonenumber = 555;
433         $gblanco->addPhonenumber($newPhone);
434
435         $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
436         $this->_em->persist($gblanco);
437
438         $this->_em->flush();
439         $this->_em->clear();
440
441         $query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
442         $gblanco2 = $query->getSingleResult();
443         $this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
444     }
445
446     public function testInitializeCollectionWithNewObjectsRetainsNewObjects()
447     {
448         $user = new CmsUser;
449         $user->name = 'Guilherme';
450         $user->username = 'gblanco';
451         $user->status = 'developer';
452
453         for ($i=0; $i<3; ++$i) {
454             $phone = new CmsPhonenumber;
455             $phone->phonenumber = 100 + $i;
456             $user->addPhonenumber($phone);
457         }
458
459         $this->_em->persist($user);
460         $this->_em->flush();
461         $this->_em->clear();
462
463         $this->assertEquals(3, $user->getPhonenumbers()->count());
464
465         $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
466
467         $gblanco = $query->getSingleResult();
468
469         $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
470
471         $newPhone = new CmsPhonenumber;
472         $newPhone->phonenumber = 555;
473         $gblanco->addPhonenumber($newPhone);
474
475         $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
476         $this->assertEquals(4, $gblanco->getPhonenumbers()->count());
477         $this->assertTrue($gblanco->getPhonenumbers()->isInitialized());
478
479         $this->_em->flush();
480         $this->_em->clear();
481
482         $query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
483         $gblanco2 = $query->getSingleResult();
484         $this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
485     }
486
487     public function testSetSetAssociationWithGetReference()
488     {
489         $user = new CmsUser;
490         $user->name = 'Guilherme';
491         $user->username = 'gblanco';
492         $user->status = 'developer';
493         $this->_em->persist($user);
494
495         $address = new CmsAddress;
496         $address->country = 'Germany';
497         $address->city = 'Berlin';
498         $address->zip = '12345';
499         $this->_em->persist($address);
500
501         $this->_em->flush();
502         $this->_em->detach($address);
503
504         $this->assertFalse($this->_em->contains($address));
505         $this->assertTrue($this->_em->contains($user));
506
507         // Assume we only got the identifier of the address and now want to attach
508         // that address to the user without actually loading it, using getReference().
509         $addressRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsAddress', $address->getId());
510
511         //$addressRef->getId();
512         //\Doctrine\Common\Util\Debug::dump($addressRef);
513
514         $user->setAddress($addressRef); // Ugh! Initializes address 'cause of $address->setUser($user)!
515
516         $this->_em->flush();
517         $this->_em->clear();
518
519         // Check with a fresh load that the association is indeed there
520         $query = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u join u.address a where u.username='gblanco'");
521         $gblanco = $query->getSingleResult();
522
523         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $gblanco);
524         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $gblanco->getAddress());
525         $this->assertEquals('Berlin', $gblanco->getAddress()->getCity());
526
527     }
528
529     public function testOneToManyCascadeRemove()
530     {
531         $user = new CmsUser;
532         $user->name = 'Guilherme';
533         $user->username = 'gblanco';
534         $user->status = 'developer';
535
536         for ($i=0; $i<3; ++$i) {
537             $phone = new CmsPhonenumber;
538             $phone->phonenumber = 100 + $i;
539             $user->addPhonenumber($phone);
540         }
541
542         $this->_em->persist($user);
543         $this->_em->flush();
544         $this->_em->clear();
545
546         $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
547         $gblanco = $query->getSingleResult();
548
549         $this->_em->remove($gblanco);
550         $this->_em->flush();
551
552         $this->_em->clear();
553
554         $this->assertEquals(0, $this->_em->createQuery(
555                 "select count(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p")
556                 ->getSingleScalarResult());
557
558         $this->assertEquals(0, $this->_em->createQuery(
559                 "select count(u.id) from Doctrine\Tests\Models\CMS\CmsUser u")
560                 ->getSingleScalarResult());
561     }
562
563     public function testTextColumnSaveAndRetrieve()
564     {
565         $user = new CmsUser;
566         $user->name = 'Guilherme';
567         $user->username = 'gblanco';
568         $user->status = 'developer';
569
570         $this->_em->persist($user);
571
572         $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
573         $article->text = "Lorem ipsum dolor sunt.";
574         $article->topic = "A Test Article!";
575         $article->setAuthor($user);
576
577         $this->_em->persist($article);
578         $this->_em->flush();
579         $articleId = $article->id;
580
581         $this->_em->clear();
582
583         // test find() with leading backslash at the same time
584         $articleNew = $this->_em->find('\Doctrine\Tests\Models\CMS\CmsArticle', $articleId);
585         $this->assertTrue($this->_em->contains($articleNew));
586         $this->assertEquals("Lorem ipsum dolor sunt.", $articleNew->text);
587
588         $this->assertNotSame($article, $articleNew);
589
590         $articleNew->text = "Lorem ipsum dolor sunt. And stuff!";
591
592         $this->_em->flush();
593         $this->_em->clear();
594
595         $articleNew = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $articleId);
596         $this->assertEquals("Lorem ipsum dolor sunt. And stuff!", $articleNew->text);
597         $this->assertTrue($this->_em->contains($articleNew));
598     }
599
600     public function testFlushDoesNotIssueUnnecessaryUpdates()
601     {
602         $user = new CmsUser;
603         $user->name = 'Guilherme';
604         $user->username = 'gblanco';
605         $user->status = 'developer';
606
607         $address = new CmsAddress;
608         $address->country = 'Germany';
609         $address->city = 'Berlin';
610         $address->zip = '12345';
611
612         $address->user = $user;
613         $user->address = $address;
614
615         $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
616         $article->text = "Lorem ipsum dolor sunt.";
617         $article->topic = "A Test Article!";
618         $article->setAuthor($user);
619
620         $this->_em->persist($article);
621         $this->_em->persist($user);
622
623         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
624
625         $this->_em->flush();
626         $this->_em->clear();
627
628         $query = $this->_em->createQuery('select u,a,ad from Doctrine\Tests\Models\CMS\CmsUser u join u.articles a join u.address ad');
629         $user2 = $query->getSingleResult();
630
631         $this->assertEquals(1, count($user2->articles));
632         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $user2->address);
633
634         $oldLogger = $this->_em->getConnection()->getConfiguration()->getSQLLogger();
635         $debugStack = new \Doctrine\DBAL\Logging\DebugStack;
636         $this->_em->getConnection()->getConfiguration()->setSQLLogger($debugStack);
637
638         $this->_em->flush();
639         $this->assertEquals(0, count($debugStack->queries));
640
641         $this->_em->getConnection()->getConfiguration()->setSQLLogger($oldLogger);
642     }
643
644     public function testRemoveEntityByReference()
645     {
646         $user = new CmsUser;
647         $user->name = 'Guilherme';
648         $user->username = 'gblanco';
649         $user->status = 'developer';
650
651         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
652
653         $this->_em->persist($user);
654         $this->_em->flush();
655         $this->_em->clear();
656
657         $userRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
658         $this->_em->remove($userRef);
659         $this->_em->flush();
660         $this->_em->clear();
661
662         $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users"));
663
664         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
665     }
666
667     public function testQueryEntityByReference()
668     {
669         $user = new CmsUser;
670         $user->name = 'Guilherme';
671         $user->username = 'gblanco';
672         $user->status = 'developer';
673
674         $address = new CmsAddress;
675         $address->country = 'Germany';
676         $address->city = 'Berlin';
677         $address->zip = '12345';
678
679         $user->setAddress($address);
680
681         $this->_em->transactional(function($em) use($user) {
682             $em->persist($user);
683         });
684         $this->_em->clear();
685
686         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
687
688         $userRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
689         $address2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsAddress a where a.user = :user')
690                 ->setParameter('user', $userRef)
691                 ->getSingleResult();
692
693         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $address2->getUser());
694         $this->assertTrue($userRef === $address2->getUser());
695         $this->assertFalse($userRef->__isInitialized__);
696         $this->assertEquals('Germany', $address2->country);
697         $this->assertEquals('Berlin', $address2->city);
698         $this->assertEquals('12345', $address2->zip);
699     }
700
701     public function testOneToOneNullUpdate()
702     {
703         $user = new CmsUser();
704         $user->username = "beberlei";
705         $user->name = "Benjamin E.";
706         $user->status = 'active';
707
708         $address = new CmsAddress();
709         $address->city = "Bonn";
710         $address->zip = "12354";
711         $address->country = "Germany";
712         $address->street = "somestreet";
713         $address->user = $user;
714
715         $this->_em->persist($address);
716         $this->_em->persist($user);
717         $this->_em->flush();
718
719         $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
720
721         $address->user = null;
722         $this->_em->flush();
723
724         $this->assertNotEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
725     }
726
727     /**
728      * @group DDC-600
729      * @group DDC-455
730      */
731     public function testNewAssociatedEntityDuringFlushThrowsException()
732     {
733         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
734         $user = new CmsUser();
735         $user->username = "beberlei";
736         $user->name = "Benjamin E.";
737         $user->status = 'active';
738
739         $address = new CmsAddress();
740         $address->city = "Bonn";
741         $address->zip = "12354";
742         $address->country = "Germany";
743         $address->street = "somestreet";
744         $address->user = $user;
745
746         $this->_em->persist($address);
747         // pretend we forgot to persist $user
748         try {
749             $this->_em->flush(); // should raise an exception
750             $this->fail();
751         } catch (\InvalidArgumentException $expected) {}
752     }
753
754     /**
755      * @group DDC-600
756      * @group DDC-455
757      */
758     public function testNewAssociatedEntityDuringFlushThrowsException2()
759     {
760         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
761         $user = new CmsUser();
762         $user->username = "beberlei";
763         $user->name = "Benjamin E.";
764         $user->status = 'active';
765
766         $address = new CmsAddress();
767         $address->city = "Bonn";
768         $address->zip = "12354";
769         $address->country = "Germany";
770         $address->street = "somestreet";
771         $address->user = $user;
772
773         $this->_em->persist($address);
774         $this->_em->persist($user);
775         $this->_em->flush();
776
777         $u2 = new CmsUser;
778         $u2->username = "beberlei";
779         $u2->name = "Benjamin E.";
780         $u2->status = 'inactive';
781         $address->user = $u2;
782         // pretend we forgot to persist $u2
783         try {
784             $this->_em->flush(); // should raise an exception
785             $this->fail();
786         } catch (\InvalidArgumentException $expected) {}
787     }
788
789     /**
790      * @group DDC-600
791      * @group DDC-455
792      */
793     public function testNewAssociatedEntityDuringFlushThrowsException3()
794     {
795         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
796         $art = new CmsArticle();
797         $art->topic = 'topic';
798         $art->text = 'the text';
799
800         $com = new CmsComment();
801         $com->topic = 'Good';
802         $com->text = 'Really good!';
803         $art->addComment($com);
804
805         $this->_em->persist($art);
806         // pretend we forgot to persist $com
807         try {
808             $this->_em->flush(); // should raise an exception
809             $this->fail();
810         } catch (\InvalidArgumentException $expected) {}
811     }
812
813     public function testOneToOneOrphanRemoval()
814     {
815         $user = new CmsUser();
816         $user->username = "beberlei";
817         $user->name = "Benjamin E.";
818         $user->status = 'active';
819
820         $address = new CmsAddress();
821         $address->city = "Bonn";
822         $address->zip = "12354";
823         $address->country = "Germany";
824         $address->street = "somestreet";
825         $address->user = $user;
826         $user->address = $address;
827
828         $this->_em->persist($address);
829         $this->_em->persist($user);
830         $this->_em->flush();
831         $addressId = $address->getId();
832
833         $user->address = null;
834
835         $this->_em->flush();
836
837         $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
838
839         // check orphan removal through replacement
840         $user->address = $address;
841         $address->user = $user;
842
843         $this->_em->flush();
844         $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
845
846         // remove $address to free up unique key id
847         $this->_em->remove($address);
848         $this->_em->flush();
849
850         $newAddress = new CmsAddress();
851         $newAddress->city = "NewBonn";
852         $newAddress->zip = "12354";
853         $newAddress->country = "NewGermany";
854         $newAddress->street = "somenewstreet";
855         $newAddress->user = $user;
856         $user->address = $newAddress;
857
858         $this->_em->flush();
859         $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
860     }
861
862     public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt()
863     {
864         $user = new CmsUser();
865         $user->username = "beberlei";
866         $user->name = "Benjamin E.";
867         $user->status = 'active';
868         $this->_em->persist($user);
869         $this->_em->flush();
870         $userId = $user->id;
871         $this->_em->clear();
872
873         $user = $this->_em->getPartialReference('Doctrine\Tests\Models\CMS\CmsUser', $userId);
874         $this->assertTrue($this->_em->contains($user));
875         $this->assertNull($user->getName());
876         $this->assertEquals($userId, $user->id);
877
878         $user->name = 'Stephan';
879         $this->_em->flush();
880         $this->_em->clear();
881
882         $this->assertEquals('Benjamin E.', $this->_em->find(get_class($user), $userId)->name);
883     }
884
885     public function testMergePersistsNewEntities()
886     {
887         $user = new CmsUser();
888         $user->username = "beberlei";
889         $user->name = "Benjamin E.";
890         $user->status = 'active';
891
892         $managedUser = $this->_em->merge($user);
893         $this->assertEquals('beberlei', $managedUser->username);
894         $this->assertEquals('Benjamin E.', $managedUser->name);
895         $this->assertEquals('active', $managedUser->status);
896
897         $this->assertTrue($user !== $managedUser);
898         $this->assertTrue($this->_em->contains($managedUser));
899
900         $this->_em->flush();
901         $userId = $managedUser->id;
902         $this->_em->clear();
903
904         $user2 = $this->_em->find(get_class($managedUser), $userId);
905         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user2);
906     }
907
908     public function testMergeNonPersistedProperties()
909     {
910         $user = new CmsUser();
911         $user->username = "beberlei";
912         $user->name = "Benjamin E.";
913         $user->status = 'active';
914         $user->nonPersistedProperty = 'test';
915         $user->nonPersistedPropertyObject = new CmsPhonenumber();
916
917         $managedUser = $this->_em->merge($user);
918         $this->assertEquals('test', $managedUser->nonPersistedProperty);
919         $this->assertSame($user->nonPersistedProperty, $managedUser->nonPersistedProperty);
920         $this->assertSame($user->nonPersistedPropertyObject, $managedUser->nonPersistedPropertyObject);
921
922         $this->assertTrue($user !== $managedUser);
923         $this->assertTrue($this->_em->contains($managedUser));
924
925         $this->_em->flush();
926         $userId = $managedUser->id;
927         $this->_em->clear();
928
929         $user2 = $this->_em->find(get_class($managedUser), $userId);
930         $this->assertNull($user2->nonPersistedProperty);
931         $this->assertNull($user2->nonPersistedPropertyObject);
932         $this->assertEquals('active', $user2->status);
933     }
934
935     public function testMergeThrowsExceptionIfEntityWithGeneratedIdentifierDoesNotExist()
936     {
937         $user = new CmsUser();
938         $user->username = "beberlei";
939         $user->name = "Benjamin E.";
940         $user->status = 'active';
941         $user->id = 42;
942         try {
943             $this->_em->merge($user);
944             $this->fail();
945         } catch (\Doctrine\ORM\EntityNotFoundException $enfe) {}
946     }
947
948     /**
949      * @group DDC-634
950      */
951     public function testOneToOneMergeSetNull()
952     {
953         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
954         $user = new CmsUser();
955         $user->username = "beberlei";
956         $user->name = "Benjamin E.";
957         $user->status = 'active';
958
959         $ph = new CmsPhonenumber();
960         $ph->phonenumber = "12345";
961         $user->addPhonenumber($ph);
962
963         $this->_em->persist($user);
964         $this->_em->persist($ph);
965         $this->_em->flush();
966
967         $this->_em->clear();
968
969         $ph->user = null;
970         $managedPh = $this->_em->merge($ph);
971
972         $this->_em->flush();
973         $this->_em->clear();
974
975         $this->assertNull($this->_em->find(get_class($ph), $ph->phonenumber)->getUser());
976     }
977
978     /**
979      * @group DDC-952
980      */
981     public function testManyToOneFetchModeQuery()
982     {
983         $user = new CmsUser();
984         $user->username = "beberlei";
985         $user->name = "Benjamin E.";
986         $user->status = 'active';
987
988         $article = new CmsArticle();
989         $article->topic = "foo";
990         $article->text = "bar";
991         $article->user = $user;
992
993         $this->_em->persist($article);
994         $this->_em->persist($user);
995         $this->_em->flush();
996         $this->_em->clear();
997
998         $qc = $this->getCurrentQueryCount();
999         $dql = "SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.id = ?1";
1000         $article = $this->_em->createQuery($dql)
1001                              ->setParameter(1, $article->id)
1002                              ->setFetchMode('Doctrine\Tests\Models\CMS\CmsArticle', 'user', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
1003                              ->getSingleResult();
1004         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $article->user, "It IS a proxy, ...");
1005         $this->assertTrue($article->user->__isInitialized__, "...but its initialized!");
1006         $this->assertEquals($qc+2, $this->getCurrentQueryCount());
1007     }
1008
1009     /**
1010      * @group DDC-1278
1011      */
1012     public function testClearWithEntityName()
1013     {
1014         $user = new CmsUser;
1015         $user->name = 'Dominik';
1016         $user->username = 'domnikl';
1017         $user->status = 'developer';
1018
1019         $address = new CmsAddress();
1020         $address->city = "Springfield";
1021         $address->zip = "12354";
1022         $address->country = "Germany";
1023         $address->street = "Foo Street";
1024         $address->user = $user;
1025         $user->address = $address;
1026
1027         $article1 = new CmsArticle();
1028         $article1->topic = 'Foo';
1029         $article1->text = 'Foo Text';
1030
1031         $article2 = new CmsArticle();
1032         $article2->topic = 'Bar';
1033         $article2->text = 'Bar Text';
1034
1035         $user->addArticle($article1);
1036         $user->addArticle($article2);
1037
1038         $this->_em->persist($article1);
1039         $this->_em->persist($article2);
1040         $this->_em->persist($address);
1041         $this->_em->persist($user);
1042         $this->_em->flush();
1043
1044         $unitOfWork = $this->_em->getUnitOfWork();
1045
1046         $this->_em->clear('Doctrine\Tests\Models\CMS\CmsUser');
1047
1048         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($user));
1049
1050         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($address));
1051         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($article1));
1052         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($article2));
1053
1054         $this->_em->clear();
1055
1056         $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($address));
1057     }
1058
1059     public function testFlushManyExplicitEntities()
1060     {
1061         $userA = new CmsUser;
1062         $userA->username = 'UserA';
1063         $userA->name = 'UserA';
1064
1065         $userB = new CmsUser;
1066         $userB->username = 'UserB';
1067         $userB->name = 'UserB';
1068
1069         $userC = new CmsUser;
1070         $userC->username = 'UserC';
1071         $userC->name = 'UserC';
1072
1073         $this->_em->persist($userA);
1074         $this->_em->persist($userB);
1075         $this->_em->persist($userC);
1076
1077         $this->_em->flush(array($userA, $userB, $userB));
1078
1079         $userC->name = 'changed name';
1080
1081         $this->_em->flush(array($userA, $userB));
1082         $this->_em->refresh($userC);
1083
1084         $this->assertTrue($userA->id > 0, 'user a has an id');
1085         $this->assertTrue($userB->id > 0, 'user b has an id');
1086         $this->assertTrue($userC->id > 0, 'user c has an id');
1087         $this->assertEquals('UserC', $userC->name, 'name has not changed because we did not flush it');
1088     }
1089
1090     /**
1091      * @group DDC-720
1092      */
1093     public function testFlushSingleManagedEntity()
1094     {
1095         $user = new CmsUser;
1096         $user->name = 'Dominik';
1097         $user->username = 'domnikl';
1098         $user->status = 'developer';
1099
1100         $this->_em->persist($user);
1101         $this->_em->flush();
1102
1103         $user->status = 'administrator';
1104         $this->_em->flush($user);
1105         $this->_em->clear();
1106
1107         $user = $this->_em->find(get_class($user), $user->id);
1108         $this->assertEquals('administrator', $user->status);
1109     }
1110
1111     /**
1112      * @group DDC-720
1113      */
1114     public function testFlushSingleUnmanagedEntity()
1115     {
1116         $user = new CmsUser;
1117         $user->name = 'Dominik';
1118         $user->username = 'domnikl';
1119         $user->status = 'developer';
1120
1121         $this->setExpectedException('InvalidArgumentException', 'Entity has to be managed for single computation');
1122         $this->_em->flush($user);
1123     }
1124
1125     /**
1126      * @group DDC-720
1127      */
1128     public function testFlushSingleAndNewEntity()
1129     {
1130         $user = new CmsUser;
1131         $user->name = 'Dominik';
1132         $user->username = 'domnikl';
1133         $user->status = 'developer';
1134
1135         $this->_em->persist($user);
1136         $this->_em->flush();
1137
1138         $otherUser = new CmsUser;
1139         $otherUser->name = 'Dominik2';
1140         $otherUser->username = 'domnikl2';
1141         $otherUser->status = 'developer';
1142
1143         $user->status = 'administrator';
1144
1145         $this->_em->persist($otherUser);
1146         $this->_em->flush($user);
1147
1148         $this->assertTrue($this->_em->contains($otherUser), "Other user is contained in EntityManager");
1149         $this->assertTrue($otherUser->id > 0, "other user has an id");
1150     }
1151
1152     /**
1153      * @group DDC-720
1154      */
1155     public function testFlushAndCascadePersist()
1156     {
1157         $user = new CmsUser;
1158         $user->name = 'Dominik';
1159         $user->username = 'domnikl';
1160         $user->status = 'developer';
1161
1162         $this->_em->persist($user);
1163         $this->_em->flush();
1164
1165         $address = new CmsAddress();
1166         $address->city = "Springfield";
1167         $address->zip = "12354";
1168         $address->country = "Germany";
1169         $address->street = "Foo Street";
1170         $address->user = $user;
1171         $user->address = $address;
1172
1173         $this->_em->flush($user);
1174
1175         $this->assertTrue($this->_em->contains($address), "Other user is contained in EntityManager");
1176         $this->assertTrue($address->id > 0, "other user has an id");
1177     }
1178
1179     /**
1180      * @group DDC-720
1181      */
1182     public function testFlushSingleAndNoCascade()
1183     {
1184         $user = new CmsUser;
1185         $user->name = 'Dominik';
1186         $user->username = 'domnikl';
1187         $user->status = 'developer';
1188
1189         $this->_em->persist($user);
1190         $this->_em->flush();
1191
1192         $article1 = new CmsArticle();
1193         $article1->topic = 'Foo';
1194         $article1->text = 'Foo Text';
1195         $article1->author = $user;
1196         $user->articles[] = $article1;
1197
1198         $this->setExpectedException('InvalidArgumentException', "A new entity was found through the relationship 'Doctrine\Tests\Models\CMS\CmsUser#articles'");
1199         $this->_em->flush($user);
1200     }
1201
1202     /**
1203      * @group DDC-720
1204      * @group DDC-1612
1205      */
1206     public function testFlushSingleNewEntity()
1207     {
1208         $user = new CmsUser;
1209         $user->name = 'Dominik';
1210         $user->username = 'domnikl';
1211         $user->status = 'developer';
1212
1213         $this->_em->persist($user);
1214         $this->_em->flush($user);
1215     }
1216
1217     /**
1218      * @group DDC-720
1219      */
1220     public function testProxyIsIgnored()
1221     {
1222         $user = new CmsUser;
1223         $user->name = 'Dominik';
1224         $user->username = 'domnikl';
1225         $user->status = 'developer';
1226
1227         $this->_em->persist($user);
1228         $this->_em->flush();
1229         $this->_em->clear();
1230
1231         $user = $this->_em->getReference(get_class($user), $user->id);
1232
1233         $otherUser = new CmsUser;
1234         $otherUser->name = 'Dominik2';
1235         $otherUser->username = 'domnikl2';
1236         $otherUser->status = 'developer';
1237
1238         $this->_em->persist($otherUser);
1239         $this->_em->flush($user);
1240
1241         $this->assertTrue($this->_em->contains($otherUser), "Other user is contained in EntityManager");
1242         $this->assertTrue($otherUser->id > 0, "other user has an id");
1243     }
1244
1245     /**
1246      * @group DDC-720
1247      */
1248     public function testFlushSingleSaveOnlySingle()
1249     {
1250         $user = new CmsUser;
1251         $user->name = 'Dominik';
1252         $user->username = 'domnikl';
1253         $user->status = 'developer';
1254         $this->_em->persist($user);
1255
1256         $user2 = new CmsUser;
1257         $user2->name = 'Dominik';
1258         $user2->username = 'domnikl2';
1259         $user2->status = 'developer';
1260         $this->_em->persist($user2);
1261
1262         $this->_em->flush();
1263
1264         $user->status = 'admin';
1265         $user2->status = 'admin';
1266
1267         $this->_em->flush($user);
1268         $this->_em->clear();
1269
1270         $user2 = $this->_em->find(get_class($user2), $user2->id);
1271         $this->assertEquals('developer', $user2->status);
1272     }
1273
1274     /**
1275      * @group DDC-1585
1276      */
1277     public function testWrongAssocationInstance()
1278     {
1279         $user = new CmsUser;
1280         $user->name = 'Dominik';
1281         $user->username = 'domnikl';
1282         $user->status = 'developer';
1283         $user->address = $user;
1284
1285         $this->_em->persist($user);
1286
1287         $this->setExpectedException("Doctrine\ORM\ORMException", "Found entity of type Doctrine\Tests\Models\CMS\CmsUser on association Doctrine\Tests\Models\CMS\CmsUser#address, but expecting Doctrine\Tests\Models\CMS\CmsAddress");
1288         $this->_em->flush();
1289     }
1290 }