2010-07-02 01:48:44 +04:00
|
|
|
<?php
|
|
|
|
namespace Doctrine\Tests\ORM\Functional\Ticket;
|
|
|
|
|
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsGroup;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
|
|
|
|
use Doctrine\Tests\Models\CMS\CmsAddress;
|
|
|
|
use Doctrine\ORM\UnitOfWork;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../../../TestInit.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ----------------- !! NOTE !! --------------------
|
|
|
|
* To reproduce the manyToMany-Bug it's necessary
|
|
|
|
* to cascade "merge" on cmUser::groups
|
|
|
|
* -------------------------------------------------
|
|
|
|
*
|
|
|
|
* @PHP-Version 5.3.2
|
|
|
|
* @PHPUnit-Version 3.4.11
|
|
|
|
*
|
|
|
|
* @author markus
|
|
|
|
*/
|
2010-07-02 01:58:46 +04:00
|
|
|
class DDC501Test extends OrmFunctionalTestCase
|
|
|
|
{
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2010-07-02 01:48:44 +04:00
|
|
|
$this->useModelSet('cms');
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
public function testMergeUnitializedManyToManyAndOneToManyCollections()
|
2010-07-02 01:48:44 +04:00
|
|
|
{
|
2010-07-02 01:58:46 +04:00
|
|
|
// Create User
|
2010-07-02 01:48:44 +04:00
|
|
|
$user = $this->createAndPersistUser();
|
|
|
|
$this->_em->flush();
|
|
|
|
|
|
|
|
$this->assertTrue($this->_em->contains($user));
|
|
|
|
$this->_em->clear();
|
|
|
|
$this->assertFalse($this->_em->contains($user));
|
|
|
|
|
|
|
|
unset($user);
|
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
// Reload User from DB *without* any associations (i.e. an uninitialized PersistantCollection)
|
2010-07-02 01:48:44 +04:00
|
|
|
$userReloaded = $this->loadUserFromEntityManager();
|
|
|
|
|
|
|
|
$this->assertTrue($this->_em->contains($userReloaded));
|
|
|
|
$this->_em->clear();
|
|
|
|
$this->assertFalse($this->_em->contains($userReloaded));
|
|
|
|
|
|
|
|
// freeze and unfreeze
|
|
|
|
$userClone = unserialize(serialize($userReloaded));
|
|
|
|
$this->assertType('Doctrine\Tests\Models\CMS\CmsUser', $userClone);
|
|
|
|
|
|
|
|
// detached user can't know about his phonenumbers
|
|
|
|
$this->assertEquals(0, count($userClone->getPhonenumbers()));
|
2010-07-02 01:58:46 +04:00
|
|
|
$this->assertFalse($userClone->getPhonenumbers()->isInitialized(), "User::phonenumbers should not be marked initialized.");
|
2010-07-02 01:48:44 +04:00
|
|
|
|
|
|
|
// detached user can't know about his groups either
|
|
|
|
$this->assertEquals(0, count($userClone->getGroups()));
|
2010-07-02 01:58:46 +04:00
|
|
|
$this->assertFalse($userClone->getGroups()->isInitialized(), "User::groups should not be marked initialized.");
|
2010-07-02 01:48:44 +04:00
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
// Merge back and flush
|
2010-07-02 01:48:44 +04:00
|
|
|
$userClone = $this->_em->merge($userClone);
|
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
// Back in managed world I would expect to have my phonenumbers back but they aren't!
|
|
|
|
// Remember I didn't touch (and propably didn't need) them at all while in detached mode.
|
2010-07-02 01:48:44 +04:00
|
|
|
$this->assertEquals(4, count($userClone->getPhonenumbers()), 'Phonenumbers are not available anymore');
|
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
// This works fine as long as cmUser::groups doesn't cascade "merge"
|
2010-07-02 01:48:44 +04:00
|
|
|
$this->assertEquals(2, count($userClone->getGroups()));
|
|
|
|
|
|
|
|
$this->_em->flush();
|
|
|
|
$this->_em->clear();
|
|
|
|
|
|
|
|
$this->assertFalse($this->_em->contains($userClone));
|
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
// Reload user from DB
|
2010-07-02 01:48:44 +04:00
|
|
|
$userFromEntityManager = $this->loadUserFromEntityManager();
|
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
//Strange: Now the phonenumbers are back again
|
2010-07-02 01:48:44 +04:00
|
|
|
$this->assertEquals(4, count($userFromEntityManager->getPhonenumbers()));
|
|
|
|
|
2010-07-02 01:58:46 +04:00
|
|
|
// This works fine as long as cmUser::groups doesn't cascade "merge"
|
|
|
|
// Otherwise group memberships are physically deleted now!
|
2010-07-02 01:48:44 +04:00
|
|
|
$this->assertEquals(2, count($userClone->getGroups()));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createAndPersistUser()
|
|
|
|
{
|
|
|
|
$user = new CmsUser();
|
|
|
|
$user->name = 'Luka';
|
|
|
|
$user->username = 'lukacho';
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Doctrine\Tests\Models\CMS\CmsUser
|
|
|
|
*/
|
|
|
|
protected function loadUserFromEntityManager()
|
|
|
|
{
|
|
|
|
return $this->_em
|
|
|
|
->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name like :name')
|
|
|
|
->setParameter('name', 'Luka')
|
|
|
|
->getSingleResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|