DDC1228Test.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional\Ticket;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Tests\Models\CMS\CmsEmployee;
  5. require_once __DIR__ . '/../../../TestInit.php';
  6. /**
  7. * @group DDC-1228
  8. * @group DDC-1226
  9. */
  10. class DDC1228Test extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. public function setUp()
  13. {
  14. parent::setUp();
  15. try {
  16. $this->_schemaTool->createSchema(array(
  17. $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1228User'),
  18. $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1228Profile'),
  19. ));
  20. } catch(\Exception $e) {
  21. }
  22. }
  23. public function testOneToOnePersist()
  24. {
  25. $user = new DDC1228User;
  26. $user->name = "Foo";
  27. $profile = new DDC1228Profile();
  28. $profile->name = "Foo";
  29. $user->profile = $profile;
  30. $this->_em->persist($user);
  31. $this->_em->persist($profile);
  32. $this->_em->flush();
  33. $this->_em->clear();
  34. $user = $this->_em->find(__NAMESPACE__ . '\\DDC1228User', $user->id);
  35. $this->assertFalse($user->getProfile()->__isInitialized__, "Proxy is not initialized");
  36. $user->getProfile()->setName("Bar");
  37. $this->assertTrue($user->getProfile()->__isInitialized__, "Proxy is not initialized");
  38. $this->assertEquals("Bar", $user->getProfile()->getName());
  39. $this->assertEquals(array("id" => 1, "name" => "Foo"), $this->_em->getUnitOfWork()->getOriginalEntityData($user->getProfile()));
  40. $this->_em->flush();
  41. $this->_em->clear();
  42. $user = $this->_em->find(__NAMESPACE__ . '\\DDC1228User', $user->id);
  43. $this->assertEquals("Bar", $user->getProfile()->getName());
  44. }
  45. public function testRefresh()
  46. {
  47. $user = new DDC1228User;
  48. $user->name = "Foo";
  49. $profile = new DDC1228Profile();
  50. $profile->name = "Foo";
  51. $user->profile = $profile;
  52. $this->_em->persist($user);
  53. $this->_em->persist($profile);
  54. $this->_em->flush();
  55. $this->_em->clear();
  56. $user = $this->_em->getReference(__NAMESPACE__ . '\\DDC1228User', $user->id);
  57. $this->_em->refresh($user);
  58. $user->name = "Baz";
  59. $this->_em->flush();
  60. $this->_em->clear();
  61. $user = $this->_em->find(__NAMESPACE__ . '\\DDC1228User', $user->id);
  62. $this->assertEquals("Baz", $user->name);
  63. }
  64. }
  65. /**
  66. * @Entity
  67. */
  68. class DDC1228User
  69. {
  70. /**
  71. * @Id @Column(type="integer") @GeneratedValue
  72. * @var int
  73. */
  74. public $id;
  75. /**
  76. * @column(type="string")
  77. * @var string
  78. */
  79. public $name = '';
  80. /**
  81. * @OneToOne(targetEntity="DDC1228Profile")
  82. * @var Profile
  83. */
  84. public $profile;
  85. public function getProfile()
  86. {
  87. return $this->profile;
  88. }
  89. }
  90. /**
  91. * @Entity
  92. */
  93. class DDC1228Profile
  94. {
  95. /**
  96. * @Id @Column(type="integer") @GeneratedValue
  97. * @var int
  98. */
  99. public $id;
  100. /**
  101. * @column(type="string")
  102. * @var string
  103. */
  104. public $name;
  105. public function getName()
  106. {
  107. return $this->name;
  108. }
  109. public function setName($name)
  110. {
  111. $this->name = $name;
  112. }
  113. }