Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Ticket / DDC440Test.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Ticket;
4
5 require_once __DIR__ . '/../../../TestInit.php';
6
7 class DDC440Test extends \Doctrine\Tests\OrmFunctionalTestCase
8 {
9
10     protected function setUp()
11     {
12         parent::setUp();
13         try {
14             $this->_schemaTool->createSchema(array(
15                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\DDC440Phone'),
16                 $this->_em->getClassMetadata('Doctrine\Tests\ORM\Functional\Ticket\DDC440Client')
17             ));
18         } catch (\Exception $e) {
19             // Swallow all exceptions. We do not test the schema tool here.
20         }
21     }
22
23     /**
24      * @group DDC-440
25      */
26     public function testOriginalEntityDataEmptyWhenProxyLoadedFromTwoAssociations()
27     {
28
29
30         /* The key of the problem is that the first phone is fetched via two association, main_phone and phones.
31          *
32          * You will notice that the original_entity_datas are not loaded for the first phone. (They are for the second)
33          *
34          * In the Client entity definition, if you define the main_phone relation after the phones relation, both assertions pass.
35          * (for the sake or this test, I defined the main_phone relation before the phones relation)
36          *
37          */
38
39         //Initialize some data
40         $client = new DDC440Client;
41         $client->setName('Client1');
42
43         $phone = new DDC440Phone;
44         $phone->setNumber('418 111-1111');
45         $phone->setClient($client);
46
47         $phone2 = new DDC440Phone;
48         $phone2->setNumber('418 222-2222');
49         $phone2->setClient($client);
50
51         $client->setMainPhone($phone);
52
53         $this->_em->persist($client);
54         $this->_em->flush();
55         $id = $client->getId();
56         $this->_em->clear();
57
58         $uw = $this->_em->getUnitOfWork();
59         $client = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC440Client', $id);
60         $clientPhones = $client->getPhones();
61         $p1 = $clientPhones[0];
62         $p2 = $clientPhones[1];
63
64         // Test the first phone.  The assertion actually failed because original entity data is not set properly.
65         // This was because it is also set as MainPhone and that one is created as a proxy, not the
66         // original object when the find on Client is called. However loading proxies did not work correctly.
67         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC440Phone', $p1);
68         $originalData = $uw->getOriginalEntityData($p1);
69         $this->assertEquals($phone->getNumber(), $originalData['number']);
70
71
72         //If you comment out previous test, this one should pass
73         $this->assertInstanceOf('Doctrine\Tests\ORM\Functional\Ticket\DDC440Phone', $p2);
74         $originalData = $uw->getOriginalEntityData($p2);
75         $this->assertEquals($phone2->getNumber(), $originalData['number']);
76     }
77
78 }
79
80 /**
81  * @Entity
82  * @Table(name="phone")
83  */
84 class DDC440Phone
85 {
86
87     /**
88      * @Column(name="id", type="integer")
89      * @Id
90      * @GeneratedValue(strategy="AUTO")
91      */
92     protected $id;
93     /**
94      * @ManyToOne(targetEntity="DDC440Client",inversedBy="phones")
95      * @JoinColumns({
96      *   @JoinColumn(name="client_id", referencedColumnName="id")
97      * })
98      */
99     protected $client;
100     /**
101      * @Column(name="phonenumber", type="string")
102      */
103     protected $number;
104
105     public function setNumber($value)
106     {
107         $this->number = $value;
108     }
109
110     public function getNumber()
111     {
112         return $this->number;
113     }
114
115     public function setClient(DDC440Client $value, $update_inverse=true)
116     {
117         $this->client = $value;
118         if ($update_inverse) {
119             $value->addPhone($this);
120         }
121     }
122
123     public function getClient()
124     {
125         return $this->client;
126     }
127
128     public function getId()
129     {
130         return $this->id;
131     }
132
133     public function setId($value)
134     {
135         $this->id = $value;
136     }
137
138 }
139
140 /**
141  * @Entity
142  * @Table(name="client")
143  */
144 class DDC440Client
145 {
146
147     /**
148      * @Column(name="id", type="integer")
149      * @Id
150      * @GeneratedValue(strategy="AUTO")
151      */
152     protected $id;
153     /**
154      * @OneToOne(targetEntity="DDC440Phone", fetch="EAGER")
155      * @JoinColumns({
156      *   @JoinColumn(name="main_phone_id", referencedColumnName="id",onDelete="SET NULL")
157      * })
158      */
159     protected $main_phone;
160     /**
161      * @OneToMany(targetEntity="DDC440Phone", mappedBy="client", cascade={"persist", "remove"}, fetch="EAGER")
162      * @orderBy({"number"="ASC"})
163      */
164     protected $phones;
165     /**
166      * @Column(name="name", type="string")
167      */
168     protected $name;
169
170     public function __construct()
171     {
172
173     }
174
175     public function setName($value)
176     {
177         $this->name = $value;
178     }
179
180     public function getName()
181     {
182         return $this->name;
183     }
184
185     public function addPhone(DDC440Phone $value)
186     {
187         $this->phones[] = $value;
188         $value->setClient($this, false);
189     }
190
191     public function getPhones()
192     {
193         return $this->phones;
194     }
195
196     public function setMainPhone(DDC440Phone $value)
197     {
198         $this->main_phone = $value;
199     }
200
201     public function getMainPhone()
202     {
203         return $this->main_phone;
204     }
205
206     public function getId()
207     {
208         return $this->id;
209     }
210
211     public function setId($value)
212     {
213         $this->id = $value;
214     }
215
216 }