DefaultValuesTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. /**
  5. * Tests basic operations on entities with default values.
  6. *
  7. * @author robo
  8. */
  9. class DefaultValuesTest extends \Doctrine\Tests\OrmFunctionalTestCase
  10. {
  11. protected function setUp() {
  12. parent::setUp();
  13. try {
  14. $this->_schemaTool->createSchema(array(
  15. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\DefaultValueUser'),
  16. $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\DefaultValueAddress')
  17. ));
  18. } catch (\Exception $e) {
  19. // Swallow all exceptions. We do not test the schema tool here.
  20. }
  21. }
  22. public function testSimpleDetachMerge() {
  23. $user = new DefaultValueUser;
  24. $user->name = 'romanb';
  25. $this->_em->persist($user);
  26. $this->_em->flush();
  27. $this->_em->clear();
  28. $userId = $user->id; // e.g. from $_REQUEST
  29. $user2 = $this->_em->getReference(get_class($user), $userId);
  30. $this->_em->flush();
  31. $this->assertFalse($user2->__isInitialized__);
  32. $a = new DefaultValueAddress;
  33. $a->country = 'de';
  34. $a->zip = '12345';
  35. $a->city = 'Berlin';
  36. $a->street = 'Sesamestreet';
  37. $a->user = $user2;
  38. $this->_em->persist($a);
  39. $this->_em->flush();
  40. $this->assertFalse($user2->__isInitialized__);
  41. $this->_em->clear();
  42. $a2 = $this->_em->find(get_class($a), $a->id);
  43. $this->assertTrue($a2->getUser() instanceof DefaultValueUser);
  44. $this->assertEquals($userId, $a2->getUser()->getId());
  45. $this->assertEquals('Poweruser', $a2->getUser()->type);
  46. }
  47. /**
  48. * @group DDC-1386
  49. */
  50. public function testGetPartialReferenceWithDefaultValueNotEvalutedInFlush()
  51. {
  52. $user = new DefaultValueUser;
  53. $user->name = 'romanb';
  54. $user->type = 'Normaluser';
  55. $this->_em->persist($user);
  56. $this->_em->flush();
  57. $this->_em->clear();
  58. $user = $this->_em->getPartialReference('Doctrine\Tests\ORM\Functional\DefaultValueUser', $user->id);
  59. $this->assertTrue($this->_em->getUnitOfWork()->isReadOnly($user));
  60. $this->_em->flush();
  61. $this->_em->clear();
  62. $user = $this->_em->find('Doctrine\Tests\ORM\Functional\DefaultValueUser', $user->id);
  63. $this->assertEquals('Normaluser', $user->type);
  64. }
  65. }
  66. /**
  67. * @Entity @Table(name="defaultvalueuser")
  68. */
  69. class DefaultValueUser
  70. {
  71. /**
  72. * @Id @Column(type="integer")
  73. * @GeneratedValue(strategy="AUTO")
  74. */
  75. public $id;
  76. /**
  77. * @Column(type="string")
  78. */
  79. public $name = '';
  80. /**
  81. * @Column(type="string")
  82. */
  83. public $type = 'Poweruser';
  84. /**
  85. * @OneToOne(targetEntity="DefaultValueAddress", mappedBy="user", cascade={"persist"})
  86. */
  87. public $address;
  88. public function getId() {return $this->id;}
  89. }
  90. /**
  91. * CmsAddress
  92. *
  93. * @Entity @Table(name="defaultvalueaddresses")
  94. */
  95. class DefaultValueAddress
  96. {
  97. /**
  98. * @Column(type="integer")
  99. * @Id @GeneratedValue(strategy="AUTO")
  100. */
  101. public $id;
  102. /**
  103. * @Column(type="string", length=50)
  104. */
  105. public $country;
  106. /**
  107. * @Column(type="string", length=50)
  108. */
  109. public $zip;
  110. /**
  111. * @Column(type="string", length=50)
  112. */
  113. public $city;
  114. /**
  115. * Testfield for Schema Updating Tests.
  116. */
  117. public $street;
  118. /**
  119. * @OneToOne(targetEntity="DefaultValueUser")
  120. * @JoinColumn(name="user_id", referencedColumnName="id")
  121. */
  122. public $user;
  123. public function getUser() {return $this->user;}
  124. }