Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / OneToOneEagerLoadingTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\ORM\UnitOfWork;
6
7 require_once __DIR__ . '/../../TestInit.php';
8
9 /**
10  * @group DDC-952
11  */
12 class OneToOneEagerLoadingTest extends \Doctrine\Tests\OrmFunctionalTestCase
13 {
14     protected function setUp()
15     {
16         parent::setUp();
17         $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->_em);
18         try {
19             $schemaTool->createSchema(array(
20                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Train'),
21                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\TrainDriver'),
22                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\TrainOwner'),
23                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Waggon'),
24                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\TrainOrder'),
25             ));
26         } catch(\Exception $e) {}
27     }
28
29     public function testEagerLoadOneToOneOwningSide()
30     {
31         $train = new Train(new TrainOwner("Alexander"));
32         $driver = new TrainDriver("Benjamin");
33         $waggon = new Waggon();
34
35         $train->setDriver($driver);
36         $train->addWaggon($waggon);
37
38         $this->_em->persist($train); // cascades
39         $this->_em->flush();
40         $this->_em->clear();
41
42         $sqlCount = count($this->_sqlLoggerStack->queries);
43
44         $train = $this->_em->find(get_class($train), $train->id);
45         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $train->driver);
46         $this->assertEquals("Benjamin", $train->driver->name);
47
48         $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
49     }
50
51     public function testEagerLoadOneToOneNullOwningSide()
52     {
53         $train = new Train(new TrainOwner("Alexander"));
54
55         $this->_em->persist($train); // cascades
56         $this->_em->flush();
57         $this->_em->clear();
58
59         $sqlCount = count($this->_sqlLoggerStack->queries);
60
61         $train = $this->_em->find(get_class($train), $train->id);
62         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $train->driver);
63         $this->assertNull($train->driver);
64
65         $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
66     }
67
68     public function testEagerLoadOneToOneInverseSide()
69     {
70         $owner = new TrainOwner("Alexander");
71         $train = new Train($owner);
72
73         $this->_em->persist($train); // cascades
74         $this->_em->flush();
75         $this->_em->clear();
76
77         $sqlCount = count($this->_sqlLoggerStack->queries);
78
79         $driver = $this->_em->find(get_class($owner), $owner->id);
80         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $owner->train);
81         $this->assertNotNull($owner->train);
82
83         $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
84     }
85
86     public function testEagerLoadOneToOneNullInverseSide()
87     {
88         $driver = new TrainDriver("Dagny Taggert");
89
90         $this->_em->persist($driver);
91         $this->_em->flush();
92         $this->_em->clear();
93
94         $this->assertNull($driver->train);
95
96         $sqlCount = count($this->_sqlLoggerStack->queries);
97
98         $driver = $this->_em->find(get_class($driver), $driver->id);
99         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $driver->train);
100         $this->assertNull($driver->train);
101
102         $this->assertEquals($sqlCount + 1, count($this->_sqlLoggerStack->queries));
103     }
104
105     public function testEagerLoadManyToOne()
106     {
107         $train = new Train(new TrainOwner("Alexander"));
108         $waggon = new Waggon();
109         $train->addWaggon($waggon);
110
111         $this->_em->persist($train); // cascades
112         $this->_em->flush();
113         $this->_em->clear();
114
115         $waggon = $this->_em->find(get_class($waggon), $waggon->id);
116         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $waggon->train);
117         $this->assertNotNull($waggon->train);
118     }
119
120     public function testEagerLoadWithNullableColumnsGeneratesLeftJoinOnBothSides()
121     {
122         $train = new Train(new TrainOwner("Alexander"));
123         $driver = new TrainDriver("Benjamin");
124         $train->setDriver($driver);
125
126         $this->_em->persist($train);
127         $this->_em->flush();
128         $this->_em->clear();
129
130         $train = $this->_em->find(get_class($train), $train->id);
131         $this->assertEquals(
132             "SELECT t0.id AS id1, t0.driver_id AS driver_id2, t3.id AS id4, t3.name AS name5, t0.owner_id AS owner_id6, t7.id AS id8, t7.name AS name9 FROM Train t0 LEFT JOIN TrainDriver t3 ON t0.driver_id = t3.id INNER JOIN TrainOwner t7 ON t0.owner_id = t7.id WHERE t0.id = ?",
133             $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
134         );
135
136         $this->_em->clear();
137         $driver = $this->_em->find(get_class($driver), $driver->id);
138         $this->assertEquals(
139             "SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id IN (?)",
140             $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
141         );
142     }
143
144     public function testEagerLoadWithNonNullableColumnsGeneratesInnerJoinOnOwningSide()
145     {
146         $waggon = new Waggon();
147
148         // It should have a train
149         $train = new Train(new TrainOwner("Alexander"));
150         $train->addWaggon($waggon);
151
152         $this->_em->persist($train);
153         $this->_em->flush();
154         $this->_em->clear();
155
156         $waggon = $this->_em->find(get_class($waggon), $waggon->id);
157
158         // The last query is the eager loading of the owner of the train
159         $this->assertEquals(
160             "SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id IN (?)",
161             $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
162         );
163
164         // The one before is the fetching of the waggon and train
165         $this->assertEquals(
166             "SELECT t0.id AS id1, t0.train_id AS train_id2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM Waggon t0 INNER JOIN Train t3 ON t0.train_id = t3.id WHERE t0.id = ?",
167             $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery - 1]['sql']
168         );
169     }
170
171     public function testEagerLoadWithNonNullableColumnsGeneratesLeftJoinOnNonOwningSide()
172     {
173         $owner = new TrainOwner('Alexander');
174         $train = new Train($owner);
175         $this->_em->persist($train);
176         $this->_em->flush();
177         $this->_em->clear();
178
179         $waggon = $this->_em->find(get_class($owner), $owner->id);
180         $this->assertEquals(
181             "SELECT t0.id AS id1, t0.name AS name2, t3.id AS id4, t3.driver_id AS driver_id5, t3.owner_id AS owner_id6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id = ?",
182             $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['sql']
183         );
184     }
185
186     /**
187      * @group DDC-1946
188      */
189     public function testEagerLoadingDoesNotBreakRefresh()
190     {
191         $train = new Train(new TrainOwner('Johannes'));
192         $order = new TrainOrder($train);
193         $this->_em->persist($train);
194         $this->_em->persist($order);
195         $this->_em->flush();
196
197         $this->_em->getConnection()->exec("UPDATE TrainOrder SET train_id = NULL");
198
199         $this->assertSame($train, $order->train);
200         $this->_em->refresh($order);
201         $this->assertTrue($order->train === null, "Train reference was not refreshed to NULL.");
202     }
203 }
204
205 /**
206  * @Entity
207  */
208 class Train
209 {
210     /**
211      * @id @column(type="integer") @generatedValue
212      * @var int
213      */
214     public $id;
215     /**
216      * Owning side
217      * @OneToOne(targetEntity="TrainDriver", inversedBy="train", fetch="EAGER", cascade={"persist"})
218      * @JoinColumn(nullable=true)
219      */
220     public $driver;
221     /**
222      * Owning side
223      * @OneToOne(targetEntity="TrainOwner", inversedBy="train", fetch="EAGER", cascade={"persist"})
224      * @JoinColumn(nullable=false)
225      */
226     public $owner;
227     /**
228      * @oneToMany(targetEntity="Waggon", mappedBy="train", cascade={"persist"})
229      */
230     public $waggons;
231
232     public function __construct(TrainOwner $owner)
233     {
234         $this->waggons = new \Doctrine\Common\Collections\ArrayCollection();
235         $this->setOwner($owner);
236     }
237
238     public function setDriver(TrainDriver $driver)
239     {
240         $this->driver = $driver;
241         $driver->setTrain($this);
242     }
243
244     public function setOwner(TrainOwner $owner)
245     {
246         $this->owner = $owner;
247         $owner->setTrain($this);
248     }
249
250     public function addWaggon(Waggon $w)
251     {
252         $w->setTrain($this);
253         $this->waggons[] = $w;
254     }
255 }
256
257 /**
258  * @Entity
259  */
260 class TrainDriver
261 {
262     /** @Id @Column(type="integer") @GeneratedValue */
263     public $id;
264     /** @column(type="string") */
265     public $name;
266     /**
267      * Inverse side
268      * @OneToOne(targetEntity="Train", mappedBy="driver", fetch="EAGER")
269      */
270     public $train;
271
272     public function __construct($name)
273     {
274         $this->name = $name;
275     }
276
277     public function setTrain(Train $t)
278     {
279         $this->train = $t;
280     }
281 }
282
283 /**
284  * @Entity
285  */
286 class TrainOwner
287 {
288     /** @Id @Column(type="integer") @GeneratedValue */
289     public $id;
290     /** @column(type="string") */
291     public $name;
292     /**
293      * Inverse side
294      * @OneToOne(targetEntity="Train", mappedBy="owner", fetch="EAGER")
295      */
296     public $train;
297
298     public function __construct($name)
299     {
300         $this->name = $name;
301     }
302
303     public function setTrain(Train $t)
304     {
305         $this->train = $t;
306     }
307 }
308
309 /**
310  * @Entity
311  */
312 class Waggon
313 {
314     /** @id @generatedValue @column(type="integer") */
315     public $id;
316     /**
317      * @ManyToOne(targetEntity="Train", inversedBy="waggons", fetch="EAGER")
318      * @JoinColumn(nullable=false)
319      */
320     public $train;
321
322     public function setTrain($train)
323     {
324         $this->train = $train;
325     }
326 }
327
328 /**
329  * @Entity
330  */
331 class TrainOrder
332 {
333     /** @id @generatedValue @column(type="integer") */
334     public $id;
335
336     /** @OneToOne(targetEntity = "Train", fetch = "EAGER") */
337     public $train;
338
339     public function __construct(Train $train)
340     {
341         $this->train = $train;
342     }
343 }