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