2009-05-11 14:43:27 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\Tests\Models\CMS\CmsUser ;
2009-07-25 20:33:29 +04:00
use Doctrine\Tests\Models\CMS\CmsPhonenumber ;
2010-02-20 00:28:17 +03:00
use Doctrine\Tests\Models\CMS\CmsAddress ;
2011-08-21 17:01:57 +04:00
use Doctrine\Tests\Models\CMS\CmsArticle ;
2009-05-11 14:43:27 +04:00
use Doctrine\ORM\UnitOfWork ;
require_once __DIR__ . '/../../TestInit.php' ;
/**
* Description of DetachedEntityTest
*
* @ author robo
*/
class DetachedEntityTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp () {
$this -> useModelSet ( 'cms' );
parent :: setUp ();
}
public function testSimpleDetachMerge () {
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'dev' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-05-11 14:43:27 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
// $user is now detached
$this -> assertFalse ( $this -> _em -> contains ( $user ));
$user -> name = 'Roman B.' ;
//$this->assertEquals(UnitOfWork::STATE_DETACHED, $this->_em->getUnitOfWork()->getEntityState($user));
$user2 = $this -> _em -> merge ( $user );
$this -> assertFalse ( $user === $user2 );
$this -> assertTrue ( $this -> _em -> contains ( $user2 ));
$this -> assertEquals ( 'Roman B.' , $user2 -> name );
}
2010-07-30 00:27:00 +04:00
2009-07-25 20:33:29 +04:00
public function testSerializeUnserializeModifyMerge ()
{
2010-07-30 00:27:00 +04:00
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
2009-07-25 20:33:29 +04:00
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$ph1 = new CmsPhonenumber ;
2010-10-31 09:06:53 +03:00
$ph1 -> phonenumber = " 1234 " ;
2009-07-25 20:33:29 +04:00
$user -> addPhonenumber ( $ph1 );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> assertTrue ( $this -> _em -> contains ( $user ));
2010-07-30 00:27:00 +04:00
$this -> assertTrue ( $user -> phonenumbers -> isInitialized ());
2009-07-25 20:33:29 +04:00
$serialized = serialize ( $user );
$this -> _em -> clear ();
$this -> assertFalse ( $this -> _em -> contains ( $user ));
unset ( $user );
$user = unserialize ( $serialized );
2010-10-31 09:06:53 +03:00
$this -> assertEquals ( 1 , count ( $user -> getPhonenumbers ()), " Pre-Condition: 1 Phonenumber " );
2009-07-25 20:33:29 +04:00
$ph2 = new CmsPhonenumber ;
2010-10-31 09:06:53 +03:00
$ph2 -> phonenumber = " 56789 " ;
2009-07-25 20:33:29 +04:00
$user -> addPhonenumber ( $ph2 );
2010-10-31 09:06:53 +03:00
$oldPhonenumbers = $user -> getPhonenumbers ();
$this -> assertEquals ( 2 , count ( $oldPhonenumbers ), " Pre-Condition: 2 Phonenumbers " );
2009-07-25 20:33:29 +04:00
$this -> assertFalse ( $this -> _em -> contains ( $user ));
$this -> _em -> persist ( $ph2 );
2010-10-31 09:06:53 +03:00
2009-07-25 20:33:29 +04:00
// Merge back in
$user = $this -> _em -> merge ( $user ); // merge cascaded to phonenumbers
2011-02-20 00:50:58 +03:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user -> phonenumbers [ 0 ] -> user );
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user -> phonenumbers [ 1 ] -> user );
2010-10-31 09:06:53 +03:00
$im = $this -> _em -> getUnitOfWork () -> getIdentityMap ();
2009-07-25 20:33:29 +04:00
$this -> _em -> flush ();
2010-10-31 09:06:53 +03:00
$this -> assertTrue ( $this -> _em -> contains ( $user ), " Failed to assert that merged user is contained inside EntityManager persistence context. " );
2009-07-25 20:33:29 +04:00
$phonenumbers = $user -> getPhonenumbers ();
2010-10-31 09:06:53 +03:00
$this -> assertNotSame ( $oldPhonenumbers , $phonenumbers , " Merge should replace the Detached Collection with a new PersistentCollection. " );
$this -> assertEquals ( 2 , count ( $phonenumbers ), " Failed to assert that two phonenumbers are contained in the merged users phonenumber collection. " );
2011-02-20 00:50:58 +03:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsPhonenumber' , $phonenumbers [ 1 ]);
2010-10-31 09:06:53 +03:00
$this -> assertTrue ( $this -> _em -> contains ( $phonenumbers [ 1 ]), " Failed to assert that second phonenumber in collection is contained inside EntityManager persistence context. " );
2011-02-20 00:50:58 +03:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsPhonenumber' , $phonenumbers [ 0 ]);
2010-10-31 09:06:53 +03:00
$this -> assertTrue ( $this -> _em -> getUnitOfWork () -> isInIdentityMap ( $phonenumbers [ 0 ]));
$this -> assertTrue ( $this -> _em -> contains ( $phonenumbers [ 0 ]), " Failed to assert that first phonenumber in collection is contained inside EntityManager persistence context. " );
2010-05-11 01:51:56 +04:00
}
2010-07-04 20:55:49 +04:00
/**
* @ group DDC - 203
*/
2010-07-08 02:20:54 +04:00
public function testDetachedEntityThrowsExceptionOnFlush ()
2010-07-04 20:55:49 +04:00
{
$ph = new CmsPhonenumber ();
$ph -> phonenumber = '12345' ;
$this -> _em -> persist ( $ph );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> _em -> persist ( $ph );
2010-07-08 02:20:54 +04:00
try {
$this -> _em -> flush ();
$this -> fail ();
} catch ( \Exception $expected ) {}
2010-07-04 20:55:49 +04:00
}
2010-09-30 23:59:01 +04:00
public function testUninitializedLazyAssociationsAreIgnoredOnMerge ()
{
2010-05-11 01:51:56 +04:00
$user = new CmsUser ;
2010-07-30 00:27:00 +04:00
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
2010-05-11 01:51:56 +04:00
2010-07-30 00:27:00 +04:00
$address = new CmsAddress ;
$address -> city = 'Berlin' ;
$address -> country = 'Germany' ;
$address -> street = 'Sesamestreet' ;
$address -> zip = 12345 ;
$address -> setUser ( $user );
$this -> _em -> persist ( $address );
$this -> _em -> persist ( $user );
2010-05-11 01:51:56 +04:00
$this -> _em -> flush ();
2010-07-30 00:27:00 +04:00
$this -> _em -> clear ();
2010-05-11 01:51:56 +04:00
2010-07-30 00:27:00 +04:00
$address2 = $this -> _em -> find ( get_class ( $address ), $address -> id );
2011-07-26 17:22:57 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $address2 -> user );
2010-07-30 00:27:00 +04:00
$this -> assertFalse ( $address2 -> user -> __isInitialized__ );
$detachedAddress2 = unserialize ( serialize ( $address2 ));
2011-07-26 17:22:57 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $detachedAddress2 -> user );
2010-07-30 00:27:00 +04:00
$this -> assertFalse ( $detachedAddress2 -> user -> __isInitialized__ );
$managedAddress2 = $this -> _em -> merge ( $detachedAddress2 );
2011-07-26 17:22:57 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $managedAddress2 -> user );
2010-07-30 00:27:00 +04:00
$this -> assertFalse ( $managedAddress2 -> user === $detachedAddress2 -> user );
$this -> assertFalse ( $managedAddress2 -> user -> __isInitialized__ );
}
2010-09-30 23:59:01 +04:00
/**
* @ group DDC - 822
*/
public function testUseDetachedEntityAsQueryParameter ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> detach ( $user );
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " ;
$query = $this -> _em -> createQuery ( $dql );
$query -> setParameter ( 1 , $user );
$newUser = $query -> getSingleResult ();
2011-02-20 00:50:58 +03:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $newUser );
2010-09-30 23:59:01 +04:00
$this -> assertEquals ( 'gblanco' , $newUser -> username );
}
2010-12-10 23:55:48 +03:00
/**
* @ group DDC - 920
*/
public function testDetachManagedUnpersistedEntity ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$this -> _em -> detach ( $user );
$this -> _em -> flush ();
2010-12-12 17:43:12 +03:00
$this -> assertFalse ( $this -> _em -> contains ( $user ));
$this -> assertFalse ( $this -> _em -> getUnitOfWork () -> isInIdentityMap ( $user ));
2010-12-10 23:55:48 +03:00
}
2011-08-21 17:01:57 +04:00
/**
* @ group DDC - 1340
*/
public function testMergeArticleWrongVersion ()
{
$article = new CmsArticle ();
$article -> topic = " test " ;
$article -> text = " test " ;
$this -> _em -> persist ( $article );
$this -> _em -> flush ();
$this -> _em -> detach ( $article );
$sql = " UPDATE cms_articles SET version = version+1 WHERE id = " . $article -> id ;
$this -> _em -> getConnection () -> executeUpdate ( $sql );
$this -> setExpectedException ( 'Doctrine\ORM\OptimisticLockException' , 'The optimistic lock failed, version 1 was expected, but is actually 2' );
$this -> _em -> merge ( $article );
}
2009-05-11 14:43:27 +04:00
}