1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Logging\DebugStack; |
6
|
|
|
use Doctrine\ORM\EntityNotFoundException; |
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
8
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
9
|
|
|
use Doctrine\ORM\Query; |
10
|
|
|
use Doctrine\ORM\UnitOfWork; |
11
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
12
|
|
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber; |
13
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress; |
14
|
|
|
use Doctrine\Tests\Models\CMS\CmsArticle; |
15
|
|
|
use Doctrine\Tests\Models\CMS\CmsComment; |
16
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
17
|
|
|
use Doctrine\Tests\Models\CMS\CmsEmail; |
18
|
|
|
|
19
|
|
|
class BasicFunctionalTest extends OrmFunctionalTestCase |
20
|
|
|
{ |
21
|
|
|
protected function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->useModelSet('cms'); |
24
|
|
|
parent::setUp(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testBasicUnitsOfWorkWithOneToManyAssociation() |
28
|
|
|
{ |
29
|
|
|
// Create |
30
|
|
|
$user = new CmsUser; |
31
|
|
|
$user->name = 'Roman'; |
32
|
|
|
$user->username = 'romanb'; |
33
|
|
|
$user->status = 'developer'; |
34
|
|
|
$this->_em->persist($user); |
35
|
|
|
|
36
|
|
|
$this->_em->flush(); |
37
|
|
|
|
38
|
|
|
$this->assertTrue(is_numeric($user->id)); |
39
|
|
|
$this->assertTrue($this->_em->contains($user)); |
40
|
|
|
|
41
|
|
|
// Read |
42
|
|
|
$user2 = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id); |
43
|
|
|
$this->assertTrue($user === $user2); |
44
|
|
|
|
45
|
|
|
// Add a phonenumber |
46
|
|
|
$ph = new CmsPhonenumber; |
47
|
|
|
$ph->phonenumber = "12345"; |
48
|
|
|
$user->addPhonenumber($ph); |
49
|
|
|
$this->_em->flush(); |
50
|
|
|
$this->assertTrue($this->_em->contains($ph)); |
51
|
|
|
$this->assertTrue($this->_em->contains($user)); |
52
|
|
|
//$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->phonenumbers); |
53
|
|
|
|
54
|
|
|
// Update name |
55
|
|
|
$user->name = 'guilherme'; |
56
|
|
|
$this->_em->flush(); |
57
|
|
|
$this->assertEquals('guilherme', $user->name); |
58
|
|
|
|
59
|
|
|
// Add another phonenumber |
60
|
|
|
$ph2 = new CmsPhonenumber; |
61
|
|
|
$ph2->phonenumber = "6789"; |
62
|
|
|
$user->addPhonenumber($ph2); |
63
|
|
|
$this->_em->flush(); |
64
|
|
|
$this->assertTrue($this->_em->contains($ph2)); |
65
|
|
|
|
66
|
|
|
// Delete |
67
|
|
|
$this->_em->remove($user); |
68
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($user)); |
69
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph)); |
70
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph2)); |
71
|
|
|
$this->_em->flush(); |
72
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($user)); |
73
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph)); |
74
|
|
|
$this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph2)); |
75
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user)); |
76
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph)); |
77
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph2)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testOneToManyAssociationModification() |
81
|
|
|
{ |
82
|
|
|
$user = new CmsUser; |
83
|
|
|
$user->name = 'Roman'; |
84
|
|
|
$user->username = 'romanb'; |
85
|
|
|
$user->status = 'developer'; |
86
|
|
|
|
87
|
|
|
$ph1 = new CmsPhonenumber; |
88
|
|
|
$ph1->phonenumber = "0301234"; |
89
|
|
|
$ph2 = new CmsPhonenumber; |
90
|
|
|
$ph2->phonenumber = "987654321"; |
91
|
|
|
|
92
|
|
|
$user->addPhonenumber($ph1); |
93
|
|
|
$user->addPhonenumber($ph2); |
94
|
|
|
|
95
|
|
|
$this->_em->persist($user); |
96
|
|
|
$this->_em->flush(); |
97
|
|
|
|
98
|
|
|
//$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->phonenumbers); |
99
|
|
|
|
100
|
|
|
// Remove the first element from the collection |
101
|
|
|
unset($user->phonenumbers[0]); |
102
|
|
|
$ph1->user = null; // owning side! |
103
|
|
|
|
104
|
|
|
$this->_em->flush(); |
105
|
|
|
|
106
|
|
|
$this->assertEquals(1, count($user->phonenumbers)); |
107
|
|
|
$this->assertNull($ph1->user); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testBasicOneToOne() |
111
|
|
|
{ |
112
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
113
|
|
|
$user = new CmsUser; |
114
|
|
|
$user->name = 'Roman'; |
115
|
|
|
$user->username = 'romanb'; |
116
|
|
|
$user->status = 'developer'; |
117
|
|
|
|
118
|
|
|
$address = new CmsAddress; |
119
|
|
|
$address->country = 'Germany'; |
120
|
|
|
$address->city = 'Berlin'; |
121
|
|
|
$address->zip = '12345'; |
122
|
|
|
|
123
|
|
|
$user->address = $address; // inverse side |
124
|
|
|
$address->user = $user; // owning side! |
125
|
|
|
|
126
|
|
|
$this->_em->persist($user); |
127
|
|
|
$this->_em->flush(); |
128
|
|
|
|
129
|
|
|
// Check that the foreign key has been set |
130
|
|
|
$userId = $this->_em->getConnection()->executeQuery( |
131
|
|
|
"SELECT user_id FROM cms_addresses WHERE id=?", array($address->id) |
132
|
|
|
)->fetchColumn(); |
133
|
|
|
$this->assertTrue(is_numeric($userId)); |
134
|
|
|
|
135
|
|
|
$this->_em->clear(); |
136
|
|
|
|
137
|
|
|
$user2 = $this->_em->createQuery('select u from \Doctrine\Tests\Models\CMS\CmsUser u where u.id=?1') |
138
|
|
|
->setParameter(1, $userId) |
139
|
|
|
->getSingleResult(); |
140
|
|
|
|
141
|
|
|
// Address has been eager-loaded because it cant be lazy |
142
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $user2->address); |
143
|
|
|
$this->assertNotInstanceOf('Doctrine\ORM\Proxy\Proxy', $user2->address); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @group DDC-1230 |
148
|
|
|
*/ |
149
|
|
|
public function testRemove() |
150
|
|
|
{ |
151
|
|
|
$user = new CmsUser; |
152
|
|
|
$user->name = 'Guilherme'; |
153
|
|
|
$user->username = 'gblanco'; |
154
|
|
|
$user->status = 'developer'; |
155
|
|
|
|
156
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW"); |
157
|
|
|
|
158
|
|
|
$this->_em->persist($user); |
159
|
|
|
|
160
|
|
|
$this->assertEquals(UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_MANAGED"); |
161
|
|
|
|
162
|
|
|
$this->_em->remove($user); |
163
|
|
|
|
164
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW"); |
165
|
|
|
|
166
|
|
|
$this->_em->persist($user); |
167
|
|
|
$this->_em->flush(); |
168
|
|
|
$id = $user->getId(); |
169
|
|
|
|
170
|
|
|
$this->_em->remove($user); |
171
|
|
|
|
172
|
|
|
$this->assertEquals(UnitOfWork::STATE_REMOVED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_REMOVED"); |
173
|
|
|
$this->_em->flush(); |
174
|
|
|
|
175
|
|
|
$this->assertEquals(UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW"); |
176
|
|
|
|
177
|
|
|
$this->assertNull($this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $id)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function testOneToManyOrphanRemoval() |
181
|
|
|
{ |
182
|
|
|
$user = new CmsUser; |
183
|
|
|
$user->name = 'Guilherme'; |
184
|
|
|
$user->username = 'gblanco'; |
185
|
|
|
$user->status = 'developer'; |
186
|
|
|
|
187
|
|
|
for ($i=0; $i<3; ++$i) { |
188
|
|
|
$phone = new CmsPhonenumber; |
189
|
|
|
$phone->phonenumber = 100 + $i; |
190
|
|
|
$user->addPhonenumber($phone); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$this->_em->persist($user); |
194
|
|
|
|
195
|
|
|
$this->_em->flush(); |
196
|
|
|
|
197
|
|
|
$user->getPhonenumbers()->remove(0); |
198
|
|
|
$this->assertEquals(2, count($user->getPhonenumbers())); |
199
|
|
|
|
200
|
|
|
$this->_em->flush(); |
201
|
|
|
|
202
|
|
|
// Check that there are just 2 phonenumbers left |
203
|
|
|
$count = $this->_em->getConnection()->fetchColumn("SELECT COUNT(*) FROM cms_phonenumbers"); |
204
|
|
|
$this->assertEquals(2, $count); // only 2 remaining |
205
|
|
|
|
206
|
|
|
// check that clear() removes the others via orphan removal |
207
|
|
|
$user->getPhonenumbers()->clear(); |
208
|
|
|
$this->_em->flush(); |
209
|
|
|
$this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_phonenumbers")); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function testBasicQuery() |
213
|
|
|
{ |
214
|
|
|
$user = new CmsUser; |
215
|
|
|
$user->name = 'Guilherme'; |
216
|
|
|
$user->username = 'gblanco'; |
217
|
|
|
$user->status = 'developer'; |
218
|
|
|
$this->_em->persist($user); |
219
|
|
|
$this->_em->flush(); |
220
|
|
|
|
221
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u"); |
222
|
|
|
|
223
|
|
|
$users = $query->getResult(); |
224
|
|
|
|
225
|
|
|
$this->assertEquals(1, count($users)); |
226
|
|
|
$this->assertEquals('Guilherme', $users[0]->name); |
227
|
|
|
$this->assertEquals('gblanco', $users[0]->username); |
228
|
|
|
$this->assertEquals('developer', $users[0]->status); |
229
|
|
|
//$this->assertNull($users[0]->phonenumbers); |
230
|
|
|
//$this->assertNull($users[0]->articles); |
231
|
|
|
|
232
|
|
|
$usersArray = $query->getArrayResult(); |
233
|
|
|
|
234
|
|
|
$this->assertTrue(is_array($usersArray)); |
235
|
|
|
$this->assertEquals(1, count($usersArray)); |
236
|
|
|
$this->assertEquals('Guilherme', $usersArray[0]['name']); |
237
|
|
|
$this->assertEquals('gblanco', $usersArray[0]['username']); |
238
|
|
|
$this->assertEquals('developer', $usersArray[0]['status']); |
239
|
|
|
|
240
|
|
|
$usersScalar = $query->getScalarResult(); |
241
|
|
|
|
242
|
|
|
$this->assertTrue(is_array($usersScalar)); |
243
|
|
|
$this->assertEquals(1, count($usersScalar)); |
244
|
|
|
$this->assertEquals('Guilherme', $usersScalar[0]['u_name']); |
245
|
|
|
$this->assertEquals('gblanco', $usersScalar[0]['u_username']); |
246
|
|
|
$this->assertEquals('developer', $usersScalar[0]['u_status']); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function testBasicOneToManyInnerJoin() |
250
|
|
|
{ |
251
|
|
|
$user = new CmsUser; |
252
|
|
|
$user->name = 'Guilherme'; |
253
|
|
|
$user->username = 'gblanco'; |
254
|
|
|
$user->status = 'developer'; |
255
|
|
|
$this->_em->persist($user); |
256
|
|
|
$this->_em->flush(); |
257
|
|
|
|
258
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p"); |
259
|
|
|
|
260
|
|
|
$users = $query->getResult(); |
261
|
|
|
|
262
|
|
|
$this->assertEquals(0, count($users)); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function testBasicOneToManyLeftJoin() |
266
|
|
|
{ |
267
|
|
|
$user = new CmsUser; |
268
|
|
|
$user->name = 'Guilherme'; |
269
|
|
|
$user->username = 'gblanco'; |
270
|
|
|
$user->status = 'developer'; |
271
|
|
|
$this->_em->persist($user); |
272
|
|
|
$this->_em->flush(); |
273
|
|
|
|
274
|
|
|
$query = $this->_em->createQuery("select u,p from Doctrine\Tests\Models\CMS\CmsUser u left join u.phonenumbers p"); |
275
|
|
|
|
276
|
|
|
$users = $query->getResult(); |
277
|
|
|
|
278
|
|
|
$this->assertEquals(1, count($users)); |
279
|
|
|
$this->assertEquals('Guilherme', $users[0]->name); |
280
|
|
|
$this->assertEquals('gblanco', $users[0]->username); |
281
|
|
|
$this->assertEquals('developer', $users[0]->status); |
282
|
|
|
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $users[0]->phonenumbers); |
283
|
|
|
$this->assertTrue($users[0]->phonenumbers->isInitialized()); |
284
|
|
|
$this->assertEquals(0, $users[0]->phonenumbers->count()); |
285
|
|
|
//$this->assertNull($users[0]->articles); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public function testBasicRefresh() |
289
|
|
|
{ |
290
|
|
|
$user = new CmsUser; |
291
|
|
|
$user->name = 'Guilherme'; |
292
|
|
|
$user->username = 'gblanco'; |
293
|
|
|
$user->status = 'developer'; |
294
|
|
|
|
295
|
|
|
$this->_em->persist($user); |
296
|
|
|
$this->_em->flush(); |
297
|
|
|
|
298
|
|
|
$user->status = 'mascot'; |
299
|
|
|
|
300
|
|
|
$this->assertEquals('mascot', $user->status); |
301
|
|
|
$this->_em->refresh($user); |
302
|
|
|
$this->assertEquals('developer', $user->status); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @group DDC-833 |
307
|
|
|
*/ |
308
|
|
|
public function testRefreshResetsCollection() |
309
|
|
|
{ |
310
|
|
|
$user = new CmsUser; |
311
|
|
|
$user->name = 'Guilherme'; |
312
|
|
|
$user->username = 'gblanco'; |
313
|
|
|
$user->status = 'developer'; |
314
|
|
|
|
315
|
|
|
// Add a phonenumber |
316
|
|
|
$ph1 = new CmsPhonenumber; |
317
|
|
|
$ph1->phonenumber = "12345"; |
318
|
|
|
$user->addPhonenumber($ph1); |
319
|
|
|
|
320
|
|
|
// Add a phonenumber |
321
|
|
|
$ph2 = new CmsPhonenumber; |
322
|
|
|
$ph2->phonenumber = "54321"; |
323
|
|
|
|
324
|
|
|
$this->_em->persist($user); |
325
|
|
|
$this->_em->persist($ph1); |
326
|
|
|
$this->_em->persist($ph2); |
327
|
|
|
$this->_em->flush(); |
328
|
|
|
|
329
|
|
|
$user->addPhonenumber($ph2); |
330
|
|
|
|
331
|
|
|
$this->assertEquals(2, count($user->phonenumbers)); |
332
|
|
|
$this->_em->refresh($user); |
333
|
|
|
|
334
|
|
|
$this->assertEquals(1, count($user->phonenumbers)); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @group DDC-833 |
339
|
|
|
*/ |
340
|
|
|
public function testDqlRefreshResetsCollection() |
341
|
|
|
{ |
342
|
|
|
$user = new CmsUser; |
343
|
|
|
$user->name = 'Guilherme'; |
344
|
|
|
$user->username = 'gblanco'; |
345
|
|
|
$user->status = 'developer'; |
346
|
|
|
|
347
|
|
|
// Add a phonenumber |
348
|
|
|
$ph1 = new CmsPhonenumber; |
349
|
|
|
$ph1->phonenumber = "12345"; |
350
|
|
|
$user->addPhonenumber($ph1); |
351
|
|
|
|
352
|
|
|
// Add a phonenumber |
353
|
|
|
$ph2 = new CmsPhonenumber; |
354
|
|
|
$ph2->phonenumber = "54321"; |
355
|
|
|
|
356
|
|
|
$this->_em->persist($user); |
357
|
|
|
$this->_em->persist($ph1); |
358
|
|
|
$this->_em->persist($ph2); |
359
|
|
|
$this->_em->flush(); |
360
|
|
|
|
361
|
|
|
$user->addPhonenumber($ph2); |
362
|
|
|
|
363
|
|
|
$this->assertEquals(2, count($user->phonenumbers)); |
364
|
|
|
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1"; |
365
|
|
|
$user = $this->_em->createQuery($dql) |
366
|
|
|
->setParameter(1, $user->id) |
367
|
|
|
->setHint(Query::HINT_REFRESH, true) |
368
|
|
|
->getSingleResult(); |
369
|
|
|
|
370
|
|
|
$this->assertEquals(1, count($user->phonenumbers)); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @group DDC-833 |
375
|
|
|
*/ |
376
|
|
|
public function testCreateEntityOfProxy() |
377
|
|
|
{ |
378
|
|
|
$user = new CmsUser; |
379
|
|
|
$user->name = 'Guilherme'; |
380
|
|
|
$user->username = 'gblanco'; |
381
|
|
|
$user->status = 'developer'; |
382
|
|
|
|
383
|
|
|
// Add a phonenumber |
384
|
|
|
$ph1 = new CmsPhonenumber; |
385
|
|
|
$ph1->phonenumber = "12345"; |
386
|
|
|
$user->addPhonenumber($ph1); |
387
|
|
|
|
388
|
|
|
// Add a phonenumber |
389
|
|
|
$ph2 = new CmsPhonenumber; |
390
|
|
|
$ph2->phonenumber = "54321"; |
391
|
|
|
|
392
|
|
|
$this->_em->persist($user); |
393
|
|
|
$this->_em->persist($ph1); |
394
|
|
|
$this->_em->persist($ph2); |
395
|
|
|
$this->_em->flush(); |
396
|
|
|
$this->_em->clear(); |
397
|
|
|
|
398
|
|
|
$userId = $user->id; |
399
|
|
|
$user = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->id); |
400
|
|
|
|
401
|
|
|
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1"; |
402
|
|
|
$user = $this->_em->createQuery($dql) |
403
|
|
|
->setParameter(1, $userId) |
404
|
|
|
->getSingleResult(); |
405
|
|
|
|
406
|
|
|
$this->assertEquals(1, count($user->phonenumbers)); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
public function testAddToCollectionDoesNotInitialize() |
410
|
|
|
{ |
411
|
|
|
$user = new CmsUser; |
412
|
|
|
$user->name = 'Guilherme'; |
413
|
|
|
$user->username = 'gblanco'; |
414
|
|
|
$user->status = 'developer'; |
415
|
|
|
|
416
|
|
|
for ($i=0; $i<3; ++$i) { |
417
|
|
|
$phone = new CmsPhonenumber; |
418
|
|
|
$phone->phonenumber = 100 + $i; |
419
|
|
|
$user->addPhonenumber($phone); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
$this->_em->persist($user); |
423
|
|
|
$this->_em->flush(); |
424
|
|
|
$this->_em->clear(); |
425
|
|
|
|
426
|
|
|
$this->assertEquals(3, $user->getPhonenumbers()->count()); |
427
|
|
|
|
428
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'"); |
429
|
|
|
|
430
|
|
|
$gblanco = $query->getSingleResult(); |
431
|
|
|
|
432
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized()); |
433
|
|
|
|
434
|
|
|
$newPhone = new CmsPhonenumber; |
435
|
|
|
$newPhone->phonenumber = 555; |
436
|
|
|
$gblanco->addPhonenumber($newPhone); |
437
|
|
|
|
438
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized()); |
439
|
|
|
$this->_em->persist($gblanco); |
440
|
|
|
|
441
|
|
|
$this->_em->flush(); |
442
|
|
|
$this->_em->clear(); |
443
|
|
|
|
444
|
|
|
$query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'"); |
445
|
|
|
$gblanco2 = $query->getSingleResult(); |
446
|
|
|
$this->assertEquals(4, $gblanco2->getPhonenumbers()->count()); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
public function testInitializeCollectionWithNewObjectsRetainsNewObjects() |
450
|
|
|
{ |
451
|
|
|
$user = new CmsUser; |
452
|
|
|
$user->name = 'Guilherme'; |
453
|
|
|
$user->username = 'gblanco'; |
454
|
|
|
$user->status = 'developer'; |
455
|
|
|
|
456
|
|
|
for ($i=0; $i<3; ++$i) { |
457
|
|
|
$phone = new CmsPhonenumber; |
458
|
|
|
$phone->phonenumber = 100 + $i; |
459
|
|
|
$user->addPhonenumber($phone); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
$this->_em->persist($user); |
463
|
|
|
$this->_em->flush(); |
464
|
|
|
$this->_em->clear(); |
465
|
|
|
|
466
|
|
|
$this->assertEquals(3, $user->getPhonenumbers()->count()); |
467
|
|
|
|
468
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'"); |
469
|
|
|
|
470
|
|
|
$gblanco = $query->getSingleResult(); |
471
|
|
|
|
472
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized()); |
473
|
|
|
|
474
|
|
|
$newPhone = new CmsPhonenumber; |
475
|
|
|
$newPhone->phonenumber = 555; |
476
|
|
|
$gblanco->addPhonenumber($newPhone); |
477
|
|
|
|
478
|
|
|
$this->assertFalse($gblanco->getPhonenumbers()->isInitialized()); |
479
|
|
|
$this->assertEquals(4, $gblanco->getPhonenumbers()->count()); |
480
|
|
|
$this->assertTrue($gblanco->getPhonenumbers()->isInitialized()); |
481
|
|
|
|
482
|
|
|
$this->_em->flush(); |
483
|
|
|
$this->_em->clear(); |
484
|
|
|
|
485
|
|
|
$query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'"); |
486
|
|
|
$gblanco2 = $query->getSingleResult(); |
487
|
|
|
$this->assertEquals(4, $gblanco2->getPhonenumbers()->count()); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function testSetSetAssociationWithGetReference() |
491
|
|
|
{ |
492
|
|
|
$user = new CmsUser; |
493
|
|
|
$user->name = 'Guilherme'; |
494
|
|
|
$user->username = 'gblanco'; |
495
|
|
|
$user->status = 'developer'; |
496
|
|
|
$this->_em->persist($user); |
497
|
|
|
|
498
|
|
|
$address = new CmsAddress; |
499
|
|
|
$address->country = 'Germany'; |
500
|
|
|
$address->city = 'Berlin'; |
501
|
|
|
$address->zip = '12345'; |
502
|
|
|
$this->_em->persist($address); |
503
|
|
|
|
504
|
|
|
$this->_em->flush(); |
505
|
|
|
$this->_em->detach($address); |
506
|
|
|
|
507
|
|
|
$this->assertFalse($this->_em->contains($address)); |
508
|
|
|
$this->assertTrue($this->_em->contains($user)); |
509
|
|
|
|
510
|
|
|
// Assume we only got the identifier of the address and now want to attach |
511
|
|
|
// that address to the user without actually loading it, using getReference(). |
512
|
|
|
$addressRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsAddress', $address->getId()); |
513
|
|
|
|
514
|
|
|
//$addressRef->getId(); |
515
|
|
|
//\Doctrine\Common\Util\Debug::dump($addressRef); |
516
|
|
|
|
517
|
|
|
$user->setAddress($addressRef); // Ugh! Initializes address 'cause of $address->setUser($user)! |
518
|
|
|
|
519
|
|
|
$this->_em->flush(); |
520
|
|
|
$this->_em->clear(); |
521
|
|
|
|
522
|
|
|
// Check with a fresh load that the association is indeed there |
523
|
|
|
$query = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u join u.address a where u.username='gblanco'"); |
524
|
|
|
$gblanco = $query->getSingleResult(); |
525
|
|
|
|
526
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $gblanco); |
527
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $gblanco->getAddress()); |
528
|
|
|
$this->assertEquals('Berlin', $gblanco->getAddress()->getCity()); |
529
|
|
|
|
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
public function testOneToManyCascadeRemove() |
533
|
|
|
{ |
534
|
|
|
$user = new CmsUser; |
535
|
|
|
$user->name = 'Guilherme'; |
536
|
|
|
$user->username = 'gblanco'; |
537
|
|
|
$user->status = 'developer'; |
538
|
|
|
|
539
|
|
|
for ($i=0; $i<3; ++$i) { |
540
|
|
|
$phone = new CmsPhonenumber; |
541
|
|
|
$phone->phonenumber = 100 + $i; |
542
|
|
|
$user->addPhonenumber($phone); |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
$this->_em->persist($user); |
546
|
|
|
$this->_em->flush(); |
547
|
|
|
$this->_em->clear(); |
548
|
|
|
|
549
|
|
|
$query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'"); |
550
|
|
|
$gblanco = $query->getSingleResult(); |
551
|
|
|
|
552
|
|
|
$this->_em->remove($gblanco); |
553
|
|
|
$this->_em->flush(); |
554
|
|
|
|
555
|
|
|
$this->_em->clear(); |
556
|
|
|
|
557
|
|
|
$this->assertEquals(0, $this->_em->createQuery( |
558
|
|
|
"select count(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p") |
559
|
|
|
->getSingleScalarResult()); |
560
|
|
|
|
561
|
|
|
$this->assertEquals(0, $this->_em->createQuery( |
562
|
|
|
"select count(u.id) from Doctrine\Tests\Models\CMS\CmsUser u") |
563
|
|
|
->getSingleScalarResult()); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
public function testTextColumnSaveAndRetrieve() |
567
|
|
|
{ |
568
|
|
|
$user = new CmsUser; |
569
|
|
|
$user->name = 'Guilherme'; |
570
|
|
|
$user->username = 'gblanco'; |
571
|
|
|
$user->status = 'developer'; |
572
|
|
|
|
573
|
|
|
$this->_em->persist($user); |
574
|
|
|
|
575
|
|
|
$article = new CmsArticle(); |
576
|
|
|
$article->text = "Lorem ipsum dolor sunt."; |
577
|
|
|
$article->topic = "A Test Article!"; |
578
|
|
|
$article->setAuthor($user); |
579
|
|
|
|
580
|
|
|
$this->_em->persist($article); |
581
|
|
|
$this->_em->flush(); |
582
|
|
|
$articleId = $article->id; |
583
|
|
|
|
584
|
|
|
$this->_em->clear(); |
585
|
|
|
|
586
|
|
|
// test find() with leading backslash at the same time |
587
|
|
|
$articleNew = $this->_em->find('\Doctrine\Tests\Models\CMS\CmsArticle', $articleId); |
588
|
|
|
$this->assertTrue($this->_em->contains($articleNew)); |
589
|
|
|
$this->assertEquals("Lorem ipsum dolor sunt.", $articleNew->text); |
590
|
|
|
|
591
|
|
|
$this->assertNotSame($article, $articleNew); |
592
|
|
|
|
593
|
|
|
$articleNew->text = "Lorem ipsum dolor sunt. And stuff!"; |
594
|
|
|
|
595
|
|
|
$this->_em->flush(); |
596
|
|
|
$this->_em->clear(); |
597
|
|
|
|
598
|
|
|
$articleNew = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $articleId); |
599
|
|
|
$this->assertEquals("Lorem ipsum dolor sunt. And stuff!", $articleNew->text); |
600
|
|
|
$this->assertTrue($this->_em->contains($articleNew)); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
public function testFlushDoesNotIssueUnnecessaryUpdates() |
604
|
|
|
{ |
605
|
|
|
$user = new CmsUser; |
606
|
|
|
$user->name = 'Guilherme'; |
607
|
|
|
$user->username = 'gblanco'; |
608
|
|
|
$user->status = 'developer'; |
609
|
|
|
|
610
|
|
|
$address = new CmsAddress; |
611
|
|
|
$address->country = 'Germany'; |
612
|
|
|
$address->city = 'Berlin'; |
613
|
|
|
$address->zip = '12345'; |
614
|
|
|
|
615
|
|
|
$address->user = $user; |
616
|
|
|
$user->address = $address; |
617
|
|
|
|
618
|
|
|
$article = new CmsArticle(); |
619
|
|
|
$article->text = "Lorem ipsum dolor sunt."; |
620
|
|
|
$article->topic = "A Test Article!"; |
621
|
|
|
$article->setAuthor($user); |
622
|
|
|
|
623
|
|
|
$this->_em->persist($article); |
624
|
|
|
$this->_em->persist($user); |
625
|
|
|
|
626
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
627
|
|
|
|
628
|
|
|
$this->_em->flush(); |
629
|
|
|
$this->_em->clear(); |
630
|
|
|
|
631
|
|
|
$query = $this->_em->createQuery('select u,a,ad from Doctrine\Tests\Models\CMS\CmsUser u join u.articles a join u.address ad'); |
632
|
|
|
$user2 = $query->getSingleResult(); |
633
|
|
|
|
634
|
|
|
$this->assertEquals(1, count($user2->articles)); |
635
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $user2->address); |
636
|
|
|
|
637
|
|
|
$oldLogger = $this->_em->getConnection()->getConfiguration()->getSQLLogger(); |
638
|
|
|
$debugStack = new DebugStack(); |
639
|
|
|
$this->_em->getConnection()->getConfiguration()->setSQLLogger($debugStack); |
640
|
|
|
|
641
|
|
|
$this->_em->flush(); |
642
|
|
|
$this->assertEquals(0, count($debugStack->queries)); |
643
|
|
|
|
644
|
|
|
$this->_em->getConnection()->getConfiguration()->setSQLLogger($oldLogger); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
public function testRemoveEntityByReference() |
648
|
|
|
{ |
649
|
|
|
$user = new CmsUser; |
650
|
|
|
$user->name = 'Guilherme'; |
651
|
|
|
$user->username = 'gblanco'; |
652
|
|
|
$user->status = 'developer'; |
653
|
|
|
|
654
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
655
|
|
|
|
656
|
|
|
$this->_em->persist($user); |
657
|
|
|
$this->_em->flush(); |
658
|
|
|
$this->_em->clear(); |
659
|
|
|
|
660
|
|
|
$userRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->getId()); |
661
|
|
|
$this->_em->remove($userRef); |
662
|
|
|
$this->_em->flush(); |
663
|
|
|
$this->_em->clear(); |
664
|
|
|
|
665
|
|
|
$this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users")); |
666
|
|
|
|
667
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(null); |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
public function testQueryEntityByReference() |
671
|
|
|
{ |
672
|
|
|
$user = new CmsUser; |
673
|
|
|
$user->name = 'Guilherme'; |
674
|
|
|
$user->username = 'gblanco'; |
675
|
|
|
$user->status = 'developer'; |
676
|
|
|
|
677
|
|
|
$address = new CmsAddress; |
678
|
|
|
$address->country = 'Germany'; |
679
|
|
|
$address->city = 'Berlin'; |
680
|
|
|
$address->zip = '12345'; |
681
|
|
|
|
682
|
|
|
$user->setAddress($address); |
683
|
|
|
|
684
|
|
|
$this->_em->transactional(function($em) use($user) { |
685
|
|
|
$em->persist($user); |
686
|
|
|
}); |
687
|
|
|
$this->_em->clear(); |
688
|
|
|
|
689
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
690
|
|
|
|
691
|
|
|
$userRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->getId()); |
692
|
|
|
$address2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsAddress a where a.user = :user') |
693
|
|
|
->setParameter('user', $userRef) |
694
|
|
|
->getSingleResult(); |
695
|
|
|
|
696
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $address2->getUser()); |
697
|
|
|
$this->assertTrue($userRef === $address2->getUser()); |
698
|
|
|
$this->assertFalse($userRef->__isInitialized__); |
699
|
|
|
$this->assertEquals('Germany', $address2->country); |
700
|
|
|
$this->assertEquals('Berlin', $address2->city); |
701
|
|
|
$this->assertEquals('12345', $address2->zip); |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
public function testOneToOneNullUpdate() |
705
|
|
|
{ |
706
|
|
|
$user = new CmsUser(); |
707
|
|
|
$user->username = "beberlei"; |
708
|
|
|
$user->name = "Benjamin E."; |
709
|
|
|
$user->status = 'active'; |
710
|
|
|
|
711
|
|
|
$address = new CmsAddress(); |
712
|
|
|
$address->city = "Bonn"; |
713
|
|
|
$address->zip = "12354"; |
714
|
|
|
$address->country = "Germany"; |
715
|
|
|
$address->street = "somestreet"; |
716
|
|
|
$address->user = $user; |
717
|
|
|
|
718
|
|
|
$this->_em->persist($address); |
719
|
|
|
$this->_em->persist($user); |
720
|
|
|
$this->_em->flush(); |
721
|
|
|
|
722
|
|
|
$this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id)); |
723
|
|
|
|
724
|
|
|
$address->user = null; |
725
|
|
|
$this->_em->flush(); |
726
|
|
|
|
727
|
|
|
$this->assertNotEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id)); |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* @group DDC-600 |
732
|
|
|
* @group DDC-455 |
733
|
|
|
*/ |
734
|
|
|
public function testNewAssociatedEntityDuringFlushThrowsException() |
735
|
|
|
{ |
736
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
737
|
|
|
$user = new CmsUser(); |
738
|
|
|
$user->username = "beberlei"; |
739
|
|
|
$user->name = "Benjamin E."; |
740
|
|
|
$user->status = 'active'; |
741
|
|
|
|
742
|
|
|
$address = new CmsAddress(); |
743
|
|
|
$address->city = "Bonn"; |
744
|
|
|
$address->zip = "12354"; |
745
|
|
|
$address->country = "Germany"; |
746
|
|
|
$address->street = "somestreet"; |
747
|
|
|
$address->user = $user; |
748
|
|
|
|
749
|
|
|
$this->_em->persist($address); |
750
|
|
|
// pretend we forgot to persist $user |
751
|
|
|
try { |
752
|
|
|
$this->_em->flush(); // should raise an exception |
753
|
|
|
$this->fail(); |
754
|
|
|
} catch (\InvalidArgumentException $expected) {} |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* @group DDC-600 |
759
|
|
|
* @group DDC-455 |
760
|
|
|
*/ |
761
|
|
|
public function testNewAssociatedEntityDuringFlushThrowsException2() |
762
|
|
|
{ |
763
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
764
|
|
|
$user = new CmsUser(); |
765
|
|
|
$user->username = "beberlei"; |
766
|
|
|
$user->name = "Benjamin E."; |
767
|
|
|
$user->status = 'active'; |
768
|
|
|
|
769
|
|
|
$address = new CmsAddress(); |
770
|
|
|
$address->city = "Bonn"; |
771
|
|
|
$address->zip = "12354"; |
772
|
|
|
$address->country = "Germany"; |
773
|
|
|
$address->street = "somestreet"; |
774
|
|
|
$address->user = $user; |
775
|
|
|
|
776
|
|
|
$this->_em->persist($address); |
777
|
|
|
$this->_em->persist($user); |
778
|
|
|
$this->_em->flush(); |
779
|
|
|
|
780
|
|
|
$u2 = new CmsUser; |
781
|
|
|
$u2->username = "beberlei"; |
782
|
|
|
$u2->name = "Benjamin E."; |
783
|
|
|
$u2->status = 'inactive'; |
784
|
|
|
$address->user = $u2; |
785
|
|
|
// pretend we forgot to persist $u2 |
786
|
|
|
try { |
787
|
|
|
$this->_em->flush(); // should raise an exception |
788
|
|
|
$this->fail(); |
789
|
|
|
} catch (\InvalidArgumentException $expected) {} |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
/** |
793
|
|
|
* @group DDC-600 |
794
|
|
|
* @group DDC-455 |
795
|
|
|
*/ |
796
|
|
|
public function testNewAssociatedEntityDuringFlushThrowsException3() |
797
|
|
|
{ |
798
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
799
|
|
|
$art = new CmsArticle(); |
800
|
|
|
$art->topic = 'topic'; |
801
|
|
|
$art->text = 'the text'; |
802
|
|
|
|
803
|
|
|
$com = new CmsComment(); |
804
|
|
|
$com->topic = 'Good'; |
805
|
|
|
$com->text = 'Really good!'; |
806
|
|
|
$art->addComment($com); |
807
|
|
|
|
808
|
|
|
$this->_em->persist($art); |
809
|
|
|
// pretend we forgot to persist $com |
810
|
|
|
try { |
811
|
|
|
$this->_em->flush(); // should raise an exception |
812
|
|
|
$this->fail(); |
813
|
|
|
} catch (\InvalidArgumentException $expected) {} |
814
|
|
|
} |
815
|
|
|
|
816
|
|
|
public function testOneToOneOrphanRemoval() |
817
|
|
|
{ |
818
|
|
|
$user = new CmsUser(); |
819
|
|
|
$user->username = "beberlei"; |
820
|
|
|
$user->name = "Benjamin E."; |
821
|
|
|
$user->status = 'active'; |
822
|
|
|
|
823
|
|
|
$address = new CmsAddress(); |
824
|
|
|
$address->city = "Bonn"; |
825
|
|
|
$address->zip = "12354"; |
826
|
|
|
$address->country = "Germany"; |
827
|
|
|
$address->street = "somestreet"; |
828
|
|
|
$address->user = $user; |
829
|
|
|
$user->address = $address; |
830
|
|
|
|
831
|
|
|
$this->_em->persist($address); |
832
|
|
|
$this->_em->persist($user); |
833
|
|
|
$this->_em->flush(); |
834
|
|
|
$addressId = $address->getId(); |
835
|
|
|
|
836
|
|
|
$user->address = null; |
837
|
|
|
|
838
|
|
|
$this->_em->flush(); |
839
|
|
|
|
840
|
|
|
$this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses")); |
841
|
|
|
|
842
|
|
|
// check orphan removal through replacement |
843
|
|
|
$user->address = $address; |
844
|
|
|
$address->user = $user; |
845
|
|
|
|
846
|
|
|
$this->_em->flush(); |
847
|
|
|
$this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses")); |
848
|
|
|
|
849
|
|
|
// remove $address to free up unique key id |
850
|
|
|
$this->_em->remove($address); |
851
|
|
|
$this->_em->flush(); |
852
|
|
|
|
853
|
|
|
$newAddress = new CmsAddress(); |
854
|
|
|
$newAddress->city = "NewBonn"; |
855
|
|
|
$newAddress->zip = "12354"; |
856
|
|
|
$newAddress->country = "NewGermany"; |
857
|
|
|
$newAddress->street = "somenewstreet"; |
858
|
|
|
$newAddress->user = $user; |
859
|
|
|
$user->address = $newAddress; |
860
|
|
|
|
861
|
|
|
$this->_em->flush(); |
862
|
|
|
$this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses")); |
863
|
|
|
} |
864
|
|
|
|
865
|
|
|
public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt() |
866
|
|
|
{ |
867
|
|
|
$user = new CmsUser(); |
868
|
|
|
$user->username = "beberlei"; |
869
|
|
|
$user->name = "Benjamin E."; |
870
|
|
|
$user->status = 'active'; |
871
|
|
|
$this->_em->persist($user); |
872
|
|
|
$this->_em->flush(); |
873
|
|
|
$userId = $user->id; |
874
|
|
|
$this->_em->clear(); |
875
|
|
|
|
876
|
|
|
$user = $this->_em->getPartialReference('Doctrine\Tests\Models\CMS\CmsUser', $userId); |
877
|
|
|
$this->assertTrue($this->_em->contains($user)); |
878
|
|
|
$this->assertNull($user->getName()); |
879
|
|
|
$this->assertEquals($userId, $user->id); |
880
|
|
|
|
881
|
|
|
$user->name = 'Stephan'; |
882
|
|
|
$this->_em->flush(); |
883
|
|
|
$this->_em->clear(); |
884
|
|
|
|
885
|
|
|
$this->assertEquals('Benjamin E.', $this->_em->find(get_class($user), $userId)->name); |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
public function testMergePersistsNewEntities() |
889
|
|
|
{ |
890
|
|
|
$user = new CmsUser(); |
891
|
|
|
$user->username = "beberlei"; |
892
|
|
|
$user->name = "Benjamin E."; |
893
|
|
|
$user->status = 'active'; |
894
|
|
|
|
895
|
|
|
$managedUser = $this->_em->merge($user); |
896
|
|
|
$this->assertEquals('beberlei', $managedUser->username); |
897
|
|
|
$this->assertEquals('Benjamin E.', $managedUser->name); |
898
|
|
|
$this->assertEquals('active', $managedUser->status); |
899
|
|
|
|
900
|
|
|
$this->assertTrue($user !== $managedUser); |
901
|
|
|
$this->assertTrue($this->_em->contains($managedUser)); |
902
|
|
|
|
903
|
|
|
$this->_em->flush(); |
904
|
|
|
$userId = $managedUser->id; |
905
|
|
|
$this->_em->clear(); |
906
|
|
|
|
907
|
|
|
$user2 = $this->_em->find(get_class($managedUser), $userId); |
908
|
|
|
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $user2); |
909
|
|
|
} |
910
|
|
|
|
911
|
|
|
public function testMergeNonPersistedProperties() |
912
|
|
|
{ |
913
|
|
|
$user = new CmsUser(); |
914
|
|
|
$user->username = "beberlei"; |
915
|
|
|
$user->name = "Benjamin E."; |
916
|
|
|
$user->status = 'active'; |
917
|
|
|
$user->nonPersistedProperty = 'test'; |
918
|
|
|
$user->nonPersistedPropertyObject = new CmsPhonenumber(); |
919
|
|
|
|
920
|
|
|
$managedUser = $this->_em->merge($user); |
921
|
|
|
$this->assertEquals('test', $managedUser->nonPersistedProperty); |
922
|
|
|
$this->assertSame($user->nonPersistedProperty, $managedUser->nonPersistedProperty); |
923
|
|
|
$this->assertSame($user->nonPersistedPropertyObject, $managedUser->nonPersistedPropertyObject); |
924
|
|
|
|
925
|
|
|
$this->assertTrue($user !== $managedUser); |
926
|
|
|
$this->assertTrue($this->_em->contains($managedUser)); |
927
|
|
|
|
928
|
|
|
$this->_em->flush(); |
929
|
|
|
$userId = $managedUser->id; |
930
|
|
|
$this->_em->clear(); |
931
|
|
|
|
932
|
|
|
$user2 = $this->_em->find(get_class($managedUser), $userId); |
933
|
|
|
$this->assertNull($user2->nonPersistedProperty); |
934
|
|
|
$this->assertNull($user2->nonPersistedPropertyObject); |
935
|
|
|
$this->assertEquals('active', $user2->status); |
936
|
|
|
} |
937
|
|
|
|
938
|
|
|
public function testMergeThrowsExceptionIfEntityWithGeneratedIdentifierDoesNotExist() |
939
|
|
|
{ |
940
|
|
|
$user = new CmsUser(); |
941
|
|
|
$user->username = "beberlei"; |
942
|
|
|
$user->name = "Benjamin E."; |
943
|
|
|
$user->status = 'active'; |
944
|
|
|
$user->id = 42; |
945
|
|
|
try { |
946
|
|
|
$this->_em->merge($user); |
947
|
|
|
$this->fail(); |
948
|
|
|
} catch (EntityNotFoundException $enfe) {} |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* @group DDC-634 |
953
|
|
|
*/ |
954
|
|
|
public function testOneToOneMergeSetNull() |
955
|
|
|
{ |
956
|
|
|
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); |
957
|
|
|
$user = new CmsUser(); |
958
|
|
|
$user->username = "beberlei"; |
959
|
|
|
$user->name = "Benjamin E."; |
960
|
|
|
$user->status = 'active'; |
961
|
|
|
|
962
|
|
|
$ph = new CmsPhonenumber(); |
963
|
|
|
$ph->phonenumber = "12345"; |
964
|
|
|
$user->addPhonenumber($ph); |
965
|
|
|
|
966
|
|
|
$this->_em->persist($user); |
967
|
|
|
$this->_em->persist($ph); |
968
|
|
|
$this->_em->flush(); |
969
|
|
|
|
970
|
|
|
$this->_em->clear(); |
971
|
|
|
|
972
|
|
|
$ph->user = null; |
973
|
|
|
$managedPh = $this->_em->merge($ph); |
974
|
|
|
|
975
|
|
|
$this->_em->flush(); |
976
|
|
|
$this->_em->clear(); |
977
|
|
|
|
978
|
|
|
$this->assertNull($this->_em->find(get_class($ph), $ph->phonenumber)->getUser()); |
979
|
|
|
} |
980
|
|
|
|
981
|
|
|
/** |
982
|
|
|
* @group DDC-952 |
983
|
|
|
*/ |
984
|
|
|
public function testManyToOneFetchModeQuery() |
985
|
|
|
{ |
986
|
|
|
$user = new CmsUser(); |
987
|
|
|
$user->username = "beberlei"; |
988
|
|
|
$user->name = "Benjamin E."; |
989
|
|
|
$user->status = 'active'; |
990
|
|
|
|
991
|
|
|
$article = new CmsArticle(); |
992
|
|
|
$article->topic = "foo"; |
993
|
|
|
$article->text = "bar"; |
994
|
|
|
$article->user = $user; |
995
|
|
|
|
996
|
|
|
$this->_em->persist($article); |
997
|
|
|
$this->_em->persist($user); |
998
|
|
|
$this->_em->flush(); |
999
|
|
|
$this->_em->clear(); |
1000
|
|
|
|
1001
|
|
|
$qc = $this->getCurrentQueryCount(); |
1002
|
|
|
$dql = "SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.id = ?1"; |
1003
|
|
|
$article = $this->_em->createQuery($dql) |
1004
|
|
|
->setParameter(1, $article->id) |
1005
|
|
|
->setFetchMode('Doctrine\Tests\Models\CMS\CmsArticle', 'user', ClassMetadata::FETCH_EAGER) |
1006
|
|
|
->getSingleResult(); |
1007
|
|
|
$this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $article->user, "It IS a proxy, ..."); |
1008
|
|
|
$this->assertTrue($article->user->__isInitialized__, "...but its initialized!"); |
1009
|
|
|
$this->assertEquals($qc+2, $this->getCurrentQueryCount()); |
1010
|
|
|
} |
1011
|
|
|
|
1012
|
|
|
/** |
1013
|
|
|
* @group DDC-1278 |
1014
|
|
|
*/ |
1015
|
|
|
public function testClearWithEntityName() |
1016
|
|
|
{ |
1017
|
|
|
$user = new CmsUser; |
1018
|
|
|
$user->name = 'Dominik'; |
1019
|
|
|
$user->username = 'domnikl'; |
1020
|
|
|
$user->status = 'developer'; |
1021
|
|
|
|
1022
|
|
|
$address = new CmsAddress(); |
1023
|
|
|
$address->city = "Springfield"; |
1024
|
|
|
$address->zip = "12354"; |
1025
|
|
|
$address->country = "Germany"; |
1026
|
|
|
$address->street = "Foo Street"; |
1027
|
|
|
$address->user = $user; |
1028
|
|
|
$user->address = $address; |
1029
|
|
|
|
1030
|
|
|
$article1 = new CmsArticle(); |
1031
|
|
|
$article1->topic = 'Foo'; |
1032
|
|
|
$article1->text = 'Foo Text'; |
1033
|
|
|
|
1034
|
|
|
$article2 = new CmsArticle(); |
1035
|
|
|
$article2->topic = 'Bar'; |
1036
|
|
|
$article2->text = 'Bar Text'; |
1037
|
|
|
|
1038
|
|
|
$user->addArticle($article1); |
1039
|
|
|
$user->addArticle($article2); |
1040
|
|
|
|
1041
|
|
|
$this->_em->persist($article1); |
1042
|
|
|
$this->_em->persist($article2); |
1043
|
|
|
$this->_em->persist($address); |
1044
|
|
|
$this->_em->persist($user); |
1045
|
|
|
$this->_em->flush(); |
1046
|
|
|
|
1047
|
|
|
$unitOfWork = $this->_em->getUnitOfWork(); |
1048
|
|
|
|
1049
|
|
|
$this->_em->clear('Doctrine\Tests\Models\CMS\CmsUser'); |
1050
|
|
|
|
1051
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($user)); |
1052
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($article1)); |
1053
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($article2)); |
1054
|
|
|
$this->assertEquals(UnitOfWork::STATE_MANAGED, $unitOfWork->getEntityState($address)); |
1055
|
|
|
|
1056
|
|
|
$this->_em->clear(); |
1057
|
|
|
|
1058
|
|
|
$this->assertEquals(UnitOfWork::STATE_DETACHED, $unitOfWork->getEntityState($address)); |
1059
|
|
|
} |
1060
|
|
|
|
1061
|
|
|
public function testFlushManyExplicitEntities() |
1062
|
|
|
{ |
1063
|
|
|
$userA = new CmsUser; |
1064
|
|
|
$userA->username = 'UserA'; |
1065
|
|
|
$userA->name = 'UserA'; |
1066
|
|
|
|
1067
|
|
|
$userB = new CmsUser; |
1068
|
|
|
$userB->username = 'UserB'; |
1069
|
|
|
$userB->name = 'UserB'; |
1070
|
|
|
|
1071
|
|
|
$userC = new CmsUser; |
1072
|
|
|
$userC->username = 'UserC'; |
1073
|
|
|
$userC->name = 'UserC'; |
1074
|
|
|
|
1075
|
|
|
$this->_em->persist($userA); |
1076
|
|
|
$this->_em->persist($userB); |
1077
|
|
|
$this->_em->persist($userC); |
1078
|
|
|
|
1079
|
|
|
$this->_em->flush(array($userA, $userB, $userB)); |
1080
|
|
|
|
1081
|
|
|
$userC->name = 'changed name'; |
1082
|
|
|
|
1083
|
|
|
$this->_em->flush(array($userA, $userB)); |
1084
|
|
|
$this->_em->refresh($userC); |
1085
|
|
|
|
1086
|
|
|
$this->assertTrue($userA->id > 0, 'user a has an id'); |
1087
|
|
|
$this->assertTrue($userB->id > 0, 'user b has an id'); |
1088
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForInsert($userC), 'user c is not flushed'); |
1089
|
|
|
|
1090
|
|
|
$this->assertEquals('changed name', $userC->name, 'name changed because we did not flush it, so we can not refresh it'); |
1091
|
|
|
} |
1092
|
|
|
|
1093
|
|
|
/** |
1094
|
|
|
* @group DDC-720 |
1095
|
|
|
*/ |
1096
|
|
|
public function testFlushSingleManagedEntity() |
1097
|
|
|
{ |
1098
|
|
|
$user = new CmsUser; |
1099
|
|
|
$user->name = 'Dominik'; |
1100
|
|
|
$user->username = 'domnikl'; |
1101
|
|
|
$user->status = 'developer'; |
1102
|
|
|
|
1103
|
|
|
$this->_em->persist($user); |
1104
|
|
|
$this->_em->flush(); |
1105
|
|
|
|
1106
|
|
|
$user->status = 'administrator'; |
1107
|
|
|
$this->_em->flush($user); |
1108
|
|
|
$this->_em->clear(); |
1109
|
|
|
|
1110
|
|
|
$user = $this->_em->find(get_class($user), $user->id); |
1111
|
|
|
$this->assertEquals('administrator', $user->status); |
1112
|
|
|
} |
1113
|
|
|
|
1114
|
|
|
/** |
1115
|
|
|
* @group DDC-720 |
1116
|
|
|
*/ |
1117
|
|
|
public function testFlushSingleUnmanagedEntity() |
1118
|
|
|
{ |
1119
|
|
|
$user = new CmsUser; |
1120
|
|
|
$user->name = 'Dominik'; |
1121
|
|
|
$user->username = 'domnikl'; |
1122
|
|
|
$user->status = 'developer'; |
1123
|
|
|
|
1124
|
|
|
$this->expectException(\InvalidArgumentException::class); |
1125
|
|
|
$this->expectExceptionMessage('Entity has to be managed or scheduled for removal for single computation'); |
1126
|
|
|
|
1127
|
|
|
$this->_em->flush($user); |
1128
|
|
|
} |
1129
|
|
|
|
1130
|
|
|
/** |
1131
|
|
|
* @group DDC-720 |
1132
|
|
|
*/ |
1133
|
|
|
public function testFlushSingleAndNewEntity() |
1134
|
|
|
{ |
1135
|
|
|
$user = new CmsUser; |
1136
|
|
|
$user->name = 'Dominik'; |
1137
|
|
|
$user->username = 'domnikl'; |
1138
|
|
|
$user->status = 'developer'; |
1139
|
|
|
|
1140
|
|
|
$this->_em->persist($user); |
1141
|
|
|
$this->_em->flush(); |
1142
|
|
|
|
1143
|
|
|
$otherUser = new CmsUser; |
1144
|
|
|
$otherUser->name = 'Dominik2'; |
1145
|
|
|
$otherUser->username = 'domnikl2'; |
1146
|
|
|
$otherUser->status = 'developer'; |
1147
|
|
|
|
1148
|
|
|
$user->status = 'administrator'; |
1149
|
|
|
|
1150
|
|
|
$this->_em->persist($otherUser); |
1151
|
|
|
$this->_em->flush($user); |
1152
|
|
|
|
1153
|
|
|
$this->assertTrue($this->_em->contains($otherUser), "Other user is contained in EntityManager"); |
1154
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForInsert($otherUser), 'other user is not flushed'); |
1155
|
|
|
} |
1156
|
|
|
|
1157
|
|
|
/** |
1158
|
|
|
* @group DDC-720 |
1159
|
|
|
*/ |
1160
|
|
|
public function testFlushAndCascadePersist() |
1161
|
|
|
{ |
1162
|
|
|
$user = new CmsUser; |
1163
|
|
|
$user->name = 'Dominik'; |
1164
|
|
|
$user->username = 'domnikl'; |
1165
|
|
|
$user->status = 'developer'; |
1166
|
|
|
|
1167
|
|
|
$this->_em->persist($user); |
1168
|
|
|
$this->_em->flush(); |
1169
|
|
|
|
1170
|
|
|
$address = new CmsAddress(); |
1171
|
|
|
$address->city = "Springfield"; |
1172
|
|
|
$address->zip = "12354"; |
1173
|
|
|
$address->country = "Germany"; |
1174
|
|
|
$address->street = "Foo Street"; |
1175
|
|
|
$address->user = $user; |
1176
|
|
|
$user->address = $address; |
1177
|
|
|
|
1178
|
|
|
$this->_em->flush($user); |
1179
|
|
|
|
1180
|
|
|
$this->assertTrue($this->_em->contains($address), "Address is contained in EntityManager"); |
1181
|
|
|
$this->assertTrue($address->id > 0, "Address has an id"); |
1182
|
|
|
} |
1183
|
|
|
|
1184
|
|
|
public function testFlushOnSingleEntity() |
1185
|
|
|
{ |
1186
|
|
|
$user = new CmsUser; |
1187
|
|
|
$user->name = 'Dominik'; |
1188
|
|
|
$user->username = 'domnikl'; |
1189
|
|
|
$user->status = 'developer'; |
1190
|
|
|
|
1191
|
|
|
$this->_em->persist($user); |
1192
|
|
|
|
1193
|
|
|
$email = new CmsEmail; |
1194
|
|
|
$email->email = '[email protected]'; |
1195
|
|
|
|
1196
|
|
|
$this->_em->persist($email); |
1197
|
|
|
|
1198
|
|
|
$q = $this->getCurrentQueryCount(); |
1199
|
|
|
$this->_em->flush($email); |
1200
|
|
|
|
1201
|
|
|
$this->assertEquals($q+3, $this->getCurrentQueryCount()); |
1202
|
|
|
|
1203
|
|
|
$q = $this->getCurrentQueryCount(); |
1204
|
|
|
$this->_em->flush($user); |
1205
|
|
|
|
1206
|
|
|
$this->assertEquals($q+3, $this->getCurrentQueryCount()); |
1207
|
|
|
} |
1208
|
|
|
|
1209
|
|
|
public function testFlushOnSingleEntitySameClass() |
1210
|
|
|
{ |
1211
|
|
|
$domnik = new CmsUser; |
1212
|
|
|
$domnik->name = 'Dominik'; |
1213
|
|
|
$domnik->username = 'domnikl'; |
1214
|
|
|
$domnik->status = 'developer'; |
1215
|
|
|
$this->_em->persist($domnik); |
1216
|
|
|
|
1217
|
|
|
$fabian = new CmsUser; |
1218
|
|
|
$fabian->name = 'Fabian'; |
1219
|
|
|
$fabian->username = 'fabian'; |
1220
|
|
|
$fabian->status = 'tester'; |
1221
|
|
|
$this->_em->persist($fabian); |
1222
|
|
|
|
1223
|
|
|
$q = $this->getCurrentQueryCount(); |
1224
|
|
|
$this->_em->flush($fabian); |
1225
|
|
|
$this->assertEquals($q+3, $this->getCurrentQueryCount(), "Only Fabian should be flushed"); |
1226
|
|
|
|
1227
|
|
|
$q = $this->getCurrentQueryCount(); |
1228
|
|
|
$this->_em->flush($domnik); |
1229
|
|
|
$this->assertEquals($q+3, $this->getCurrentQueryCount(), "Dominik should be flushed"); |
1230
|
|
|
} |
1231
|
|
|
|
1232
|
|
|
/** |
1233
|
|
|
* @group DDC-720 |
1234
|
|
|
*/ |
1235
|
|
|
public function testFlushSingleAndNoCascade() |
1236
|
|
|
{ |
1237
|
|
|
$user = new CmsUser; |
1238
|
|
|
$user->name = 'Dominik'; |
1239
|
|
|
$user->username = 'domnikl'; |
1240
|
|
|
$user->status = 'developer'; |
1241
|
|
|
|
1242
|
|
|
$this->_em->persist($user); |
1243
|
|
|
$this->_em->flush(); |
1244
|
|
|
|
1245
|
|
|
$article1 = new CmsArticle(); |
1246
|
|
|
$article1->topic = 'Foo'; |
1247
|
|
|
$article1->text = 'Foo Text'; |
1248
|
|
|
$article1->author = $user; |
1249
|
|
|
$user->articles[] = $article1; |
1250
|
|
|
|
1251
|
|
|
$this->expectException(\InvalidArgumentException::class); |
1252
|
|
|
$this->expectExceptionMessage("A new entity was found through the relationship 'Doctrine\Tests\Models\CMS\CmsUser#articles'"); |
1253
|
|
|
|
1254
|
|
|
$this->_em->flush($user); |
1255
|
|
|
} |
1256
|
|
|
|
1257
|
|
|
/** |
1258
|
|
|
* @group DDC-720 |
1259
|
|
|
* @group DDC-1612 |
1260
|
|
|
* @group DDC-2267 |
1261
|
|
|
*/ |
1262
|
|
|
public function testFlushSingleNewEntityThenRemove() |
1263
|
|
|
{ |
1264
|
|
|
$user = new CmsUser; |
1265
|
|
|
$user->name = 'Dominik'; |
1266
|
|
|
$user->username = 'domnikl'; |
1267
|
|
|
$user->status = 'developer'; |
1268
|
|
|
|
1269
|
|
|
$this->_em->persist($user); |
1270
|
|
|
$this->_em->flush($user); |
1271
|
|
|
|
1272
|
|
|
$userId = $user->id; |
1273
|
|
|
|
1274
|
|
|
$this->_em->remove($user); |
1275
|
|
|
$this->_em->flush($user); |
1276
|
|
|
$this->_em->clear(); |
1277
|
|
|
|
1278
|
|
|
$this->assertNull($this->_em->find(get_class($user), $userId)); |
1279
|
|
|
} |
1280
|
|
|
|
1281
|
|
|
/** |
1282
|
|
|
* @group DDC-720 |
1283
|
|
|
*/ |
1284
|
|
|
public function testProxyIsIgnored() |
1285
|
|
|
{ |
1286
|
|
|
$user = new CmsUser; |
1287
|
|
|
$user->name = 'Dominik'; |
1288
|
|
|
$user->username = 'domnikl'; |
1289
|
|
|
$user->status = 'developer'; |
1290
|
|
|
|
1291
|
|
|
$this->_em->persist($user); |
1292
|
|
|
$this->_em->flush(); |
1293
|
|
|
$this->_em->clear(); |
1294
|
|
|
|
1295
|
|
|
$user = $this->_em->getReference(get_class($user), $user->id); |
1296
|
|
|
|
1297
|
|
|
$otherUser = new CmsUser; |
1298
|
|
|
$otherUser->name = 'Dominik2'; |
1299
|
|
|
$otherUser->username = 'domnikl2'; |
1300
|
|
|
$otherUser->status = 'developer'; |
1301
|
|
|
|
1302
|
|
|
$this->_em->persist($otherUser); |
1303
|
|
|
$this->_em->flush($user); |
1304
|
|
|
|
1305
|
|
|
$this->assertTrue($this->_em->contains($otherUser), "Other user is contained in EntityManager"); |
1306
|
|
|
$this->assertTrue($this->_em->getUnitOfWork()->isScheduledForInsert($otherUser), 'other user is not flushed'); |
1307
|
|
|
} |
1308
|
|
|
|
1309
|
|
|
/** |
1310
|
|
|
* @group DDC-720 |
1311
|
|
|
*/ |
1312
|
|
|
public function testFlushSingleSaveOnlySingle() |
1313
|
|
|
{ |
1314
|
|
|
$user = new CmsUser; |
1315
|
|
|
$user->name = 'Dominik'; |
1316
|
|
|
$user->username = 'domnikl'; |
1317
|
|
|
$user->status = 'developer'; |
1318
|
|
|
$this->_em->persist($user); |
1319
|
|
|
|
1320
|
|
|
$user2 = new CmsUser; |
1321
|
|
|
$user2->name = 'Dominik'; |
1322
|
|
|
$user2->username = 'domnikl2'; |
1323
|
|
|
$user2->status = 'developer'; |
1324
|
|
|
$this->_em->persist($user2); |
1325
|
|
|
|
1326
|
|
|
$this->_em->flush(); |
1327
|
|
|
|
1328
|
|
|
$user->status = 'admin'; |
1329
|
|
|
$user2->status = 'admin'; |
1330
|
|
|
|
1331
|
|
|
$this->_em->flush($user); |
1332
|
|
|
$this->_em->clear(); |
1333
|
|
|
|
1334
|
|
|
$user2 = $this->_em->find(get_class($user2), $user2->id); |
1335
|
|
|
$this->assertEquals('developer', $user2->status); |
1336
|
|
|
} |
1337
|
|
|
|
1338
|
|
|
/** |
1339
|
|
|
* @group DDC-1585 |
1340
|
|
|
*/ |
1341
|
|
|
public function testWrongAssociationInstance() |
1342
|
|
|
{ |
1343
|
|
|
$user = new CmsUser; |
1344
|
|
|
$user->name = 'Dominik'; |
1345
|
|
|
$user->username = 'domnikl'; |
1346
|
|
|
$user->status = 'developer'; |
1347
|
|
|
$user->address = $user; |
1348
|
|
|
|
1349
|
|
|
$this->expectException(ORMInvalidArgumentException::class); |
1350
|
|
|
$this->expectExceptionMessage( |
1351
|
|
|
'Expected value of type "Doctrine\Tests\Models\CMS\CmsAddress" for association field ' . |
1352
|
|
|
'"Doctrine\Tests\Models\CMS\CmsUser#$address", got "Doctrine\Tests\Models\CMS\CmsUser" instead.' |
1353
|
|
|
); |
1354
|
|
|
|
1355
|
|
|
$this->_em->persist($user); |
1356
|
|
|
|
1357
|
|
|
$this->_em->flush(); |
1358
|
|
|
} |
1359
|
|
|
} |
1360
|
|
|
|