OneToOneUnidirectionalAssociationTest.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
  4. use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
  5. use Doctrine\ORM\Mapping\AssociationMapping;
  6. use Doctrine\ORM\Mapping\ClassMetadata;
  7. use Doctrine\ORM\Query;
  8. require_once __DIR__ . '/../../TestInit.php';
  9. /**
  10. * Tests a unidirectional one-to-one association mapping (without inheritance).
  11. * Inverse side is not present.
  12. */
  13. class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
  14. {
  15. private $product;
  16. private $shipping;
  17. protected function setUp()
  18. {
  19. $this->useModelSet('ecommerce');
  20. parent::setUp();
  21. $this->product = new ECommerceProduct();
  22. $this->product->setName('Doctrine 2 Manual');
  23. $this->shipping = new ECommerceShipping();
  24. $this->shipping->setDays('5');
  25. }
  26. public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
  27. $this->product->setShipping($this->shipping);
  28. $this->_em->persist($this->product);
  29. $this->_em->flush();
  30. $this->assertForeignKeyIs($this->shipping->getId());
  31. }
  32. public function testRemovesOneToOneAssociation()
  33. {
  34. $this->product->setShipping($this->shipping);
  35. $this->_em->persist($this->product);
  36. $this->product->removeShipping();
  37. $this->_em->flush();
  38. $this->assertForeignKeyIs(null);
  39. }
  40. public function _testEagerLoad()
  41. {
  42. $this->_createFixture();
  43. $query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s');
  44. $result = $query->getResult();
  45. $product = $result[0];
  46. $this->assertTrue($product->getShipping() instanceof ECommerceShipping);
  47. $this->assertEquals(1, $product->getShipping()->getDays());
  48. }
  49. public function testLazyLoadsObjects() {
  50. $this->_createFixture();
  51. $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
  52. $metadata->associationMappings['shipping']['fetch'] = ClassMetadata::FETCH_LAZY;
  53. $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
  54. $result = $query->getResult();
  55. $product = $result[0];
  56. $this->assertTrue($product->getShipping() instanceof ECommerceShipping);
  57. $this->assertEquals(1, $product->getShipping()->getDays());
  58. }
  59. public function testDoesNotLazyLoadObjectsIfConfigurationDoesNotAllowIt() {
  60. $this->_createFixture();
  61. $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
  62. $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
  63. $result = $query->getResult();
  64. $product = $result[0];
  65. $this->assertNull($product->getShipping());
  66. }
  67. protected function _createFixture()
  68. {
  69. $product = new ECommerceProduct;
  70. $product->setName('Php manual');
  71. $shipping = new ECommerceShipping;
  72. $shipping->setDays('1');
  73. $product->setShipping($shipping);
  74. $this->_em->persist($product);
  75. $this->_em->flush();
  76. $this->_em->clear();
  77. }
  78. public function assertForeignKeyIs($value) {
  79. $foreignKey = $this->_em->getConnection()->executeQuery(
  80. 'SELECT shipping_id FROM ecommerce_products WHERE id=?',
  81. array($this->product->getId())
  82. )->fetchColumn();
  83. $this->assertEquals($value, $foreignKey);
  84. }
  85. /**
  86. * @group DDC-762
  87. */
  88. public function testNullForeignKey()
  89. {
  90. $product = new ECommerceProduct();
  91. $product->setName('Doctrine 2 Manual');
  92. $this->_em->persist($product);
  93. $this->_em->flush();
  94. $product = $this->_em->find(get_class($product), $product->getId());
  95. $this->assertNull($product->getShipping());
  96. }
  97. }