DDC1228Test.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. $profile = new DDC1228Profile();
  27. $profile->name = "Foo";
  28. $user->profile = $profile;
  29. $this->_em->persist($user);
  30. $this->_em->persist($profile);
  31. $this->_em->flush();
  32. $this->_em->clear();
  33. $user = $this->_em->find(__NAMESPACE__ . '\\DDC1228User', $user->id);
  34. $this->assertFalse($user->getProfile()->__isInitialized__, "Proxy is not initialized");
  35. $user->getProfile()->setName("Bar");
  36. $this->assertTrue($user->getProfile()->__isInitialized__, "Proxy is not initialized");
  37. $this->assertEquals("Bar", $user->getProfile()->getName());
  38. $this->assertEquals(array("id" => 1, "name" => "Foo"), $this->_em->getUnitOfWork()->getOriginalEntityData($user->getProfile()));
  39. $this->_em->flush();
  40. $this->_em->clear();
  41. $user = $this->_em->find(__NAMESPACE__ . '\\DDC1228User', $user->id);
  42. $this->assertEquals("Bar", $user->getProfile()->getName());
  43. }
  44. public function testRefresh()
  45. {
  46. $user = new DDC1228User;
  47. $profile = new DDC1228Profile();
  48. $profile->name = "Foo";
  49. $user->profile = $profile;
  50. $this->_em->persist($user);
  51. $this->_em->persist($profile);
  52. $this->_em->flush();
  53. $this->_em->clear();
  54. $user = $this->_em->getReference(__NAMESPACE__ . '\\DDC1228User', $user->id);
  55. $this->_em->refresh($user);
  56. $user->name = "Baz";
  57. $this->_em->flush();
  58. $this->_em->clear();
  59. $user = $this->_em->find(__NAMESPACE__ . '\\DDC1228User', $user->id);
  60. $this->assertEquals("Baz", $user->name);
  61. }
  62. }
  63. /**
  64. * @Entity
  65. */
  66. class DDC1228User
  67. {
  68. /**
  69. * @Id @Column(type="integer") @GeneratedValue
  70. * @var int
  71. */
  72. public $id;
  73. /**
  74. * @Column(type="string")
  75. * @var string
  76. */
  77. public $name = 'Bar';
  78. /**
  79. * @OneToOne(targetEntity="DDC1228Profile")
  80. * @var Profile
  81. */
  82. public $profile;
  83. public function getProfile()
  84. {
  85. return $this->profile;
  86. }
  87. }
  88. /**
  89. * @Entity
  90. */
  91. class DDC1228Profile
  92. {
  93. /**
  94. * @Id @Column(type="integer") @GeneratedValue
  95. * @var int
  96. */
  97. public $id;
  98. /**
  99. * @column(type="string")
  100. * @var string
  101. */
  102. public $name;
  103. public function getName()
  104. {
  105. return $this->name;
  106. }
  107. public function setName($name)
  108. {
  109. $this->name = $name;
  110. }
  111. }