Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC729Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 require_once __DIR__ . '/../../../TestInit.php';
6
7 class DDC729Test extends \Doctrine\Tests\OrmFunctionalTestCase
8 {
9     public function setUp()
10     {
11         parent::setUp();
12
13         try {
14             $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em);
15             $schemaTool->createSchema(array(
16                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC729A'),
17                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC729B'),
18             ));
19         } catch(\Exception $e) {
20
21         }
22     }
23
24     public function testMergeManyToMany()
25     {
26         $a = new DDC729A();
27         $b = new DDC729B();
28         $a->related[] = $b;
29
30         $this->_em->persist($a);
31         $this->_em->persist($b);
32         $this->_em->flush();
33         $this->_em->clear();
34         $aId = $a->id;
35
36         $a = new DDC729A();
37         $a->id = $aId;
38
39         $this->assertInstanceOf('Doctrine\Common\Collections\ArrayCollection', $a->related);
40
41         $a = $this->_em->merge($a);
42
43         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $a->related);
44
45         $this->assertFalse($a->related->isInitialized(), "Collection should not be marked initialized.");
46         $this->assertFalse($a->related->isDirty(), "Collection should not be marked as dirty.");
47
48         $this->_em->flush();
49         $this->_em->clear();
50
51         $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
52         $this->assertEquals(1, count($a->related));
53     }
54
55     public function testUnidirectionalMergeManyToMany()
56     {
57         $a = new DDC729A();
58         $b1 = new DDC729B();
59         $b2 = new DDC729B();
60         $a->related[] = $b1;
61
62         $this->_em->persist($a);
63         $this->_em->persist($b1);
64         $this->_em->persist($b2);
65         $this->_em->flush();
66         $this->_em->clear();
67         $aId = $a->id;
68
69         $a = new DDC729A();
70         $a->id = $aId;
71
72         $a = $this->_em->merge($a);
73
74         $a->related->set(0, $this->_em->merge($b1));
75
76         $a->related->set(1, $this->_em->merge($b2));
77
78         $this->_em->flush();
79         $this->_em->clear();
80
81         $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
82         $this->assertEquals(2, count($a->related));
83     }
84
85     public function testBidirectionalMergeManyToMany()
86     {
87         $a = new DDC729A();
88         $b1 = new DDC729B();
89         $b2 = new DDC729B();
90         $a->related[] = $b1;
91
92         $this->_em->persist($a);
93         $this->_em->persist($b1);
94         $this->_em->persist($b2);
95         $this->_em->flush();
96         $this->_em->clear();
97         $aId = $a->id;
98
99         $a = new DDC729A();
100         $a->id = $aId;
101
102         $a = $this->_em->merge($a);
103
104         $a->related->set(0, $this->_em->merge($b1));
105         $b1->related->set(0, $a);
106
107         $a->related->set(1, $this->_em->merge($b2));
108         $b2->related->set(0, $a);
109
110         $this->_em->flush();
111         $this->_em->clear();
112
113         $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
114         $this->assertEquals(2, count($a->related));
115     }
116
117     public function testBidirectionalMultiMergeManyToMany()
118     {
119         $a = new DDC729A();
120         $b1 = new DDC729B();
121         $b2 = new DDC729B();
122         $a->related[] = $b1;
123
124         $this->_em->persist($a);
125         $this->_em->persist($b1);
126         $this->_em->persist($b2);
127         $this->_em->flush();
128         $this->_em->clear();
129         $aId = $a->id;
130
131         $a = new DDC729A();
132         $a->id = $aId;
133
134         $a = $this->_em->merge($a);
135
136         $a->related->set(0, $this->_em->merge($b1));
137         $b1->related->set(0, $this->_em->merge($a));
138
139         $a->related->set(1, $this->_em->merge($b2));
140         $b2->related->set(0, $this->_em->merge($a));
141
142         $this->_em->flush();
143         $this->_em->clear();
144
145         $a = $this->_em->find(__NAMESPACE__ . '\DDC729A', $aId);
146         $this->assertEquals(2, count($a->related));
147     }
148 }
149
150 /**
151  * @Entity
152  */
153 class DDC729A
154 {
155     /** @Id @GeneratedValue @Column(type="integer") */
156     public $id;
157
158     /** @ManyToMany(targetEntity="DDC729B", inversedBy="related") */
159     public $related;
160
161     public function __construct()
162     {
163         $this->related = new \Doctrine\Common\Collections\ArrayCollection();
164     }
165 }
166
167 /**
168  * @Entity
169  */
170 class DDC729B
171 {
172     /** @Id @GeneratedValue @Column(type="integer") */
173     public $id;
174
175     /** @ManyToMany(targetEntity="DDC729B", mappedBy="related") */
176     public $related;
177
178     public function __construct()
179     {
180         $this->related = new \Doctrine\Common\Collections\ArrayCollection();
181     }
182 }