Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / AdvancedAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\ORM\Query;
6
7 require_once __DIR__ . '/../../TestInit.php';
8
9 /**
10  * Functional tests for the Single Table Inheritance mapping strategy.
11  *
12  * @author robo
13  */
14 class AdvancedAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
15 {
16     protected function setUp() {
17         parent::setUp();
18         try {
19             $this->_schemaTool->createSchema(array(
20                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Phrase'),
21                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\PhraseType'),
22                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Definition'),
23                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Lemma'),
24                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Type')
25             ));
26         } catch (\Exception $e) {
27             // Swallow all exceptions. We do not test the schema tool here.
28         }
29     }
30
31     public function testIssue()
32     {
33         //setup
34         $phrase = new Phrase;
35         $phrase->setPhrase('lalala');
36
37         $type = new PhraseType;
38         $type->setType('nonsense');
39         $type->setAbbreviation('non');
40
41         $def1 = new Definition;
42         $def1->setDefinition('def1');
43         $def2 = new Definition;
44         $def2->setDefinition('def2');
45
46         $phrase->setType($type);
47         $phrase->addDefinition($def1);
48         $phrase->addDefinition($def2);
49
50         $this->_em->persist($phrase);
51         $this->_em->persist($type);
52
53         $this->_em->flush();
54         $this->_em->clear();
55         //end setup
56
57         // test1 - lazy-loading many-to-one after find()
58         $phrase2 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
59         $this->assertTrue(is_numeric($phrase2->getType()->getId()));
60
61         $this->_em->clear();
62
63         // test2 - eager load in DQL query
64         $query = $this->_em->createQuery("SELECT p,t FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t");
65         $res = $query->getResult();
66         $this->assertEquals(1, count($res));
67         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\PhraseType', $res[0]->getType());
68         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $res[0]->getType()->getPhrases());
69         $this->assertFalse($res[0]->getType()->getPhrases()->isInitialized());
70
71         $this->_em->clear();
72
73         // test2 - eager load in DQL query with double-join back and forth
74         $query = $this->_em->createQuery("SELECT p,t,pp FROM Doctrine\Tests\ORM\Functional\Phrase p JOIN p.type t JOIN t.phrases pp");
75         $res = $query->getResult();
76         $this->assertEquals(1, count($res));
77         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\PhraseType', $res[0]->getType());
78         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $res[0]->getType()->getPhrases());
79         $this->assertTrue($res[0]->getType()->getPhrases()->isInitialized());
80
81         $this->_em->clear();
82
83         // test3 - lazy-loading one-to-many after find()
84         $phrase3 = $this->_em->find('Doctrine\Tests\ORM\Functional\Phrase', $phrase->getId());
85         $definitions = $phrase3->getDefinitions();
86         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $definitions);
87         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Definition', $definitions[0]);
88
89         $this->_em->clear();
90
91         // test4 - lazy-loading after DQL query
92         $query = $this->_em->createQuery("SELECT p FROM Doctrine\Tests\ORM\Functional\Phrase p");
93         $res = $query->getResult();
94         $definitions = $res[0]->getDefinitions();
95
96         $this->assertEquals(1, count($res));
97
98         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Definition', $definitions[0]);
99         $this->assertEquals(2, $definitions->count());
100     }
101
102     public function testManyToMany()
103     {
104         $lemma = new Lemma;
105         $lemma->setLemma('abu');
106
107         $type = new Type();
108         $type->setType('nonsense');
109         $type->setAbbreviation('non');
110
111         $lemma->addType($type);
112
113         $this->_em->persist($lemma);
114         $this->_em->persist($type);
115         $this->_em->flush();
116
117         // test5 ManyToMany
118         $query = $this->_em->createQuery("SELECT l FROM Doctrine\Tests\ORM\Functional\Lemma l");
119         $res = $query->getResult();
120         $types = $res[0]->getTypes();
121
122         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Type', $types[0]);
123     }
124 }
125
126 /**
127  * @Entity
128  * @Table(name="lemma")
129  */
130 class Lemma {
131
132         const CLASS_NAME = __CLASS__;
133
134         /**
135          * @var int
136          * @Id
137          * @Column(type="integer", name="lemma_id")
138          * @GeneratedValue(strategy="AUTO")
139          */
140         private $id;
141
142         /**
143          *
144          * @var string
145          * @Column(type="string", name="lemma_name", unique=true, length=255)
146          */
147         private $lemma;
148
149         /**
150          * @var kateglo\application\utilities\collections\ArrayCollection
151          * @ManyToMany(targetEntity="Type", mappedBy="lemmas", cascade={"persist"})
152          */
153         private $types;
154
155         public function __construct() {
156                 $this->types = new \Doctrine\Common\Collections\ArrayCollection();
157         }
158
159
160         /**
161          *
162          * @return int
163          */
164         public function getId(){
165                 return $this->id;
166         }
167
168         /**
169          *
170          * @param string $lemma
171          * @return void
172          */
173         public function setLemma($lemma){
174                 $this->lemma = $lemma;
175         }
176
177         /**
178          *
179          * @return string
180          */
181         public function getLemma(){
182                 return $this->lemma;
183         }
184
185         /**
186      *
187      * @param kateglo\application\models\Type $type
188      * @return void
189      */
190         public function addType(Type $type){
191         if (!$this->types->contains($type)) {
192             $this->types[] = $type;
193             $type->addLemma($this);
194         }
195     }
196
197     /**
198      *
199      * @param kateglo\application\models\Type $type
200      * @return void
201      */
202     public function removeType(Type $type)
203     {
204         $removed = $this->sources->removeElement($type);
205         if ($removed !== null) {
206             $removed->removeLemma($this);
207         }
208     }
209
210     /**
211      *
212      * @return kateglo\application\helpers\collections\ArrayCollection
213      */
214     public function getTypes()
215     {
216         return $this->types;
217     }
218 }
219
220 /**
221  * @Entity
222  * @Table(name="type")
223  */
224 class Type {
225
226         const CLASS_NAME = __CLASS__;
227
228         /**
229          *
230          * @var int
231          * @Id
232          * @Column(type="integer", name="type_id")
233          * @GeneratedValue(strategy="AUTO")
234          */
235         private $id;
236
237         /**
238          *
239          * @var string
240          * @Column(type="string", name="type_name", unique=true)
241          */
242         private $type;
243
244         /**
245          *
246          * @var string
247          * @Column(type="string", name="type_abbreviation", unique=true)
248          */
249         private $abbreviation;
250
251         /**
252          * @var kateglo\application\helpers\collections\ArrayCollection
253          * @ManyToMany(targetEntity="Lemma")
254          * @JoinTable(name="lemma_type",
255          *              joinColumns={@JoinColumn(name="type_id", referencedColumnName="type_id")},
256          *              inverseJoinColumns={@JoinColumn(name="lemma_id", referencedColumnName="lemma_id")}
257          * )
258          */
259         private $lemmas;
260
261         public function __construct(){
262                 $this->lemmas = new \Doctrine\Common\Collections\ArrayCollection();
263         }
264
265         /**
266          *
267          * @return int
268          */
269         public function getId(){
270                 return $this->id;
271         }
272
273         /**
274          *
275          * @param string $type
276          * @return void
277          */
278         public function setType($type){
279                 $this->type = $type;
280         }
281
282         /**
283          *
284          * @return string
285          */
286         public function getType(){
287                 return $this->type;
288         }
289
290         /**
291          *
292          * @param string $abbreviation
293          * @return void
294          */
295         public function setAbbreviation($abbreviation){
296                 $this->abbreviation = $abbreviation;
297         }
298
299         /**
300          *
301          * @return string
302          */
303         public function getAbbreviation(){
304                 return $this->abbreviation;
305         }
306
307         /**
308          *
309          * @param kateglo\application\models\Lemma $lemma
310          * @return void
311          */
312         public function addLemma(Lemma $lemma)
313         {
314                 if (!$this->lemmas->contains($lemma)) {
315                         $this->lemmas[] = $lemma;
316                         $lemma->addType($this);
317                 }
318         }
319
320         /**
321          *
322          * @param kateglo\application\models\Lemma $lemma
323          * @return void
324          */
325         public function removeLEmma(Lemma $lemma)
326         {
327                 $removed = $this->lemmas->removeElement($lemma);
328                 if ($removed !== null) {
329                         $removed->removeType($this);
330                 }
331         }
332
333         /**
334          *
335          * @return kateglo\application\helpers\collections\ArrayCollection
336          */
337         public function getCategories()
338         {
339                 return $this->categories;
340         }
341
342 }
343
344
345 /**
346  * @Entity
347  * @Table(name="phrase")
348  */
349 class Phrase {
350
351     const CLASS_NAME = __CLASS__;
352
353     /**
354      * @Id
355      * @Column(type="integer", name="phrase_id")
356      * @GeneratedValue(strategy="AUTO")
357      */
358     private $id;
359
360     /**
361      * @Column(type="string", name="phrase_name", unique=true, length=255)
362      */
363     private $phrase;
364
365     /**
366      * @ManyToOne(targetEntity="PhraseType")
367      * @JoinColumn(name="phrase_type_id", referencedColumnName="phrase_type_id")
368      */
369     private $type;
370
371     /**
372      * @OneToMany(targetEntity="Definition", mappedBy="phrase", cascade={"persist"})
373      */
374     private $definitions;
375
376     public function __construct() {
377         $this->definitions = new \Doctrine\Common\Collections\ArrayCollection;
378     }
379
380     /**
381      *
382      * @param Definition $definition
383      * @return void
384      */
385     public function addDefinition(Definition $definition){
386         $this->definitions[] = $definition;
387         $definition->setPhrase($this);
388     }
389
390     /**
391      * @return int
392      */
393     public function getId(){
394         return $this->id;
395     }
396
397     /**
398      * @param string $phrase
399      * @return void
400      */
401     public function setPhrase($phrase){
402         $this->phrase = $phrase;
403     }
404
405     /**
406      * @return string
407      */
408     public function getPhrase(){
409         return $this->phrase;
410     }
411
412     /**
413      *
414      * @param PhraseType $type
415      * @return void
416      */
417     public function setType(PhraseType $type){
418         $this->type = $type;
419     }
420
421     /**
422      *
423      * @return PhraseType
424      */
425     public function getType(){
426         return $this->type;
427     }
428
429     /**
430      *
431      * @return ArrayCollection
432      */
433     public function getDefinitions(){
434         return $this->definitions;
435     }
436 }
437
438 /**
439  * @Entity
440  * @Table(name="phrase_type")
441  */
442 class PhraseType {
443
444     const CLASS_NAME = __CLASS__;
445
446     /**
447      * @Id
448      * @Column(type="integer", name="phrase_type_id")
449      * @GeneratedValue(strategy="AUTO")
450      */
451     private $id;
452
453     /**
454      * @Column(type="string", name="phrase_type_name", unique=true)
455      */
456     private $type;
457
458     /**
459      * @Column(type="string", name="phrase_type_abbreviation", unique=true)
460      */
461     private $abbreviation;
462
463     /**
464      * @OneToMany(targetEntity="Phrase", mappedBy="type")
465      */
466     private $phrases;
467
468     public function __construct() {
469         $this->phrases = new \Doctrine\Common\Collections\ArrayCollection;
470     }
471
472     /**
473      * @return int
474      */
475     public function getId(){
476         return $this->id;
477     }
478
479     /**
480      * @param string $type
481      * @return void
482      */
483     public function setType($type){
484         $this->type = $type;
485     }
486
487     /**
488      * @return string
489      */
490     public function getType(){
491         return $this->type;
492     }
493
494     /**
495      * @param string $abbreviation
496      * @return void
497      */
498     public function setAbbreviation($abbreviation){
499         $this->abbreviation = $abbreviation;
500     }
501
502     /**
503      * @return string
504      */
505     public function getAbbreviation(){
506         return $this->abbreviation;
507     }
508
509     /**
510      * @param ArrayCollection $phrases
511      * @return void
512      */
513     public function setPhrases($phrases){
514         $this->phrases = $phrases;
515     }
516
517     /**
518      *
519      * @return ArrayCollection
520      */
521     public function getPhrases(){
522         return $this->phrases;
523     }
524
525 }
526
527 /**
528  * @Entity
529  * @Table(name="definition")
530  */
531 class Definition {
532
533     const CLASS_NAME = __CLASS__;
534
535     /**
536      * @Id
537      * @Column(type="integer", name="definition_id")
538      * @GeneratedValue(strategy="AUTO")
539      */
540     private $id;
541
542     /**
543      * @ManyToOne(targetEntity="Phrase")
544      * @JoinColumn(name="definition_phrase_id", referencedColumnName="phrase_id")
545      */
546     private $phrase;
547
548     /**
549      * @Column(type="text", name="definition_text")
550      */
551     private $definition;
552
553     /**
554      * @return int
555      */
556     public function getId(){
557         return $this->id;
558     }
559
560     /**
561      * @param Phrase $phrase
562      * @return void
563      */
564     public function setPhrase(Phrase $phrase){
565         $this->phrase = $phrase;
566     }
567
568     /**
569      * @return Phrase
570      */
571     public function getPhrase(){
572         return $this->phrase;
573     }
574
575     public function removePhrase() {
576         if ($this->phrase !== null) {
577             /*@var $phrase kateglo\application\models\Phrase */
578             $phrase = $this->phrase;
579             $this->phrase = null;
580             $phrase->removeDefinition($this);
581         }
582     }
583
584     /**
585      * @param string $definition
586      * @return void
587      */
588     public function setDefinition($definition){
589         $this->definition = $definition;
590     }
591
592     /**
593      * @return string
594      */
595     public function getDefinition(){
596         return $this->definition;
597     }
598 }