/** * This test reproduces DDC-1734 which is: * - A non-initialized proxy is detached and serialized (the identifier of the proxy is *not* serialized) * - the object is deserialized and merged (to turn into an entity) * - the entity is broken because it has no identifier and no field defined * * @group DDC-1734 */ public function testMergeWorksOnSerializedProxies() { $group = new CmsGroup(); $group->setName('Foo'); $this->_em->persist($group); $this->_em->flush(); $this->_em->clear(); $proxy = $this->getProxy($group); $this->assertInstanceOf('Doctrine\\ORM\\Proxy\\Proxy', $proxy); $this->assertFalse($proxy->__isInitialized()); $this->_em->detach($proxy); $serializedProxy = serialize($proxy); $this->_em->clear(); $unserializedProxy = $this->_em->merge(unserialize($serializedProxy)); $this->assertEquals('Foo', $unserializedProxy->getName(), 'The entity is broken'); }
protected function createAndPersistUser() { $user = new CmsUser(); $user->name = 'Luka'; $user->username = '******'; $user->status = 'developer'; foreach (array(1111, 2222, 3333, 4444) as $number) { $phone = new CmsPhonenumber(); $phone->phonenumber = $number; $user->addPhonenumber($phone); } foreach (array('Moshers', 'Headbangers') as $groupName) { $group = new CmsGroup(); $group->setName($groupName); $user->addGroup($group); } $this->_em->persist($user); return $user; }
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())); }
public function testClearingCollectionDoesNotInitialize() { $user = new CmsUser(); $user->username = "******"; $user->name = "Benjamin E."; $user->status = 'active'; $grp = new CmsGroup(); $grp->setName("The Dudes"); $grp->addUser($user); $user->addGroup($grp); $this->_em->persist($user); $this->_em->persist($grp); $this->_em->flush(); $this->_em->clear(); $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users_groups")); $user2 = $this->_em->find(get_class($user), $user->id); $this->assertFalse($user2->groups->isInitialized()); $user2->groups->clear(); $this->assertFalse($user2->groups->isInitialized()); $this->_em->flush(); $this->assertFalse($user2->groups->isInitialized()); $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users_groups")); }
public function testBasicManyToManyJoin() { $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $group1 = new CmsGroup(); $group1->setName('Doctrine Developers'); $user->addGroup($group1); $this->_em->persist($user); $this->_em->flush(); $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->assertTrue($result[0] instanceof CmsUser); $this->assertEquals('Guilherme', $result[0]->name); $this->assertEquals(1, $result[0]->getGroups()->count()); $groups = $result[0]->getGroups(); $this->assertEquals('Doctrine Developers', $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->assertTrue($groups instanceof \Doctrine\ORM\PersistentCollection); $this->assertTrue($groups[0]->getUsers() instanceof \Doctrine\ORM\PersistentCollection); $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 testRepeatedFlush() { $user1 = new CmsUser(); $user1->username = '******'; $user2 = new CmsTeamUser(); $user2->username = '******'; $user2->parent = $user1; $user3 = new CmsTeamUser(); $user3->username = '******'; $user3->parent = $user2; $group = new CmsGroup(); $group->id = '/functional/group'; $group->setName('foo'); $group->addUser($user1); $group->addUser($user2); $group->addUser($user3); $this->dm->persist($group); $this->assertCount(3, $group->getUsers()); $this->dm->flush(); $user4 = new CmsTeamUser(); $user4->username = '******'; $user4->parent = $user1; $group->addUser($user4); $this->assertCount(4, $group->getUsers()); $this->dm->flush(); $this->dm->getPhpcrSession()->removeItem($user2->id); $this->dm->getPhpcrSession()->save(); $this->dm->flush(); $this->assertInstanceOf('\\PHPCR\\NodeInterface', $user1->node); $this->assertCount(4, $group->getUsers()); $this->dm->clear(); $group = $this->dm->find(null, '/functional/group'); $group->getUsers()->first(); $this->assertCount(2, $group->getUsers()); $this->assertInstanceOf('\\PHPCR\\NodeInterface', $group->getUsers()->first()->node); }
public function testBasicManyToManyJoin() { $user = new CmsUser(); $user->name = 'Guilherme'; $user->username = '******'; $user->status = 'developer'; $group1 = new CmsGroup(); $group1->setName('Doctrine Developers'); $user->addGroup($group1); $this->_em->save($user); $this->_em->save($group1); $this->_em->flush(); $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->getResultList(); $this->assertEquals(2, $this->_em->getUnitOfWork()->size()); $this->assertTrue($result[0] instanceof CmsUser); $this->assertEquals('Guilherme', $result[0]->name); $this->assertEquals(1, $result[0]->getGroups()->count()); $groups = $result[0]->getGroups(); $this->assertEquals('Doctrine Developers', $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->assertTrue($groups instanceof \Doctrine\ORM\PersistentCollection); $this->assertTrue($groups[0]->getUsers() instanceof \Doctrine\ORM\PersistentCollection); $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 inner join u.groups g"); $this->assertEquals(0, count($query->getResultList())); /* RB: TEST */ /* $address = new CmsAddress; $address->country = 'Germany'; $address->zip = '103040'; $address->city = 'Berlin'; $address->user = $user; $this->_em->save($address); $this->_em->clear(); $proxy = $this->_em->getProxyGenerator()->getAssociationProxy($user, $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser')->getAssociationMapping('address')); var_dump($proxy->getId()); //var_dump(get_class($proxy)); var_dump(get_class($proxy->user)); //var_dump($proxy); //$proxy = $this->_em->getProxyGenerator()->getReferenceProxy('Doctrine\Tests\Models\CMS\CmsUser', 1); //echo $proxy->getId(); //var_dump(serialize($proxy)); */ }