Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / OneToOneSelfReferentialAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
6 use Doctrine\ORM\Mapping\AssociationMapping;
7 use Doctrine\ORM\Mapping\ClassMetadata;
8
9 require_once __DIR__ . '/../../TestInit.php';
10
11 /**
12  * Tests a self referential one-to-one association mapping (without inheritance).
13  * Relation is defined as the mentor that a customer choose. The mentor could
14  * help only one other customer, while a customer can choose only one mentor
15  * for receiving support.
16  * Inverse side is not present.
17  */
18 class OneToOneSelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
19 {
20     private $customer;
21     private $mentor;
22
23     protected function setUp()
24     {
25         $this->useModelSet('ecommerce');
26         parent::setUp();
27         $this->customer = new ECommerceCustomer();
28         $this->customer->setName('Anakin Skywalker');
29         $this->mentor = new ECommerceCustomer();
30         $this->mentor->setName('Obi-wan Kenobi');
31     }
32
33     public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
34         $this->customer->setMentor($this->mentor);
35         $this->_em->persist($this->customer);
36         $this->_em->flush();
37
38         $this->assertForeignKeyIs($this->mentor->getId());
39     }
40
41     public function testRemovesOneToOneAssociation()
42     {
43         $this->customer->setMentor($this->mentor);
44         $this->_em->persist($this->customer);
45         $this->customer->removeMentor();
46
47         $this->_em->flush();
48
49         $this->assertForeignKeyIs(null);
50     }
51
52     public function testFind()
53     {
54         $id = $this->_createFixture();
55
56         $customer = $this->_em->find('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $id);
57         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $customer->getMentor());
58     }
59
60     public function testEagerLoadsAssociation()
61     {
62         $this->_createFixture();
63
64         $query = $this->_em->createQuery('select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m order by c.id asc');
65         $result = $query->getResult();
66         $customer = $result[0];
67         $this->assertLoadingOfAssociation($customer);
68     }
69
70     /**
71      * @group mine
72      * @return unknown_type
73      */
74     public function testLazyLoadsAssociation()
75     {
76         $this->_createFixture();
77
78         $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer');
79         $metadata->associationMappings['mentor']['fetch'] = ClassMetadata::FETCH_LAZY;
80
81         $query = $this->_em->createQuery("select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c where c.name='Luke Skywalker'");
82         $result = $query->getResult();
83         $customer = $result[0];
84         $this->assertLoadingOfAssociation($customer);
85     }
86
87     public function testMultiSelfReference()
88     {
89         try {
90             $this->_schemaTool->createSchema(array(
91                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\MultiSelfReference')
92             ));
93         } catch (\Exception $e) {
94             // Swallow all exceptions. We do not test the schema tool here.
95         }
96
97         $entity1 = new MultiSelfReference();
98         $this->_em->persist($entity1);
99         $entity1->setOther1($entity2 = new MultiSelfReference);
100         $entity1->setOther2($entity3 = new MultiSelfReference);
101         $this->_em->flush();
102
103         $this->_em->clear();
104
105         $entity2 = $this->_em->find(get_class($entity1), $entity1->getId());
106
107         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\MultiSelfReference', $entity2->getOther1());
108         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\MultiSelfReference', $entity2->getOther2());
109         $this->assertNull($entity2->getOther1()->getOther1());
110         $this->assertNull($entity2->getOther1()->getOther2());
111         $this->assertNull($entity2->getOther2()->getOther1());
112         $this->assertNull($entity2->getOther2()->getOther2());
113     }
114
115     public function assertLoadingOfAssociation($customer)
116     {
117         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCustomer', $customer->getMentor());
118         $this->assertEquals('Obi-wan Kenobi', $customer->getMentor()->getName());
119     }
120
121     public function assertForeignKeyIs($value) {
122         $foreignKey = $this->_em->getConnection()->executeQuery('SELECT mentor_id FROM ecommerce_customers WHERE id=?', array($this->customer->getId()))->fetchColumn();
123         $this->assertEquals($value, $foreignKey);
124     }
125
126     private function _createFixture()
127     {
128         $customer = new ECommerceCustomer;
129         $customer->setName('Luke Skywalker');
130         $mentor = new ECommerceCustomer;
131         $mentor->setName('Obi-wan Kenobi');
132         $customer->setMentor($mentor);
133
134         $this->_em->persist($customer);
135
136         $this->_em->flush();
137         $this->_em->clear();
138
139         return $customer->getId();
140     }
141 }
142
143 /**
144  * @Entity
145  */
146 class MultiSelfReference {
147     /** @Id @GeneratedValue(strategy="AUTO") @Column(type="integer") */
148     private $id;
149     /**
150      * @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
151      * @JoinColumn(name="other1", referencedColumnName="id")
152      */
153     private $other1;
154     /**
155      * @OneToOne(targetEntity="MultiSelfReference", cascade={"persist"})
156      * @JoinColumn(name="other2", referencedColumnName="id")
157      */
158     private $other2;
159
160     public function getId() {return $this->id;}
161     public function setOther1($other1) {$this->other1 = $other1;}
162     public function getOther1() {return $this->other1;}
163     public function setOther2($other2) {$this->other2 = $other2;}
164     public function getOther2() {return $this->other2;}
165 }