OneToOneBidirectionalAssociationTest.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceCart;
  4. use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
  5. use Doctrine\ORM\Mapping\AssociationMapping;
  6. use Doctrine\ORM\Mapping\ClassMetadata;
  7. require_once __DIR__ . '/../../TestInit.php';
  8. /**
  9. * Tests a bidirectional one-to-one association mapping (without inheritance).
  10. */
  11. class OneToOneBidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
  12. {
  13. private $customer;
  14. private $cart;
  15. protected function setUp()
  16. {
  17. $this->useModelSet('ecommerce');
  18. parent::setUp();
  19. $this->customer = new ECommerceCustomer();
  20. $this->customer->setName('John Doe');
  21. $this->cart = new ECommerceCart();
  22. $this->cart->setPayment('Credit card');
  23. }
  24. public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
  25. $this->customer->setCart($this->cart);
  26. $this->_em->persist($this->customer);
  27. $this->_em->flush();
  28. $this->assertCartForeignKeyIs($this->customer->getId());
  29. }
  30. public function testDoesNotSaveAnInverseSideSet() {
  31. $this->customer->brokenSetCart($this->cart);
  32. $this->_em->persist($this->customer);
  33. $this->_em->flush();
  34. $this->assertCartForeignKeyIs(null);
  35. }
  36. public function testRemovesOneToOneAssociation()
  37. {
  38. $this->customer->setCart($this->cart);
  39. $this->_em->persist($this->customer);
  40. $this->customer->removeCart();
  41. $this->_em->flush();
  42. $this->assertCartForeignKeyIs(null);
  43. }
  44. public function testEagerLoad()
  45. {
  46. $this->_createFixture();
  47. $query = $this->_em->createQuery('select c, ca from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c join c.cart ca');
  48. $result = $query->getResult();
  49. $customer = $result[0];
  50. $this->assertTrue($customer->getCart() instanceof ECommerceCart);
  51. $this->assertEquals('paypal', $customer->getCart()->getPayment());
  52. }
  53. public function testLazyLoadsObjectsOnTheOwningSide() {
  54. $this->_createFixture();
  55. $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart');
  56. $metadata->associationMappings['customer']['fetchMode'] = ClassMetadata::FETCH_LAZY;
  57. $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCart c');
  58. $result = $query->getResult();
  59. $cart = $result[0];
  60. $this->assertTrue($cart->getCustomer() instanceof ECommerceCustomer);
  61. $this->assertEquals('Giorgio', $cart->getCustomer()->getName());
  62. }
  63. public function testInverseSideIsNeverLazy()
  64. {
  65. $this->_createFixture();
  66. $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer');
  67. $metadata->associationMappings['mentor']['fetch'] = ClassMetadata::FETCH_EAGER;
  68. $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c');
  69. $result = $query->getResult();
  70. $customer = $result[0];
  71. $this->assertNull($customer->getMentor());
  72. $this->assertTrue($customer->getCart() instanceof ECommerceCart);
  73. $this->assertFalse($customer->getCart() instanceof \Doctrine\ORM\Proxy\Proxy);
  74. $this->assertEquals('paypal', $customer->getCart()->getPayment());
  75. }
  76. public function testUpdateWithProxyObject()
  77. {
  78. $cust = new ECommerceCustomer;
  79. $cust->setName('Roman');
  80. $cart = new ECommerceCart;
  81. $cart->setPayment('CARD');
  82. $cust->setCart($cart);
  83. $this->_em->persist($cust);
  84. $this->_em->flush();
  85. $this->_em->clear();
  86. $this->assertTrue($cust->getCart() instanceof ECommerceCart);
  87. $this->assertEquals('Roman', $cust->getName());
  88. $this->assertSame($cust, $cart->getCustomer());
  89. $query = $this->_em->createQuery('select ca from Doctrine\Tests\Models\ECommerce\ECommerceCart ca where ca.id =?1');
  90. $query->setParameter(1, $cart->getId());
  91. $cart2 = $query->getSingleResult();
  92. $cart2->setPayment('CHEQUE');
  93. $this->_em->flush();
  94. $this->_em->clear();
  95. $query2 = $this->_em->createQuery('select ca, c from Doctrine\Tests\Models\ECommerce\ECommerceCart ca left join ca.customer c where ca.id =?1');
  96. $query2->setParameter(1, $cart->getId());
  97. $cart3 = $query2->getSingleResult();
  98. $this->assertTrue($cart3->getCustomer() instanceof ECommerceCustomer);
  99. $this->assertEquals('Roman', $cart3->getCustomer()->getName());
  100. }
  101. protected function _createFixture()
  102. {
  103. $customer = new ECommerceCustomer;
  104. $customer->setName('Giorgio');
  105. $cart = new ECommerceCart;
  106. $cart->setPayment('paypal');
  107. $customer->setCart($cart);
  108. $this->_em->persist($customer);
  109. $this->_em->flush();
  110. $this->_em->clear();
  111. }
  112. public function assertCartForeignKeyIs($value) {
  113. $foreignKey = $this->_em->getConnection()->executeQuery('SELECT customer_id FROM ecommerce_carts WHERE id=?', array($this->cart->getId()))->fetchColumn();
  114. $this->assertEquals($value, $foreignKey);
  115. }
  116. }