Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / FlushEventTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\CMS\CmsUser;
6 use Doctrine\Tests\Models\CMS\CmsPhonenumber;
7 use Doctrine\ORM\Event\OnFlushEventArgs;
8 use Doctrine\ORM\Events;
9
10 require_once __DIR__ . '/../../TestInit.php';
11
12 /**
13  * FlushEventTest
14  *
15  * @author robo
16  */
17 class FlushEventTest extends \Doctrine\Tests\OrmFunctionalTestCase
18 {
19     protected function setUp() {
20         $this->useModelSet('cms');
21         parent::setUp();
22     }
23
24     public function testPersistNewEntitiesOnPreFlush()
25     {
26         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
27         $this->_em->getEventManager()->addEventListener(Events::onFlush, new OnFlushListener);
28
29         $user = new CmsUser;
30         $user->username = 'romanb';
31         $user->name = 'Roman';
32         $user->status = 'Dev';
33
34         $this->_em->persist($user);
35
36         $this->assertEquals(0, $user->phonenumbers->count());
37
38         $this->_em->flush();
39
40         $this->assertEquals(1, $user->phonenumbers->count());
41         $this->assertTrue($this->_em->contains($user->phonenumbers->get(0)));
42         $this->assertTrue($user->phonenumbers->get(0)->getUser() === $user);
43
44         $this->assertFalse($user->phonenumbers->isDirty());
45
46         // Can be used together with SQL Logging to check that a subsequent flush has
47         // nothing to do. This proofs the correctness of the changes that happened in onFlush.
48         //echo "SECOND FLUSH";
49         //$this->_em->flush();
50     }
51 }
52
53 class OnFlushListener
54 {
55     public function onFlush(OnFlushEventArgs $args)
56     {
57         //echo "---preFlush".PHP_EOL;
58
59         $em = $args->getEntityManager();
60         $uow = $em->getUnitOfWork();
61
62         foreach ($uow->getScheduledEntityInsertions() as $entity) {
63
64             if ($entity instanceof CmsUser) {
65                 // Adds a phonenumber to every newly persisted CmsUser ...
66
67                 $phone = new CmsPhonenumber;
68                 $phone->phonenumber = 12345;
69                 // Update object model
70                 $entity->addPhonenumber($phone);
71                 // Invoke regular persist call
72                 $em->persist($phone);
73                 // Explicitly calculate the changeset since onFlush is raised
74                 // after changeset calculation!
75                 $uow->computeChangeSet($em->getClassMetadata(get_class($phone)), $phone);
76
77                 // Take a snapshot because the UoW wont do this for us, because
78                 // the UoW did not visit this collection.
79                 // Alternatively we could provide an ->addVisitedCollection() method
80                 // on the UoW.
81                 $entity->getPhonenumbers()->takeSnapshot();
82             }
83
84             /*foreach ($uow->getEntityChangeSet($entity) as $field => $change) {
85                 list ($old, $new) = $change;
86
87                 var_dump($old);
88             }*/
89
90         }
91     }
92 }
93
94