Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / DetachedEntityTest.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\Tests\Models\CMS\CmsAddress;
8 use Doctrine\Tests\Models\CMS\CmsArticle;
9 use Doctrine\ORM\UnitOfWork;
10
11 require_once __DIR__ . '/../../TestInit.php';
12
13 /**
14  * Description of DetachedEntityTest
15  *
16  * @author robo
17  */
18 class DetachedEntityTest extends \Doctrine\Tests\OrmFunctionalTestCase
19 {
20     protected function setUp() {
21         $this->useModelSet('cms');
22         parent::setUp();
23     }
24
25     public function testSimpleDetachMerge() {
26         $user = new CmsUser;
27         $user->name = 'Roman';
28         $user->username = 'romanb';
29         $user->status = 'dev';
30         $this->_em->persist($user);
31         $this->_em->flush();
32         $this->_em->clear();
33
34         // $user is now detached
35
36         $this->assertFalse($this->_em->contains($user));
37
38         $user->name = 'Roman B.';
39
40         //$this->assertEquals(UnitOfWork::STATE_DETACHED, $this->_em->getUnitOfWork()->getEntityState($user));
41
42         $user2 = $this->_em->merge($user);
43
44         $this->assertFalse($user === $user2);
45         $this->assertTrue($this->_em->contains($user2));
46         $this->assertEquals('Roman B.', $user2->name);
47     }
48
49     public function testSerializeUnserializeModifyMerge()
50     {
51         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
52         $user = new CmsUser;
53         $user->name = 'Guilherme';
54         $user->username = 'gblanco';
55         $user->status = 'developer';
56
57         $ph1 = new CmsPhonenumber;
58         $ph1->phonenumber = "1234";
59         $user->addPhonenumber($ph1);
60
61         $this->_em->persist($user);
62         $this->_em->flush();
63         $this->assertTrue($this->_em->contains($user));
64         $this->assertTrue($user->phonenumbers->isInitialized());
65
66         $serialized = serialize($user);
67         $this->_em->clear();
68         $this->assertFalse($this->_em->contains($user));
69         unset($user);
70
71         $user = unserialize($serialized);
72
73         $this->assertEquals(1, count($user->getPhonenumbers()), "Pre-Condition: 1 Phonenumber");
74
75         $ph2 = new CmsPhonenumber;
76         $ph2->phonenumber = "56789";
77         $user->addPhonenumber($ph2);
78         $oldPhonenumbers = $user->getPhonenumbers();
79         $this->assertEquals(2, count($oldPhonenumbers), "Pre-Condition: 2 Phonenumbers");
80         $this->assertFalse($this->_em->contains($user));
81
82         $this->_em->persist($ph2);
83
84         // Merge back in
85         $user = $this->_em->merge($user); // merge cascaded to phonenumbers
86         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user->phonenumbers[0]->user);
87         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user->phonenumbers[1]->user);
88         $im = $this->_em->getUnitOfWork()->getIdentityMap();
89         $this->_em->flush();
90
91         $this->assertTrue($this->_em->contains($user), "Failed to assert that merged user is contained inside EntityManager persistence context.");
92         $phonenumbers = $user->getPhonenumbers();
93         $this->assertNotSame($oldPhonenumbers, $phonenumbers, "Merge should replace the Detached Collection with a new PersistentCollection.");
94         $this->assertEquals(2, count($phonenumbers), "Failed to assert that two phonenumbers are contained in the merged users phonenumber collection.");
95
96         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsPhonenumber', $phonenumbers[1]);
97         $this->assertTrue($this->_em->contains($phonenumbers[1]), "Failed to assert that second phonenumber in collection is contained inside EntityManager persistence context.");
98
99         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsPhonenumber', $phonenumbers[0]);
100         $this->assertTrue($this->_em->getUnitOfWork()->isInIdentityMap($phonenumbers[0]));
101         $this->assertTrue($this->_em->contains($phonenumbers[0]), "Failed to assert that first phonenumber in collection is contained inside EntityManager persistence context.");
102     }
103
104     /**
105      * @group DDC-203
106      */
107     public function testDetachedEntityThrowsExceptionOnFlush()
108     {
109         $ph = new CmsPhonenumber();
110         $ph->phonenumber = '12345';
111         $this->_em->persist($ph);
112         $this->_em->flush();
113         $this->_em->clear();
114         $this->_em->persist($ph);
115         try {
116             $this->_em->flush();
117             $this->fail();
118         } catch (\Exception $expected) {}
119     }
120
121     public function testUninitializedLazyAssociationsAreIgnoredOnMerge()
122     {
123         $user = new CmsUser;
124         $user->name = 'Guilherme';
125         $user->username = 'gblanco';
126         $user->status = 'developer';
127
128         $address = new CmsAddress;
129         $address->city = 'Berlin';
130         $address->country = 'Germany';
131         $address->street = 'Sesamestreet';
132         $address->zip = 12345;
133         $address->setUser($user);
134         $this->_em->persist($address);
135         $this->_em->persist($user);
136
137         $this->_em->flush();
138         $this->_em->clear();
139
140         $address2 = $this->_em->find(get_class($address), $address->id);
141         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $address2->user);
142         $this->assertFalse($address2->user->__isInitialized__);
143         $detachedAddress2 = unserialize(serialize($address2));
144         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $detachedAddress2->user);
145         $this->assertFalse($detachedAddress2->user->__isInitialized__);
146
147         $managedAddress2 = $this->_em->merge($detachedAddress2);
148         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $managedAddress2->user);
149         $this->assertFalse($managedAddress2->user === $detachedAddress2->user);
150         $this->assertFalse($managedAddress2->user->__isInitialized__);
151     }
152
153     /**
154      * @group DDC-822
155      */
156     public function testUseDetachedEntityAsQueryParameter()
157     {
158         $user = new CmsUser;
159         $user->name = 'Guilherme';
160         $user->username = 'gblanco';
161         $user->status = 'developer';
162
163         $this->_em->persist($user);
164
165         $this->_em->flush();
166         $this->_em->detach($user);
167
168         $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
169         $query = $this->_em->createQuery($dql);
170         $query->setParameter(1, $user);
171
172         $newUser = $query->getSingleResult();
173
174         $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $newUser);
175         $this->assertEquals('gblanco', $newUser->username);
176     }
177
178     /**
179      * @group DDC-920
180      */
181     public function testDetachManagedUnpersistedEntity()
182     {
183         $user = new CmsUser;
184         $user->name = 'Guilherme';
185         $user->username = 'gblanco';
186         $user->status = 'developer';
187
188         $this->_em->persist($user);
189         $this->_em->detach($user);
190
191         $this->_em->flush();
192
193         $this->assertFalse($this->_em->contains($user));
194         $this->assertFalse($this->_em->getUnitOfWork()->isInIdentityMap($user));
195     }
196
197     /**
198      * @group DDC-1340
199      */
200     public function testMergeArticleWrongVersion()
201     {
202         $article = new CmsArticle();
203         $article->topic = "test";
204         $article->text = "test";
205
206         $this->_em->persist($article);
207         $this->_em->flush();
208
209         $this->_em->detach($article);
210
211         $sql = "UPDATE cms_articles SET version = version+1 WHERE id = " . $article->id;
212         $this->_em->getConnection()->executeUpdate($sql);
213
214         $this->setExpectedException('Doctrine\ORM\OptimisticLockException', 'The optimistic lock failed, version 1 was expected, but is actually 2');
215         $this->_em->merge($article);
216     }
217 }
218