DetachedEntityTest.php 7.5KB

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