DefaultValuesTest.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  49. * @Entity @Table(name="defaultvalueuser")
  50. */
  51. class DefaultValueUser
  52. {
  53. /**
  54. * @Id @Column(type="integer")
  55. * @GeneratedValue(strategy="AUTO")
  56. */
  57. public $id;
  58. /**
  59. * @Column(type="string")
  60. */
  61. public $name = '';
  62. /**
  63. * @Column(type="string")
  64. */
  65. public $type = 'Poweruser';
  66. /**
  67. * @OneToOne(targetEntity="DefaultValueAddress", mappedBy="user", cascade={"persist"})
  68. */
  69. public $address;
  70. public function getId() {return $this->id;}
  71. }
  72. /**
  73. * CmsAddress
  74. *
  75. * @Entity @Table(name="defaultvalueaddresses")
  76. */
  77. class DefaultValueAddress
  78. {
  79. /**
  80. * @Column(type="integer")
  81. * @Id @GeneratedValue(strategy="AUTO")
  82. */
  83. public $id;
  84. /**
  85. * @Column(type="string", length=50)
  86. */
  87. public $country;
  88. /**
  89. * @Column(type="string", length=50)
  90. */
  91. public $zip;
  92. /**
  93. * @Column(type="string", length=50)
  94. */
  95. public $city;
  96. /**
  97. * Testfield for Schema Updating Tests.
  98. */
  99. public $street;
  100. /**
  101. * @OneToOne(targetEntity="DefaultValueUser")
  102. * @JoinColumn(name="user_id", referencedColumnName="id")
  103. */
  104. public $user;
  105. public function getUser() {return $this->user;}
  106. }