-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ManyToManyBasicAssociationTest.php
380 lines (288 loc) · 12 KB
/
ManyToManyBasicAssociationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Tests\Models\CMS\CmsUser,
Doctrine\Tests\Models\CMS\CmsGroup,
Doctrine\Common\Collections\ArrayCollection;
require_once __DIR__ . '/../../TestInit.php';
/**
* Basic many-to-many association tests.
* ("Working with associations")
*
* @author robo
*/
class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
$this->useModelSet('cms');
parent::setUp();
}
public function testUnsetManyToMany()
{
$user = $this->addCmsUserGblancoWithGroups(1);
unset($user->groups[0]->users[0]); // inverse side
unset($user->groups[0]); // owning side!
$this->_em->flush();
// Check that the link in the association table has been deleted
$this->assertGblancoGroupCountIs(0);
}
public function testBasicManyToManyJoin()
{
$user = $this->addCmsUserGblancoWithGroups(1);
$this->_em->clear();
$this->assertEquals(0, $this->_em->getUnitOfWork()->size());
$query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u join u.groups g");
$result = $query->getResult();
$this->assertEquals(2, $this->_em->getUnitOfWork()->size());
$this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0]);
$this->assertEquals('Guilherme', $result[0]->name);
$this->assertEquals(1, $result[0]->getGroups()->count());
$groups = $result[0]->getGroups();
$this->assertEquals('Developers_0', $groups[0]->getName());
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($result[0]));
$this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($groups[0]));
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $groups);
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $groups[0]->getUsers());
$groups[0]->getUsers()->clear();
$groups->clear();
$this->_em->flush();
$this->_em->clear();
$query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u join u.groups g");
$this->assertEquals(0, count($query->getResult()));
}
public function testManyToManyAddRemove()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$this->_em->clear();
$uRep = $this->_em->getRepository(get_class($user));
// Get user
$user = $uRep->findOneById($user->getId());
$this->assertNotNull($user, "Has to return exactly one entry.");
$this->assertFalse($user->getGroups()->isInitialized());
// Check groups
$this->assertEquals(2, $user->getGroups()->count());
$this->assertTrue($user->getGroups()->isInitialized());
// Remove first group
unset($user->groups[0]);
//$user->getGroups()->remove(0);
$this->_em->flush();
$this->_em->clear();
// Reload same user
$user2 = $uRep->findOneById($user->getId());
// Check groups
$this->assertEquals(1, $user2->getGroups()->count());
}
public function testManyToManyInverseSideIgnored()
{
$user = $this->addCmsUserGblancoWithGroups(0);
$group = new CmsGroup;
$group->name = 'Humans';
// modify directly, addUser() would also (properly) set the owning side
$group->users[] = $user;
$this->_em->persist($user);
$this->_em->persist($group);
$this->_em->flush();
$this->_em->clear();
// Association should not exist
$user2 = $this->_em->find(get_class($user), $user->getId());
$this->assertNotNull($user2, "Has to return exactly one entry.");
$this->assertEquals(0, $user2->getGroups()->count());
}
public function testManyToManyCollectionClearing()
{
$user = $this->addCmsUserGblancoWithGroups($groupCount = 10);
// Check that there are indeed 10 links in the association table
$this->assertGblancoGroupCountIs($groupCount);
$user->groups->clear();
$this->_em->flush();
// Check that the links in the association table have been deleted
$this->assertGblancoGroupCountIs(0);
}
public function testManyToManyCollectionClearAndAdd()
{
$user = $this->addCmsUserGblancoWithGroups($groupCount = 10);
$groups = $user->groups->toArray();
$user->groups->clear();
foreach ($groups AS $group) {
$user->groups[] = $group;
}
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->groups);
$this->assertTrue($user->groups->isDirty());
$this->assertEquals($groupCount, count($user->groups), "There should be 10 groups in the collection.");
$this->_em->flush();
$this->assertGblancoGroupCountIs($groupCount);
}
/**
* @param int $expectedGroupCount
*/
public function assertGblancoGroupCountIs($expectedGroupCount)
{
$countDql = "SELECT count(g.id) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g WHERE u.username = 'gblanco'";
$this->assertEquals(
$expectedGroupCount,
$this->_em->createQuery($countDql)->getSingleScalarResult(),
"Failed to verify that CmsUser with username 'gblanco' has a group count of 10 with a DQL count query."
);
}
public function testRetrieveManyToManyAndAddMore()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$group = new CmsGroup();
$group->name = 'Developers_Fresh';
$this->_em->persist($group);
$this->_em->flush();
$this->_em->clear();
/* @var $freshUser CmsUser */
$freshUser = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
$newGroup = new CmsGroup();
$newGroup->setName('12Monkeys');
$freshUser->addGroup($newGroup);
$this->assertFalse($freshUser->groups->isInitialized(), "CmsUser::groups Collection has to be uninitialized for this test.");
$this->_em->flush();
$this->assertFalse($freshUser->groups->isInitialized(), "CmsUser::groups Collection has to be uninitialized for this test.");
$this->assertEquals(3, count($freshUser->getGroups()));
$this->assertEquals(3, count($freshUser->getGroups()->getSnapshot()), "Snapshot of CmsUser::groups should contain 3 entries.");
$this->_em->clear();
$freshUser = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
$this->assertEquals(3, count($freshUser->getGroups()));
}
/**
* @group DDC-130
*/
public function testRemoveUserWithManyGroups()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$userId = $user->getId();
$this->_em->remove($user);
$this->_em->flush();
$newUser = $this->_em->find(get_class($user), $userId);
$this->assertNull($newUser);
}
/**
* @group DDC-130
*/
public function testRemoveGroupWithUser()
{
$user = $this->addCmsUserGblancoWithGroups(2);
foreach ($user->getGroups() AS $group) {
$this->_em->remove($group);
}
$this->_em->flush();
$this->_em->clear();
$newUser = $this->_em->find(get_class($user), $user->getId());
$this->assertEquals(0, count($newUser->getGroups()));
}
public function testDereferenceCollectionDelete()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$user->groups = null;
$this->_em->flush();
$this->_em->clear();
$newUser = $this->_em->find(get_class($user), $user->getId());
$this->assertEquals(0, count($newUser->getGroups()));
}
/**
* @group DDC-839
*/
public function testWorkWithDqlHydratedEmptyCollection()
{
$user = $this->addCmsUserGblancoWithGroups(0);
$group = new CmsGroup();
$group->name = "Developers0";
$this->_em->persist($group);
$this->_em->flush();
$this->_em->clear();
$newUser = $this->_em->createQuery('SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.groups g WHERE u.id = ?1')
->setParameter(1, $user->getId())
->getSingleResult();
$this->assertEquals(0, count($newUser->groups));
$this->assertInternalType('array', $newUser->groups->getMapping());
$newUser->addGroup($group);
$this->_em->flush();
$this->_em->clear();
$newUser = $this->_em->find(get_class($user), $user->getId());
$this->assertEquals(1, count($newUser->groups));
}
/**
* @param int $groupCount
* @return CmsUser
*/
public function addCmsUserGblancoWithGroups($groupCount = 1)
{
$user = new CmsUser;
$user->name = 'Guilherme';
$user->username = 'gblanco';
$user->status = 'developer';
for ($i=0; $i < $groupCount; ++$i) {
$group = new CmsGroup;
$group->name = 'Developers_' . $i;
$user->addGroup($group);
}
$this->_em->persist($user);
$this->_em->flush();
$this->assertNotNull($user->getId(), "User 'gblanco' should have an ID assigned after the persist()/flush() operation.");
return $user;
}
/**
* @group DDC-980
*/
public function testUpdateDeleteSizeSubselectQueries()
{
$this->_em->createQuery("DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10")->execute();
$this->_em->createQuery("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = 'inactive' WHERE SIZE(u.groups) = 10")->execute();
}
/**
* @group DDC-978
*/
public function testClearAndResetCollection()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$group1 = new CmsGroup;
$group1->name = 'Developers_New1';
$group2 = new CmsGroup;
$group2->name = 'Developers_New2';
$this->_em->persist($group1);
$this->_em->persist($group2);
$this->_em->flush();
$this->_em->clear();
$user = $this->_em->find(get_class($user), $user->id);
$coll = new ArrayCollection(array($group1, $group2));
$user->groups = $coll;
$this->_em->flush();
$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->groups,
"UnitOfWork should have replaced ArrayCollection with PersistentCollection.");
$this->_em->flush();
$this->_em->clear();
$user = $this->_em->find(get_class($user), $user->id);
$this->assertEquals(2, count($user->groups));
$this->assertEquals('Developers_New1', $user->groups[0]->name);
$this->assertEquals('Developers_New2', $user->groups[1]->name);
}
/**
* @group DDC-733
*/
public function testInitializePersistentCollection()
{
$user = $this->addCmsUserGblancoWithGroups(2);
$this->_em->clear();
$user = $this->_em->find(get_class($user), $user->id);
$this->assertFalse($user->groups->isInitialized(), "Pre-condition: lazy collection");
$this->_em->getUnitOfWork()->initializeObject($user->groups);
$this->assertTrue($user->groups->isInitialized(), "Collection should be initialized after calling UnitOfWork::initializeObject()");
}
/**
* @group DDC-1189
* @group DDC-956
*/
public function testClearBeforeLazyLoad()
{
$user = $this->addCmsUserGblancoWithGroups(4);
$this->_em->clear();
$user = $this->_em->find(get_class($user), $user->id);
$user->groups->clear();
$this->assertEquals(0, count($user->groups));
$this->_em->flush();
$user = $this->_em->find(get_class($user), $user->id);
$this->assertEquals(0, count($user->groups));
}
}