Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC881Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 require_once __DIR__ . '/../../../TestInit.php';
6
7 class DDC881Test extends \Doctrine\Tests\OrmFunctionalTestCase
8 {
9
10     protected function setUp()
11     {
12         parent::setUp();
13
14         try {
15             $this->_schemaTool->createSchema(array(
16                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881User'),
17                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881Phonenumber'),
18                 $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC881Phonecall'),
19             ));
20         } catch (\Exception $e) {
21
22         }
23     }
24
25     /**
26      * @group DDC-117
27      * @group DDC-881
28      */
29     public function testIssue()
30     {
31         /* Create two test users: albert and alfons */
32         $albert = new DDC881User;
33         $albert->setName("albert");
34         $this->_em->persist($albert);
35
36         $alfons = new DDC881User;
37         $alfons->setName("alfons");
38         $this->_em->persist($alfons);
39
40         $this->_em->flush();
41
42         /* Assign two phone numbers to each user */
43         $phoneAlbert1 = new DDC881PhoneNumber();
44         $phoneAlbert1->setUser($albert);
45         $phoneAlbert1->setId(1);
46         $phoneAlbert1->setPhoneNumber("albert home: 012345");
47         $this->_em->persist($phoneAlbert1);
48
49         $phoneAlbert2 = new DDC881PhoneNumber();
50         $phoneAlbert2->setUser($albert);
51         $phoneAlbert2->setId(2);
52         $phoneAlbert2->setPhoneNumber("albert mobile: 67890");
53         $this->_em->persist($phoneAlbert2);
54
55         $phoneAlfons1 = new DDC881PhoneNumber();
56         $phoneAlfons1->setId(1);
57         $phoneAlfons1->setUser($alfons);
58         $phoneAlfons1->setPhoneNumber("alfons home: 012345");
59         $this->_em->persist($phoneAlfons1);
60
61         $phoneAlfons2 = new DDC881PhoneNumber();
62         $phoneAlfons2->setId(2);
63         $phoneAlfons2->setUser($alfons);
64         $phoneAlfons2->setPhoneNumber("alfons mobile: 67890");
65         $this->_em->persist($phoneAlfons2);
66
67         /* We call alfons and albert once on their mobile numbers */
68         $call1 = new DDC881PhoneCall();
69         $call1->setPhoneNumber($phoneAlfons2);
70         $this->_em->persist($call1);
71
72         $call2 = new DDC881PhoneCall();
73         $call2->setPhoneNumber($phoneAlbert2);
74         $this->_em->persist($call2);
75
76         $this->_em->flush();
77         $this->_em->clear();
78
79         // fetch-join that foreign-key/primary-key entity association
80         $dql = "SELECT c, p FROM " . __NAMESPACE__ . "\DDC881PhoneCall c JOIN c.phonenumber p";
81         $calls = $this->_em->createQuery($dql)->getResult();
82
83         $this->assertEquals(2, count($calls));
84         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $calls[0]->getPhoneNumber());
85         $this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $calls[1]->getPhoneNumber());
86
87         $dql = "SELECT p, c FROM " . __NAMESPACE__ . "\DDC881PhoneNumber p JOIN p.calls c";
88         $numbers = $this->_em->createQuery($dql)->getResult();
89
90         $this->assertEquals(2, count($numbers));
91         $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $numbers[0]->getCalls());
92         $this->assertTrue($numbers[0]->getCalls()->isInitialized());
93     }
94
95 }
96
97 /**
98  * @Entity
99  */
100 class DDC881User
101 {
102
103     /**
104      * @Id
105      * @Column(type="integer")
106      * @GeneratedValue(strategy="AUTO")
107      */
108     private $id;
109     /**
110      * @Column(type="string")
111      */
112     private $name;
113     /**
114      * @OneToMany(targetEntity="DDC881PhoneNumber",mappedBy="id")
115      */
116     private $phoneNumbers;
117
118     public function getName()
119     {
120         return $this->name;
121     }
122
123     public function setName($name)
124     {
125         $this->name = $name;
126     }
127 }
128
129 /**
130  * @Entity
131  */
132 class DDC881PhoneNumber
133 {
134
135     /**
136      * @Id
137      * @Column(type="integer")
138      */
139     private $id;
140     /**
141      * @Id
142      * @ManyToOne(targetEntity="DDC881User",cascade={"all"})
143      */
144     private $user;
145     /**
146      * @Column(type="string")
147      */
148     private $phonenumber;
149
150     /**
151      * @OneToMany(targetEntity="DDC881PhoneCall", mappedBy="phonenumber")
152      */
153     private $calls;
154
155     public function __construct()
156     {
157         $this->calls = new \Doctrine\Common\Collections\ArrayCollection();
158     }
159
160     public function setId($id)
161     {
162         $this->id = $id;
163     }
164
165     public function setUser(DDC881User $user)
166     {
167         $this->user = $user;
168     }
169
170     public function setPhoneNumber($phoneNumber)
171     {
172         $this->phonenumber = $phoneNumber;
173     }
174
175     public function getCalls()
176     {
177         return $this->calls;
178     }
179 }
180
181 /**
182  * @Entity
183  */
184 class DDC881PhoneCall
185 {
186
187     /**
188      * @Id
189      * @Column(type="integer")
190      * @GeneratedValue(strategy="AUTO")
191      */
192     private $id;
193     /**
194      * @ManyToOne(targetEntity="DDC881PhoneNumber", inversedBy="calls", cascade={"all"})
195      * @JoinColumns({
196      *  @JoinColumn(name="phonenumber_id", referencedColumnName="id"),
197      *  @JoinColumn(name="user_id", referencedColumnName="user_id")
198      * })
199      */
200     private $phonenumber;
201     /**
202      * @Column(type="string",nullable=true)
203      */
204     private $callDate;
205
206     public function setPhoneNumber(DDC881PhoneNumber $phoneNumber)
207     {
208         $this->phonenumber = $phoneNumber;
209     }
210
211     public function getPhoneNumber()
212     {
213         return $this->phonenumber;
214     }
215 }