Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC522Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 use Doctrine\Tests\Models\Company\CompanyPerson;
6
7 require_once __DIR__ . '/../../../TestInit.php';
8
9 /**
10  * Tests that join columns (foreign keys) can be named the same as the association
11  * fields they're used on without causing issues.
12  */
13 class DDC522Test extends \Doctrine\Tests\OrmFunctionalTestCase
14 {
15     protected function setUp()
16     {
17         parent::setUp();
18         try {
19             $this->_schemaTool->createSchema(array(
20                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC522Customer'),
21                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC522Cart'),
22                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC522ForeignKeyTest')
23             ));
24         } catch(\Exception $e) {
25
26         }
27     }
28
29     /**
30      * @group DDC-522
31      */
32     public function testJoinColumnWithSameNameAsAssociationField()
33     {
34         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
35
36         $cust = new DDC522Customer;
37         $cust->name = "name";
38         $cart = new DDC522Cart;
39         $cart->total = 0;
40         $cust->cart = $cart;
41         $cart->customer = $cust;
42         $this->_em->persist($cust);
43         $this->_em->persist($cart);
44         $this->_em->flush();
45
46         $this->_em->clear();
47
48         $r = $this->_em->createQuery("select ca,c from ".get_class($cart)." ca join ca.customer c")
49                 ->getResult();
50
51         $this->assertInstanceOf(__NAMESPACE__ . '\DDC522Cart', $r[0]);
52         $this->assertInstanceOf(__NAMESPACE__ . '\DDC522Customer', $r[0]->customer);
53         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $r[0]->customer);
54         $this->assertEquals('name', $r[0]->customer->name);
55
56         $fkt = new DDC522ForeignKeyTest();
57         $fkt->cartId = $r[0]->id; // ignored for persistence
58         $fkt->cart = $r[0]; // must be set properly
59         $this->_em->persist($fkt);
60         $this->_em->flush();
61         $this->_em->clear();
62
63         $fkt2 = $this->_em->find(get_class($fkt), $fkt->id);
64         $this->assertEquals($fkt->cart->id, $fkt2->cartId);
65         $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $fkt2->cart);
66         $this->assertFalse($fkt2->cart->__isInitialized__);
67     }
68
69     /**
70      * @group DDC-522
71      * @group DDC-762
72      */
73     public function testJoinColumnWithNullSameNameAssociationField()
74     {
75         $fkCust = new DDC522ForeignKeyTest;
76         $fkCust->name = "name";
77         $fkCust->cart = null;
78
79         $this->_em->persist($fkCust);
80         $this->_em->flush();
81         $this->_em->clear();
82
83         $newCust = $this->_em->find(get_class($fkCust), $fkCust->id);
84     }
85 }
86
87 /** @Entity */
88 class DDC522Customer {
89     /** @Id @Column(type="integer") @GeneratedValue */
90     public $id;
91     /** @Column */
92     public $name;
93     /** @OneToOne(targetEntity="DDC522Cart", mappedBy="customer") */
94     public $cart;
95 }
96
97 /** @Entity */
98 class DDC522Cart {
99     /** @Id @Column(type="integer") @GeneratedValue */
100     public $id;
101     /** @Column(type="integer") */
102     public $total;
103     /**
104      * @OneToOne(targetEntity="DDC522Customer", inversedBy="cart")
105      * @JoinColumn(name="customer", referencedColumnName="id")
106      */
107     public $customer;
108 }
109
110 /** @Entity */
111 class DDC522ForeignKeyTest {
112     /** @Id @Column(type="integer") @GeneratedValue */
113     public $id;
114     /** @Column(type="integer", name="cart_id", nullable=true) */
115     public $cartId;
116     /**
117      * @OneToOne(targetEntity="DDC522Cart")
118      * @JoinColumn(name="cart_id", referencedColumnName="id")
119      */
120     public $cart;
121 }