3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
6 use Doctrine\Tests\Models\ECommerce\ECommerceShipping;
7 use Doctrine\ORM\Mapping\AssociationMapping;
8 use Doctrine\ORM\Mapping\ClassMetadata;
9 use Doctrine\ORM\Query;
11 require_once __DIR__ . '/../../TestInit.php';
14 * Tests a unidirectional one-to-one association mapping (without inheritance).
15 * Inverse side is not present.
17 class OneToOneUnidirectionalAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
22 protected function setUp()
24 $this->useModelSet('ecommerce');
26 $this->product = new ECommerceProduct();
27 $this->product->setName('Doctrine 2 Manual');
28 $this->shipping = new ECommerceShipping();
29 $this->shipping->setDays('5');
32 public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
33 $this->product->setShipping($this->shipping);
34 $this->_em->persist($this->product);
37 $this->assertForeignKeyIs($this->shipping->getId());
40 public function testRemovesOneToOneAssociation()
42 $this->product->setShipping($this->shipping);
43 $this->_em->persist($this->product);
44 $this->product->removeShipping();
48 $this->assertForeignKeyIs(null);
51 public function _testEagerLoad()
53 $this->_createFixture();
55 $query = $this->_em->createQuery('select p, s from Doctrine\Tests\Models\ECommerce\ECommerceProduct p left join p.shipping s');
56 $result = $query->getResult();
57 $product = $result[0];
59 $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceShipping', $product->getShipping());
60 $this->assertEquals(1, $product->getShipping()->getDays());
63 public function testLazyLoadsObjects() {
64 $this->_createFixture();
65 $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
66 $metadata->associationMappings['shipping']['fetch'] = ClassMetadata::FETCH_LAZY;
68 $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
69 $result = $query->getResult();
70 $product = $result[0];
72 $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceShipping', $product->getShipping());
73 $this->assertEquals(1, $product->getShipping()->getDays());
76 public function testDoesNotLazyLoadObjectsIfConfigurationDoesNotAllowIt() {
77 $this->_createFixture();
79 $query = $this->_em->createQuery('select p from Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
80 $query->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true);
82 $result = $query->getResult();
83 $product = $result[0];
85 $this->assertNull($product->getShipping());
88 protected function _createFixture()
90 $product = new ECommerceProduct;
91 $product->setName('Php manual');
92 $shipping = new ECommerceShipping;
93 $shipping->setDays('1');
94 $product->setShipping($shipping);
96 $this->_em->persist($product);
102 public function assertForeignKeyIs($value) {
103 $foreignKey = $this->_em->getConnection()->executeQuery(
104 'SELECT shipping_id FROM ecommerce_products WHERE id=?',
105 array($this->product->getId())
107 $this->assertEquals($value, $foreignKey);
113 public function testNullForeignKey()
115 $product = new ECommerceProduct();
116 $product->setName('Doctrine 2 Manual');
118 $this->_em->persist($product);
121 $product = $this->_em->find(get_class($product), $product->getId());
123 $this->assertNull($product->getShipping());