Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / OrderedJoinedTableInheritanceCollectionTest.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 Benjamin Eberlei <kontakt@beberlei.de>
13  */
14 class OrderedJoinedTableInheritanceCollectionTest 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\OJTIC_Pet'),
21                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\OJTIC_Cat'),
22                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\OJTIC_Dog'),
23             ));
24         } catch (\Exception $e) {
25             // Swallow all exceptions. We do not test the schema tool here.
26         }
27
28         $dog = new OJTIC_Dog();
29         $dog->name = "Poofy";
30
31         $dog1 = new OJTIC_Dog();
32         $dog1->name = "Zampa";
33         $dog2 = new OJTIC_Dog();
34         $dog2->name = "Aari";
35
36         $dog1->mother = $dog;
37         $dog2->mother = $dog;
38
39         $dog->children[] = $dog1;
40         $dog->children[] = $dog2;
41
42         $this->_em->persist($dog);
43         $this->_em->persist($dog1);
44         $this->_em->persist($dog2);
45         $this->_em->flush();
46         $this->_em->clear();
47     }
48
49     public function testOrderdOneToManyCollection()
50     {
51         $poofy = $this->_em->createQuery("SELECT p FROM Doctrine\Tests\ORM\Functional\OJTIC_Pet p WHERE p.name = 'Poofy'")->getSingleResult();
52
53         $this->assertEquals('Aari', $poofy->children[0]->getName());
54         $this->assertEquals('Zampa', $poofy->children[1]->getName());
55
56         $this->_em->clear();
57
58         $result = $this->_em->createQuery(
59             "SELECT p, c FROM Doctrine\Tests\ORM\Functional\OJTIC_Pet p JOIN p.children c WHERE p.name = 'Poofy'")
60                 ->getResult();
61
62         $this->assertEquals(1, count($result));
63         $poofy = $result[0];
64
65         $this->assertEquals('Aari', $poofy->children[0]->getName());
66         $this->assertEquals('Zampa', $poofy->children[1]->getName());
67     }
68 }
69
70 /**
71  * @Entity
72  * @InheritanceType("JOINED")
73  * @DiscriminatorColumn(name="discr", type="string")
74  * @DiscriminatorMap({
75  *      "cat" = "OJTIC_Cat",
76  *      "dog" = "OJTIC_Dog"})
77  */
78 abstract class OJTIC_Pet
79 {
80     /**
81      * @Id
82      * @column(type="integer")
83      * @generatedValue(strategy="AUTO")
84      */
85     public $id;
86
87     /**
88      *
89      * @Column
90      */
91     public $name;
92
93     /**
94      * @ManyToOne(targetEntity="OJTIC_PET")
95      */
96     public $mother;
97
98     /**
99      * @OneToMany(targetEntity="OJTIC_Pet", mappedBy="mother")
100      * @OrderBy({"name" = "ASC"})
101      */
102     public $children;
103
104     /**
105      * @ManyToMany(targetEntity="OJTIC_Pet")
106      * @JoinTable(name="OTJIC_Pet_Friends",
107      *     joinColumns={@JoinColumn(name="pet_id", referencedColumnName="id")},
108      *     inverseJoinColumns={@JoinColumn(name="friend_id", referencedColumnName="id")})
109      * @OrderBy({"name" = "ASC"})
110      */
111     public $friends;
112
113     public function getName()
114     {
115         return $this->name;
116     }
117 }
118
119 /**
120  * @Entity
121  */
122 class OJTIC_Cat extends OJTIC_Pet
123 {
124
125 }
126
127 /**
128  * @Entity
129  */
130 class OJTIC_Dog extends OJTIC_Pet
131 {
132
133 }