3 namespace Doctrine\Tests\ORM\Functional;
5 use Doctrine\Tests\Models\CMS\CmsUser,
6 Doctrine\Tests\Models\CMS\CmsGroup,
7 Doctrine\Common\Collections\ArrayCollection;
9 require_once __DIR__ . '/../../TestInit.php';
12 * Basic many-to-many association tests.
13 * ("Working with associations")
17 class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
19 protected function setUp()
21 $this->useModelSet('cms');
25 public function testUnsetManyToMany()
27 $user = $this->addCmsUserGblancoWithGroups(1);
29 unset($user->groups[0]->users[0]); // inverse side
30 unset($user->groups[0]); // owning side!
34 // Check that the link in the association table has been deleted
35 $this->assertGblancoGroupCountIs(0);
38 public function testBasicManyToManyJoin()
40 $user = $this->addCmsUserGblancoWithGroups(1);
43 $this->assertEquals(0, $this->_em->getUnitOfWork()->size());
45 $query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u join u.groups g");
47 $result = $query->getResult();
49 $this->assertEquals(2, $this->_em->getUnitOfWork()->size());
50 $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0]);
51 $this->assertEquals('Guilherme', $result[0]->name);
52 $this->assertEquals(1, $result[0]->getGroups()->count());
53 $groups = $result[0]->getGroups();
54 $this->assertEquals('Developers_0', $groups[0]->getName());
56 $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($result[0]));
57 $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($groups[0]));
59 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $groups);
60 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $groups[0]->getUsers());
62 $groups[0]->getUsers()->clear();
68 $query = $this->_em->createQuery("select u, g from Doctrine\Tests\Models\CMS\CmsUser u join u.groups g");
69 $this->assertEquals(0, count($query->getResult()));
72 public function testManyToManyAddRemove()
74 $user = $this->addCmsUserGblancoWithGroups(2);
77 $uRep = $this->_em->getRepository(get_class($user));
80 $user = $uRep->findOneById($user->getId());
82 $this->assertNotNull($user, "Has to return exactly one entry.");
84 $this->assertFalse($user->getGroups()->isInitialized());
87 $this->assertEquals(2, $user->getGroups()->count());
89 $this->assertTrue($user->getGroups()->isInitialized());
92 unset($user->groups[0]);
93 //$user->getGroups()->remove(0);
99 $user2 = $uRep->findOneById($user->getId());
102 $this->assertEquals(1, $user2->getGroups()->count());
105 public function testManyToManyInverseSideIgnored()
107 $user = $this->addCmsUserGblancoWithGroups(0);
109 $group = new CmsGroup;
110 $group->name = 'Humans';
112 // modify directly, addUser() would also (properly) set the owning side
113 $group->users[] = $user;
115 $this->_em->persist($user);
116 $this->_em->persist($group);
120 // Association should not exist
121 $user2 = $this->_em->find(get_class($user), $user->getId());
123 $this->assertNotNull($user2, "Has to return exactly one entry.");
124 $this->assertEquals(0, $user2->getGroups()->count());
127 public function testManyToManyCollectionClearing()
129 $user = $this->addCmsUserGblancoWithGroups($groupCount = 10);
131 // Check that there are indeed 10 links in the association table
132 $this->assertGblancoGroupCountIs($groupCount);
134 $user->groups->clear();
138 // Check that the links in the association table have been deleted
139 $this->assertGblancoGroupCountIs(0);
142 public function testManyToManyCollectionClearAndAdd()
144 $user = $this->addCmsUserGblancoWithGroups($groupCount = 10);
146 $groups = $user->groups->toArray();
147 $user->groups->clear();
149 foreach ($groups AS $group) {
150 $user->groups[] = $group;
153 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->groups);
154 $this->assertTrue($user->groups->isDirty());
156 $this->assertEquals($groupCount, count($user->groups), "There should be 10 groups in the collection.");
160 $this->assertGblancoGroupCountIs($groupCount);
164 * @param int $expectedGroupCount
166 public function assertGblancoGroupCountIs($expectedGroupCount)
168 $countDql = "SELECT count(g.id) FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g WHERE u.username = 'gblanco'";
171 $this->_em->createQuery($countDql)->getSingleScalarResult(),
172 "Failed to verify that CmsUser with username 'gblanco' has a group count of 10 with a DQL count query."
176 public function testRetrieveManyToManyAndAddMore()
178 $user = $this->addCmsUserGblancoWithGroups(2);
180 $group = new CmsGroup();
181 $group->name = 'Developers_Fresh';
182 $this->_em->persist($group);
187 /* @var $freshUser CmsUser */
188 $freshUser = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
189 $newGroup = new CmsGroup();
190 $newGroup->setName('12Monkeys');
191 $freshUser->addGroup($newGroup);
193 $this->assertFalse($freshUser->groups->isInitialized(), "CmsUser::groups Collection has to be uninitialized for this test.");
197 $this->assertFalse($freshUser->groups->isInitialized(), "CmsUser::groups Collection has to be uninitialized for this test.");
198 $this->assertEquals(3, count($freshUser->getGroups()));
199 $this->assertEquals(3, count($freshUser->getGroups()->getSnapshot()), "Snapshot of CmsUser::groups should contain 3 entries.");
203 $freshUser = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
204 $this->assertEquals(3, count($freshUser->getGroups()));
210 public function testRemoveUserWithManyGroups()
212 $user = $this->addCmsUserGblancoWithGroups(2);
213 $userId = $user->getId();
215 $this->_em->remove($user);
218 $newUser = $this->_em->find(get_class($user), $userId);
219 $this->assertNull($newUser);
225 public function testRemoveGroupWithUser()
227 $user = $this->addCmsUserGblancoWithGroups(2);
229 foreach ($user->getGroups() AS $group) {
230 $this->_em->remove($group);
235 $newUser = $this->_em->find(get_class($user), $user->getId());
236 $this->assertEquals(0, count($newUser->getGroups()));
239 public function testDereferenceCollectionDelete()
241 $user = $this->addCmsUserGblancoWithGroups(2);
242 $user->groups = null;
247 $newUser = $this->_em->find(get_class($user), $user->getId());
248 $this->assertEquals(0, count($newUser->getGroups()));
254 public function testWorkWithDqlHydratedEmptyCollection()
256 $user = $this->addCmsUserGblancoWithGroups(0);
257 $group = new CmsGroup();
258 $group->name = "Developers0";
259 $this->_em->persist($group);
264 $newUser = $this->_em->createQuery('SELECT u, g FROM Doctrine\Tests\Models\CMS\CmsUser u LEFT JOIN u.groups g WHERE u.id = ?1')
265 ->setParameter(1, $user->getId())
267 $this->assertEquals(0, count($newUser->groups));
268 $this->assertInternalType('array', $newUser->groups->getMapping());
270 $newUser->addGroup($group);
275 $newUser = $this->_em->find(get_class($user), $user->getId());
276 $this->assertEquals(1, count($newUser->groups));
280 * @param int $groupCount
283 public function addCmsUserGblancoWithGroups($groupCount = 1)
286 $user->name = 'Guilherme';
287 $user->username = 'gblanco';
288 $user->status = 'developer';
290 for ($i=0; $i < $groupCount; ++$i) {
291 $group = new CmsGroup;
292 $group->name = 'Developers_' . $i;
293 $user->addGroup($group);
296 $this->_em->persist($user);
299 $this->assertNotNull($user->getId(), "User 'gblanco' should have an ID assigned after the persist()/flush() operation.");
307 public function testUpdateDeleteSizeSubselectQueries()
309 $this->_em->createQuery("DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10")->execute();
310 $this->_em->createQuery("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = 'inactive' WHERE SIZE(u.groups) = 10")->execute();
316 public function testClearAndResetCollection()
318 $user = $this->addCmsUserGblancoWithGroups(2);
319 $group1 = new CmsGroup;
320 $group1->name = 'Developers_New1';
321 $group2 = new CmsGroup;
322 $group2->name = 'Developers_New2';
324 $this->_em->persist($group1);
325 $this->_em->persist($group2);
329 $user = $this->_em->find(get_class($user), $user->id);
331 $coll = new ArrayCollection(array($group1, $group2));
332 $user->groups = $coll;
334 $this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->groups,
335 "UnitOfWork should have replaced ArrayCollection with PersistentCollection.");
340 $user = $this->_em->find(get_class($user), $user->id);
341 $this->assertEquals(2, count($user->groups));
342 $this->assertEquals('Developers_New1', $user->groups[0]->name);
343 $this->assertEquals('Developers_New2', $user->groups[1]->name);
349 public function testInitializePersistentCollection()
351 $user = $this->addCmsUserGblancoWithGroups(2);
354 $user = $this->_em->find(get_class($user), $user->id);
356 $this->assertFalse($user->groups->isInitialized(), "Pre-condition: lazy collection");
357 $this->_em->getUnitOfWork()->initializeObject($user->groups);
358 $this->assertTrue($user->groups->isInitialized(), "Collection should be initialized after calling UnitOfWork::initializeObject()");
365 public function testClearBeforeLazyLoad()
367 $user = $this->addCmsUserGblancoWithGroups(4);
371 $user = $this->_em->find(get_class($user), $user->id);
372 $user->groups->clear();
373 $this->assertEquals(0, count($user->groups));
377 $user = $this->_em->find(get_class($user), $user->id);
378 $this->assertEquals(0, count($user->groups));