Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / ClassTableInheritanceTest2.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 require_once __DIR__ . '/../../TestInit.php';
6
7 /**
8  * Functional tests for the Class Table Inheritance mapping strategy.
9  *
10  * @author robo
11  */
12 class ClassTableInheritanceTest2 extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14     protected function setUp() {
15         parent::setUp();
16         try {
17             $this->_schemaTool->createSchema(array(
18                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIParent'),
19                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIChild'),
20                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIRelated'),
21                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\CTIRelated2')
22             ));
23         } catch (\Exception $ignored) {
24             // Swallow all exceptions. We do not test the schema tool here.
25         }
26     }
27
28     public function testOneToOneAssocToBaseTypeBidirectional()
29     {
30         $child = new CTIChild;
31         $child->setData('hello');
32
33         $related = new CTIRelated;
34         $related->setCTIParent($child);
35
36         $this->_em->persist($related);
37         $this->_em->persist($child);
38
39         $this->_em->flush();
40         $this->_em->clear();
41
42         $relatedId = $related->getId();
43
44         $related2 = $this->_em->find('Doctrine\Tests\ORM\Functional\CTIRelated', $relatedId);
45
46         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\CTIRelated', $related2);
47         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\CTIChild', $related2->getCTIParent());
48         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $related2->getCTIParent());
49         $this->assertEquals('hello', $related2->getCTIParent()->getData());
50
51         $this->assertSame($related2, $related2->getCTIParent()->getRelated());
52     }
53
54     public function testManyToManyToCTIHierarchy()
55     {
56         //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
57         $mmrel = new CTIRelated2;
58         $child = new CTIChild;
59         $child->setData('child');
60         $mmrel->addCTIChild($child);
61
62         $this->_em->persist($mmrel);
63         $this->_em->persist($child);
64
65         $this->_em->flush();
66         $this->_em->clear();
67
68         $mmrel2 = $this->_em->find(get_class($mmrel), $mmrel->getId());
69         $this->assertFalse($mmrel2->getCTIChildren()->isInitialized());
70         $this->assertEquals(1, count($mmrel2->getCTIChildren()));
71         $this->assertTrue($mmrel2->getCTIChildren()->isInitialized());
72         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\CTIChild', $mmrel2->getCTIChildren()->get(0));
73     }
74 }
75
76 /**
77  * @Entity @Table(name="cti_parents")
78  * @InheritanceType("JOINED")
79  * @DiscriminatorColumn(name="type", type="string")
80  * @DiscriminatorMap({"parent" = "CTIParent", "child" = "CTIChild"})
81  */
82 class CTIParent {
83    /**
84      * @Id @Column(type="integer")
85      * @GeneratedValue(strategy="AUTO")
86      */
87     private $id;
88
89     /** @OneToOne(targetEntity="CTIRelated", mappedBy="ctiParent") */
90     private $related;
91
92     public function getId() {
93         return $this->id;
94     }
95
96     public function getRelated() {
97         return $this->related;
98     }
99
100     public function setRelated($related) {
101         $this->related = $related;
102         $related->setCTIParent($this);
103     }
104 }
105
106 /**
107  * @Entity @Table(name="cti_children")
108  */
109 class CTIChild extends CTIParent {
110    /**
111      * @Column(type="string")
112      */
113     private $data;
114
115     public function getData() {
116         return $this->data;
117     }
118
119     public function setData($data) {
120         $this->data = $data;
121     }
122
123 }
124
125 /** @Entity */
126 class CTIRelated {
127     /**
128      * @Id @Column(type="integer")
129      * @GeneratedValue(strategy="AUTO")
130      */
131     private $id;
132
133     /**
134      * @OneToOne(targetEntity="CTIParent")
135      * @JoinColumn(name="ctiparent_id", referencedColumnName="id")
136      */
137     private $ctiParent;
138
139     public function getId() {
140         return $this->id;
141     }
142
143     public function getCTIParent() {
144         return $this->ctiParent;
145     }
146
147     public function setCTIParent($ctiParent) {
148         $this->ctiParent = $ctiParent;
149     }
150 }
151
152 /** @Entity */
153 class CTIRelated2
154 {
155     /** @Id @Column(type="integer") @GeneratedValue */
156     private $id;
157     /** @ManyToMany(targetEntity="CTIChild") */
158     private $ctiChildren;
159
160
161     public function __construct() {
162         $this->ctiChildren = new \Doctrine\Common\Collections\ArrayCollection;
163     }
164
165     public function getId() {
166         return $this->id;
167     }
168
169     public function addCTIChild(CTIChild $child) {
170         $this->ctiChildren->add($child);
171     }
172
173     public function getCTIChildren() {
174         return $this->ctiChildren;
175     }
176 }