2009-01-07 20:46:02 +03:00
< ? php
2009-01-22 22:38:10 +03:00
namespace Doctrine\Tests\ORM\Functional ;
2009-03-30 23:43:05 +04:00
use Doctrine\ORM\Tools\SchemaTool ;
2010-10-12 00:15:18 +04:00
use Doctrine\ORM\Query ;
2009-01-22 22:38:10 +03:00
use Doctrine\Tests\Models\CMS\CmsUser ;
use Doctrine\Tests\Models\CMS\CmsPhonenumber ;
2009-02-02 14:55:50 +03:00
use Doctrine\Tests\Models\CMS\CmsAddress ;
2009-02-06 20:16:39 +03:00
use Doctrine\Tests\Models\CMS\CmsGroup ;
2010-07-08 02:20:54 +04:00
use Doctrine\Tests\Models\CMS\CmsArticle ;
use Doctrine\Tests\Models\CMS\CmsComment ;
2009-01-22 22:38:10 +03:00
2009-01-24 19:56:44 +03:00
require_once __DIR__ . '/../../TestInit.php' ;
2009-01-07 20:46:02 +03:00
2009-03-30 23:43:05 +04:00
class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
2009-02-18 10:59:11 +03:00
{
2009-03-30 23:43:05 +04:00
protected function setUp ()
2009-02-18 10:59:11 +03:00
{
2009-03-30 23:43:05 +04:00
$this -> useModelSet ( 'cms' );
parent :: setUp ();
2009-03-28 23:59:07 +03:00
}
2009-01-07 20:46:02 +03:00
2009-03-28 23:59:07 +03:00
public function testBasicUnitsOfWorkWithOneToManyAssociation ()
{
2009-01-07 20:46:02 +03:00
// Create
$user = new CmsUser ;
2009-02-02 14:55:50 +03:00
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'developer' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-07-18 15:41:37 +04:00
2009-07-19 20:54:53 +04:00
$this -> _em -> flush ();
2009-01-07 20:46:02 +03:00
$this -> assertTrue ( is_numeric ( $user -> id ));
2009-03-28 23:59:07 +03:00
$this -> assertTrue ( $this -> _em -> contains ( $user ));
2009-01-07 20:46:02 +03:00
// Read
2009-03-28 23:59:07 +03:00
$user2 = $this -> _em -> find ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user -> id );
2009-01-09 19:25:06 +03:00
$this -> assertTrue ( $user === $user2 );
2009-01-08 14:23:24 +03:00
2009-01-09 19:25:06 +03:00
// Add a phonenumber
2009-01-08 14:23:24 +03:00
$ph = new CmsPhonenumber ;
$ph -> phonenumber = " 12345 " ;
2009-01-09 19:25:06 +03:00
$user -> addPhonenumber ( $ph );
2009-03-28 23:59:07 +03:00
$this -> _em -> flush ();
$this -> assertTrue ( $this -> _em -> contains ( $ph ));
$this -> assertTrue ( $this -> _em -> contains ( $user ));
2011-07-26 17:22:57 +04:00
//$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->phonenumbers);
2009-01-08 14:23:24 +03:00
2009-01-29 20:00:44 +03:00
// Update name
2009-01-09 19:25:06 +03:00
$user -> name = 'guilherme' ;
2009-03-28 23:59:07 +03:00
$this -> _em -> flush ();
2009-01-09 19:25:06 +03:00
$this -> assertEquals ( 'guilherme' , $user -> name );
2009-01-29 20:00:44 +03:00
// Add another phonenumber
$ph2 = new CmsPhonenumber ;
$ph2 -> phonenumber = " 6789 " ;
$user -> addPhonenumber ( $ph2 );
2009-03-28 23:59:07 +03:00
$this -> _em -> flush ();
$this -> assertTrue ( $this -> _em -> contains ( $ph2 ));
2009-01-29 20:00:44 +03:00
2009-01-09 19:25:06 +03:00
// Delete
2009-07-19 20:54:53 +04:00
$this -> _em -> remove ( $user );
$this -> assertTrue ( $this -> _em -> getUnitOfWork () -> isScheduledForDelete ( $user ));
$this -> assertTrue ( $this -> _em -> getUnitOfWork () -> isScheduledForDelete ( $ph ));
$this -> assertTrue ( $this -> _em -> getUnitOfWork () -> isScheduledForDelete ( $ph2 ));
2009-03-28 23:59:07 +03:00
$this -> _em -> flush ();
2009-07-19 20:54:53 +04:00
$this -> assertFalse ( $this -> _em -> getUnitOfWork () -> isScheduledForDelete ( $user ));
$this -> assertFalse ( $this -> _em -> getUnitOfWork () -> isScheduledForDelete ( $ph ));
$this -> assertFalse ( $this -> _em -> getUnitOfWork () -> isScheduledForDelete ( $ph2 ));
2009-11-11 19:20:29 +03:00
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_NEW , $this -> _em -> getUnitOfWork () -> getEntityState ( $user ));
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_NEW , $this -> _em -> getUnitOfWork () -> getEntityState ( $ph ));
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_NEW , $this -> _em -> getUnitOfWork () -> getEntityState ( $ph2 ));
2009-01-09 19:25:06 +03:00
}
2009-01-08 14:23:24 +03:00
2009-02-18 10:59:11 +03:00
public function testOneToManyAssociationModification ()
{
2009-02-02 14:55:50 +03:00
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'developer' ;
2009-01-12 16:34:41 +03:00
2009-02-02 14:55:50 +03:00
$ph1 = new CmsPhonenumber ;
$ph1 -> phonenumber = " 0301234 " ;
$ph2 = new CmsPhonenumber ;
$ph2 -> phonenumber = " 987654321 " ;
$user -> addPhonenumber ( $ph1 );
$user -> addPhonenumber ( $ph2 );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-02-02 14:55:50 +03:00
$this -> _em -> flush ();
2011-07-26 17:22:57 +04:00
//$this->assertInstanceOf('Doctrine\ORM\PersistentCollection', $user->phonenumbers);
2009-02-02 14:55:50 +03:00
// Remove the first element from the collection
unset ( $user -> phonenumbers [ 0 ]);
$ph1 -> user = null ; // owning side!
2009-01-12 16:34:41 +03:00
2009-02-02 14:55:50 +03:00
$this -> _em -> flush ();
$this -> assertEquals ( 1 , count ( $user -> phonenumbers ));
$this -> assertNull ( $ph1 -> user );
}
public function testBasicOneToOne ()
{
2010-08-09 15:13:21 +04:00
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
2009-02-02 14:55:50 +03:00
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'developer' ;
2009-01-12 16:34:41 +03:00
2009-02-02 14:55:50 +03:00
$address = new CmsAddress ;
$address -> country = 'Germany' ;
$address -> city = 'Berlin' ;
$address -> zip = '12345' ;
$user -> address = $address ; // inverse side
$address -> user = $user ; // owning side!
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-01-12 16:34:41 +03:00
$this -> _em -> flush ();
2009-02-02 14:55:50 +03:00
// Check that the foreign key has been set
2010-04-01 01:13:34 +04:00
$userId = $this -> _em -> getConnection () -> executeQuery (
" SELECT user_id FROM cms_addresses WHERE id=? " , array ( $address -> id )
) -> fetchColumn ();
2009-02-02 14:55:50 +03:00
$this -> assertTrue ( is_numeric ( $userId ));
2010-03-29 17:20:41 +04:00
$this -> _em -> clear ();
2010-08-08 17:01:03 +04:00
$user2 = $this -> _em -> createQuery ( 'select u from \Doctrine\Tests\Models\CMS\CmsUser u where u.id=?1' )
2010-03-29 17:20:41 +04:00
-> setParameter ( 1 , $userId )
-> getSingleResult ();
// Address has been eager-loaded because it cant be lazy
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $user2 -> address );
$this -> assertNotInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $user2 -> address );
2009-02-02 14:55:50 +03:00
}
2009-07-21 19:53:58 +04:00
2011-06-28 23:37:53 +04:00
/**
* @ group DDC - 1230
*/
public function testRemove ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
2011-08-28 17:57:33 +04:00
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_NEW , $this -> _em -> getUnitOfWork () -> getEntityState ( $user ), " State should be UnitOfWork::STATE_NEW " );
2011-06-28 23:37:53 +04:00
$this -> _em -> persist ( $user );
2011-08-28 17:57:33 +04:00
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_MANAGED , $this -> _em -> getUnitOfWork () -> getEntityState ( $user ), " State should be UnitOfWork::STATE_MANAGED " );
2011-06-28 23:37:53 +04:00
$this -> _em -> remove ( $user );
2011-08-28 17:57:33 +04:00
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_NEW , $this -> _em -> getUnitOfWork () -> getEntityState ( $user ), " State should be UnitOfWork::STATE_NEW " );
2011-06-28 23:37:53 +04:00
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$id = $user -> getId ();
$this -> _em -> remove ( $user );
2011-08-28 17:57:33 +04:00
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_REMOVED , $this -> _em -> getUnitOfWork () -> getEntityState ( $user ), " State should be UnitOfWork::STATE_REMOVED " );
2011-06-28 23:37:53 +04:00
$this -> _em -> flush ();
2011-08-28 17:57:33 +04:00
$this -> assertEquals ( \Doctrine\ORM\UnitOfWork :: STATE_NEW , $this -> _em -> getUnitOfWork () -> getEntityState ( $user ), " State should be UnitOfWork::STATE_NEW " );
2011-06-28 23:37:53 +04:00
$this -> assertNull ( $this -> _em -> find ( 'Doctrine\Tests\Models\CMS\CmsUser' , $id ));
}
2009-07-23 13:52:16 +04:00
public function testOneToManyOrphanRemoval ()
2009-07-21 19:53:58 +04:00
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
for ( $i = 0 ; $i < 3 ; ++ $i ) {
$phone = new CmsPhonenumber ;
$phone -> phonenumber = 100 + $i ;
$user -> addPhonenumber ( $phone );
}
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$user -> getPhonenumbers () -> remove ( 0 );
2009-07-23 13:52:16 +04:00
$this -> assertEquals ( 2 , count ( $user -> getPhonenumbers ()));
2009-07-21 19:53:58 +04:00
$this -> _em -> flush ();
2009-07-23 13:52:16 +04:00
// Check that there are just 2 phonenumbers left
2010-07-08 19:30:39 +04:00
$count = $this -> _em -> getConnection () -> fetchColumn ( " SELECT COUNT(*) FROM cms_phonenumbers " );
2009-07-21 19:53:58 +04:00
$this -> assertEquals ( 2 , $count ); // only 2 remaining
2010-07-08 19:30:39 +04:00
// check that clear() removes the others via orphan removal
$user -> getPhonenumbers () -> clear ();
$this -> _em -> flush ();
$this -> assertEquals ( 0 , $this -> _em -> getConnection () -> fetchColumn ( " select count(*) from cms_phonenumbers " ));
2009-07-23 13:52:16 +04:00
}
2009-03-28 23:59:07 +03:00
public function testBasicQuery ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-03-28 23:59:07 +03:00
$this -> _em -> flush ();
$query = $this -> _em -> createQuery ( " select u from Doctrine \T ests \ Models \ CMS \ CmsUser u " );
2009-08-03 21:18:37 +04:00
$users = $query -> getResult ();
2009-03-28 23:59:07 +03:00
2009-08-03 17:25:56 +04:00
$this -> assertEquals ( 1 , count ( $users ));
2009-03-28 23:59:07 +03:00
$this -> assertEquals ( 'Guilherme' , $users [ 0 ] -> name );
$this -> assertEquals ( 'gblanco' , $users [ 0 ] -> username );
$this -> assertEquals ( 'developer' , $users [ 0 ] -> status );
2009-05-13 19:19:27 +04:00
//$this->assertNull($users[0]->phonenumbers);
//$this->assertNull($users[0]->articles);
2009-03-30 23:43:05 +04:00
2009-08-03 21:18:37 +04:00
$usersArray = $query -> getArrayResult ();
2009-03-30 23:43:05 +04:00
$this -> assertTrue ( is_array ( $usersArray ));
$this -> assertEquals ( 1 , count ( $usersArray ));
$this -> assertEquals ( 'Guilherme' , $usersArray [ 0 ][ 'name' ]);
$this -> assertEquals ( 'gblanco' , $usersArray [ 0 ][ 'username' ]);
$this -> assertEquals ( 'developer' , $usersArray [ 0 ][ 'status' ]);
$usersScalar = $query -> getScalarResult ();
$this -> assertTrue ( is_array ( $usersScalar ));
$this -> assertEquals ( 1 , count ( $usersScalar ));
$this -> assertEquals ( 'Guilherme' , $usersScalar [ 0 ][ 'u_name' ]);
$this -> assertEquals ( 'gblanco' , $usersScalar [ 0 ][ 'u_username' ]);
$this -> assertEquals ( 'developer' , $usersScalar [ 0 ][ 'u_status' ]);
2009-03-28 23:59:07 +03:00
}
2009-05-07 17:54:01 +04:00
public function testBasicOneToManyInnerJoin ()
2009-03-28 23:59:07 +03:00
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-03-28 23:59:07 +03:00
$this -> _em -> flush ();
$query = $this -> _em -> createQuery ( " select u from Doctrine \T ests \ Models \ CMS \ CmsUser u join u.phonenumbers p " );
2009-08-03 21:18:37 +04:00
$users = $query -> getResult ();
2009-03-28 23:59:07 +03:00
2009-08-03 17:25:56 +04:00
$this -> assertEquals ( 0 , count ( $users ));
2009-03-28 23:59:07 +03:00
}
2009-05-07 17:54:01 +04:00
public function testBasicOneToManyLeftJoin ()
2009-03-28 23:59:07 +03:00
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
2009-03-28 23:59:07 +03:00
$this -> _em -> flush ();
$query = $this -> _em -> createQuery ( " select u,p from Doctrine \T ests \ Models \ CMS \ CmsUser u left join u.phonenumbers p " );
2009-08-03 21:18:37 +04:00
$users = $query -> getResult ();
2009-03-28 23:59:07 +03:00
2009-08-03 17:25:56 +04:00
$this -> assertEquals ( 1 , count ( $users ));
2009-03-28 23:59:07 +03:00
$this -> assertEquals ( 'Guilherme' , $users [ 0 ] -> name );
$this -> assertEquals ( 'gblanco' , $users [ 0 ] -> username );
$this -> assertEquals ( 'developer' , $users [ 0 ] -> status );
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\PersistentCollection' , $users [ 0 ] -> phonenumbers );
2010-02-20 00:28:17 +03:00
$this -> assertTrue ( $users [ 0 ] -> phonenumbers -> isInitialized ());
2009-03-28 23:59:07 +03:00
$this -> assertEquals ( 0 , $users [ 0 ] -> phonenumbers -> count ());
2009-05-13 19:19:27 +04:00
//$this->assertNull($users[0]->articles);
2009-03-28 23:59:07 +03:00
}
2009-07-21 19:53:58 +04:00
public function testBasicRefresh ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$user -> status = 'mascot' ;
$this -> assertEquals ( 'mascot' , $user -> status );
$this -> _em -> refresh ( $user );
$this -> assertEquals ( 'developer' , $user -> status );
}
2010-10-12 00:15:18 +04:00
/**
* @ group DDC - 833
*/
public function testRefreshResetsCollection ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
// Add a phonenumber
$ph1 = new CmsPhonenumber ;
$ph1 -> phonenumber = " 12345 " ;
$user -> addPhonenumber ( $ph1 );
// Add a phonenumber
$ph2 = new CmsPhonenumber ;
$ph2 -> phonenumber = " 54321 " ;
$this -> _em -> persist ( $user );
$this -> _em -> persist ( $ph1 );
$this -> _em -> persist ( $ph2 );
$this -> _em -> flush ();
$user -> addPhonenumber ( $ph2 );
$this -> assertEquals ( 2 , count ( $user -> phonenumbers ));
$this -> _em -> refresh ( $user );
$this -> assertEquals ( 1 , count ( $user -> phonenumbers ));
}
/**
* @ group DDC - 833
*/
public function testDqlRefreshResetsCollection ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
// Add a phonenumber
$ph1 = new CmsPhonenumber ;
$ph1 -> phonenumber = " 12345 " ;
$user -> addPhonenumber ( $ph1 );
// Add a phonenumber
$ph2 = new CmsPhonenumber ;
$ph2 -> phonenumber = " 54321 " ;
$this -> _em -> persist ( $user );
$this -> _em -> persist ( $ph1 );
$this -> _em -> persist ( $ph2 );
$this -> _em -> flush ();
$user -> addPhonenumber ( $ph2 );
$this -> assertEquals ( 2 , count ( $user -> phonenumbers ));
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " ;
$user = $this -> _em -> createQuery ( $dql )
-> setParameter ( 1 , $user -> id )
-> setHint ( Query :: HINT_REFRESH , true )
-> getSingleResult ();
$this -> assertEquals ( 1 , count ( $user -> phonenumbers ));
}
/**
* @ group DDC - 833
*/
public function testCreateEntityOfProxy ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
// Add a phonenumber
$ph1 = new CmsPhonenumber ;
$ph1 -> phonenumber = " 12345 " ;
$user -> addPhonenumber ( $ph1 );
// Add a phonenumber
$ph2 = new CmsPhonenumber ;
$ph2 -> phonenumber = " 54321 " ;
$this -> _em -> persist ( $user );
$this -> _em -> persist ( $ph1 );
$this -> _em -> persist ( $ph2 );
$this -> _em -> flush ();
$this -> _em -> clear ();
$userId = $user -> id ;
$user = $this -> _em -> getReference ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user -> id );
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " ;
$user = $this -> _em -> createQuery ( $dql )
-> setParameter ( 1 , $userId )
-> getSingleResult ();
$this -> assertEquals ( 1 , count ( $user -> phonenumbers ));
}
2009-10-07 16:39:46 +04:00
public function testAddToCollectionDoesNotInitialize ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
for ( $i = 0 ; $i < 3 ; ++ $i ) {
$phone = new CmsPhonenumber ;
$phone -> phonenumber = 100 + $i ;
$user -> addPhonenumber ( $phone );
}
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertEquals ( 3 , $user -> getPhonenumbers () -> count ());
$query = $this -> _em -> createQuery ( " select u from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.username='gblanco' " );
$gblanco = $query -> getSingleResult ();
$this -> assertFalse ( $gblanco -> getPhonenumbers () -> isInitialized ());
$newPhone = new CmsPhonenumber ;
2009-11-07 14:41:16 +03:00
$newPhone -> phonenumber = 555 ;
$gblanco -> addPhonenumber ( $newPhone );
2009-10-07 16:39:46 +04:00
$this -> assertFalse ( $gblanco -> getPhonenumbers () -> isInitialized ());
2009-11-08 14:07:49 +03:00
$this -> _em -> persist ( $gblanco );
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select u, p from Doctrine \T ests \ Models \ CMS \ CmsUser u join u.phonenumbers p where u.username='gblanco' " );
$gblanco2 = $query -> getSingleResult ();
$this -> assertEquals ( 4 , $gblanco2 -> getPhonenumbers () -> count ());
}
public function testInitializeCollectionWithNewObjectsRetainsNewObjects ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
for ( $i = 0 ; $i < 3 ; ++ $i ) {
$phone = new CmsPhonenumber ;
$phone -> phonenumber = 100 + $i ;
$user -> addPhonenumber ( $phone );
}
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertEquals ( 3 , $user -> getPhonenumbers () -> count ());
$query = $this -> _em -> createQuery ( " select u from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.username='gblanco' " );
$gblanco = $query -> getSingleResult ();
$this -> assertFalse ( $gblanco -> getPhonenumbers () -> isInitialized ());
$newPhone = new CmsPhonenumber ;
$newPhone -> phonenumber = 555 ;
$gblanco -> addPhonenumber ( $newPhone );
$this -> assertFalse ( $gblanco -> getPhonenumbers () -> isInitialized ());
$this -> assertEquals ( 4 , $gblanco -> getPhonenumbers () -> count ());
$this -> assertTrue ( $gblanco -> getPhonenumbers () -> isInitialized ());
2009-10-15 18:39:43 +04:00
2009-10-07 16:39:46 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select u, p from Doctrine \T ests \ Models \ CMS \ CmsUser u join u.phonenumbers p where u.username='gblanco' " );
$gblanco2 = $query -> getSingleResult ();
$this -> assertEquals ( 4 , $gblanco2 -> getPhonenumbers () -> count ());
}
public function testSetSetAssociationWithGetReference ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$address = new CmsAddress ;
$address -> country = 'Germany' ;
$address -> city = 'Berlin' ;
$address -> zip = '12345' ;
$this -> _em -> persist ( $address );
$this -> _em -> flush ();
$this -> _em -> detach ( $address );
$this -> assertFalse ( $this -> _em -> contains ( $address ));
$this -> assertTrue ( $this -> _em -> contains ( $user ));
// Assume we only got the identifier of the address and now want to attach
// that address to the user without actually loading it, using getReference().
$addressRef = $this -> _em -> getReference ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $address -> getId ());
2009-10-15 18:39:43 +04:00
//$addressRef->getId();
//\Doctrine\Common\Util\Debug::dump($addressRef);
$user -> setAddress ( $addressRef ); // Ugh! Initializes address 'cause of $address->setUser($user)!
2009-10-07 16:39:46 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
// Check with a fresh load that the association is indeed there
$query = $this -> _em -> createQuery ( " select u, a from Doctrine \T ests \ Models \ CMS \ CmsUser u join u.address a where u.username='gblanco' " );
$gblanco = $query -> getSingleResult ();
2009-10-15 18:39:43 +04:00
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $gblanco );
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $gblanco -> getAddress ());
2009-10-07 16:39:46 +04:00
$this -> assertEquals ( 'Berlin' , $gblanco -> getAddress () -> getCity ());
}
2009-11-13 22:34:10 +03:00
public function testOneToManyCascadeRemove ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
for ( $i = 0 ; $i < 3 ; ++ $i ) {
$phone = new CmsPhonenumber ;
$phone -> phonenumber = 100 + $i ;
$user -> addPhonenumber ( $phone );
}
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select u from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.username='gblanco' " );
$gblanco = $query -> getSingleResult ();
$this -> _em -> remove ( $gblanco );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertEquals ( 0 , $this -> _em -> createQuery (
" select count(p.phonenumber) from Doctrine \T ests \ Models \ CMS \ CmsPhonenumber p " )
-> getSingleScalarResult ());
$this -> assertEquals ( 0 , $this -> _em -> createQuery (
" select count(u.id) from Doctrine \T ests \ Models \ CMS \ CmsUser u " )
-> getSingleScalarResult ());
}
2009-12-07 02:40:38 +03:00
public function testTextColumnSaveAndRetrieve ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$article = new \Doctrine\Tests\Models\CMS\CmsArticle ();
$article -> text = " Lorem ipsum dolor sunt. " ;
$article -> topic = " A Test Article! " ;
$article -> setAuthor ( $user );
$this -> _em -> persist ( $article );
$this -> _em -> flush ();
$articleId = $article -> id ;
$this -> _em -> clear ();
2010-08-08 17:01:03 +04:00
// test find() with leading backslash at the same time
$articleNew = $this -> _em -> find ( '\Doctrine\Tests\Models\CMS\CmsArticle' , $articleId );
$this -> assertTrue ( $this -> _em -> contains ( $articleNew ));
2009-12-07 02:49:30 +03:00
$this -> assertEquals ( " Lorem ipsum dolor sunt. " , $articleNew -> text );
$this -> assertNotSame ( $article , $articleNew );
$articleNew -> text = " Lorem ipsum dolor sunt. And stuff! " ;
$this -> _em -> flush ();
$this -> _em -> clear ();
$articleNew = $this -> _em -> find ( 'Doctrine\Tests\Models\CMS\CmsArticle' , $articleId );
$this -> assertEquals ( " Lorem ipsum dolor sunt. And stuff! " , $articleNew -> text );
2010-07-09 15:18:53 +04:00
$this -> assertTrue ( $this -> _em -> contains ( $articleNew ));
2009-12-07 02:40:38 +03:00
}
2010-07-08 02:20:54 +04:00
2009-12-09 20:00:18 +03:00
public function testFlushDoesNotIssueUnnecessaryUpdates ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$address = new CmsAddress ;
$address -> country = 'Germany' ;
$address -> city = 'Berlin' ;
$address -> zip = '12345' ;
$address -> user = $user ;
$user -> address = $address ;
2009-12-11 00:27:20 +03:00
$article = new \Doctrine\Tests\Models\CMS\CmsArticle ();
$article -> text = " Lorem ipsum dolor sunt. " ;
$article -> topic = " A Test Article! " ;
$article -> setAuthor ( $user );
$this -> _em -> persist ( $article );
2009-12-09 20:00:18 +03:00
$this -> _em -> persist ( $user );
2010-04-01 00:47:35 +04:00
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
2009-12-11 00:27:20 +03:00
2009-12-09 20:00:18 +03:00
$this -> _em -> flush ();
$this -> _em -> clear ();
2009-12-11 00:27:20 +03:00
$query = $this -> _em -> createQuery ( 'select u,a,ad from Doctrine\Tests\Models\CMS\CmsUser u join u.articles a join u.address ad' );
2009-12-09 20:00:18 +03:00
$user2 = $query -> getSingleResult ();
2009-12-11 00:27:20 +03:00
$this -> assertEquals ( 1 , count ( $user2 -> articles ));
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $user2 -> address );
2009-12-09 20:00:18 +03:00
2010-04-01 00:47:35 +04:00
$oldLogger = $this -> _em -> getConnection () -> getConfiguration () -> getSQLLogger ();
2009-12-09 20:00:18 +03:00
$debugStack = new \Doctrine\DBAL\Logging\DebugStack ;
2010-04-01 00:47:35 +04:00
$this -> _em -> getConnection () -> getConfiguration () -> setSQLLogger ( $debugStack );
2009-12-09 20:00:18 +03:00
$this -> _em -> flush ();
$this -> assertEquals ( 0 , count ( $debugStack -> queries ));
2010-04-01 00:47:35 +04:00
$this -> _em -> getConnection () -> getConfiguration () -> setSQLLogger ( $oldLogger );
2009-12-09 20:00:18 +03:00
}
2010-02-10 13:47:42 +03:00
public function testRemoveEntityByReference ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
2010-04-01 00:47:35 +04:00
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
2010-02-10 13:47:42 +03:00
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$userRef = $this -> _em -> getReference ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user -> getId ());
$this -> _em -> remove ( $userRef );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertEquals ( 0 , $this -> _em -> getConnection () -> fetchColumn ( " select count(*) from cms_users " ));
2010-04-01 00:47:35 +04:00
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
2010-02-10 13:47:42 +03:00
}
2010-07-08 02:20:54 +04:00
2010-02-23 17:58:12 +03:00
public function testQueryEntityByReference ()
2010-02-20 21:27:05 +03:00
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$address = new CmsAddress ;
$address -> country = 'Germany' ;
$address -> city = 'Berlin' ;
$address -> zip = '12345' ;
$user -> setAddress ( $address );
2010-05-13 15:19:59 +04:00
$this -> _em -> transactional ( function ( $em ) use ( $user ) {
$em -> persist ( $user );
});
2010-02-20 21:27:05 +03:00
$this -> _em -> clear ();
2010-04-01 00:47:35 +04:00
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
2010-02-23 17:58:12 +03:00
2010-02-20 21:27:05 +03:00
$userRef = $this -> _em -> getReference ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user -> getId ());
$address2 = $this -> _em -> createQuery ( 'select a from Doctrine\Tests\Models\CMS\CmsAddress a where a.user = :user' )
-> setParameter ( 'user' , $userRef )
-> getSingleResult ();
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $address2 -> getUser ());
2010-02-23 17:58:12 +03:00
$this -> assertTrue ( $userRef === $address2 -> getUser ());
$this -> assertFalse ( $userRef -> __isInitialized__ );
$this -> assertEquals ( 'Germany' , $address2 -> country );
$this -> assertEquals ( 'Berlin' , $address2 -> city );
$this -> assertEquals ( '12345' , $address2 -> zip );
}
2010-07-08 02:20:54 +04:00
public function testOneToOneNullUpdate ()
{
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$address = new CmsAddress ();
$address -> city = " Bonn " ;
$address -> zip = " 12354 " ;
$address -> country = " Germany " ;
$address -> street = " somestreet " ;
$address -> user = $user ;
$this -> _em -> persist ( $address );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> assertEquals ( 1 , $this -> _em -> getConnection () -> fetchColumn ( " select 1 from cms_addresses where user_id = " . $user -> id ));
$address -> user = null ;
$this -> _em -> flush ();
2010-07-29 01:36:52 +04:00
$this -> assertNotEquals ( 1 , $this -> _em -> getConnection () -> fetchColumn ( " select 1 from cms_addresses where user_id = " . $user -> id ));
2010-07-08 02:20:54 +04:00
}
/**
* @ group DDC - 600
* @ group DDC - 455
*/
public function testNewAssociatedEntityDuringFlushThrowsException ()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$address = new CmsAddress ();
$address -> city = " Bonn " ;
$address -> zip = " 12354 " ;
$address -> country = " Germany " ;
$address -> street = " somestreet " ;
$address -> user = $user ;
$this -> _em -> persist ( $address );
// pretend we forgot to persist $user
try {
$this -> _em -> flush (); // should raise an exception
$this -> fail ();
} catch ( \InvalidArgumentException $expected ) {}
}
/**
* @ group DDC - 600
* @ group DDC - 455
*/
public function testNewAssociatedEntityDuringFlushThrowsException2 ()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$address = new CmsAddress ();
$address -> city = " Bonn " ;
$address -> zip = " 12354 " ;
$address -> country = " Germany " ;
$address -> street = " somestreet " ;
$address -> user = $user ;
$this -> _em -> persist ( $address );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$u2 = new CmsUser ;
$u2 -> username = " beberlei " ;
$u2 -> name = " Benjamin E. " ;
$u2 -> status = 'inactive' ;
$address -> user = $u2 ;
// pretend we forgot to persist $u2
try {
$this -> _em -> flush (); // should raise an exception
$this -> fail ();
} catch ( \InvalidArgumentException $expected ) {}
}
/**
* @ group DDC - 600
* @ group DDC - 455
*/
public function testNewAssociatedEntityDuringFlushThrowsException3 ()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$art = new CmsArticle ();
$art -> topic = 'topic' ;
$art -> text = 'the text' ;
$com = new CmsComment ();
$com -> topic = 'Good' ;
$com -> text = 'Really good!' ;
$art -> addComment ( $com );
$this -> _em -> persist ( $art );
// pretend we forgot to persist $com
try {
$this -> _em -> flush (); // should raise an exception
$this -> fail ();
} catch ( \InvalidArgumentException $expected ) {}
}
public function testOneToOneOrphanRemoval ()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$address = new CmsAddress ();
$address -> city = " Bonn " ;
$address -> zip = " 12354 " ;
$address -> country = " Germany " ;
$address -> street = " somestreet " ;
$address -> user = $user ;
$user -> address = $address ;
$this -> _em -> persist ( $address );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$addressId = $address -> getId ();
$user -> address = null ;
$this -> _em -> flush ();
$this -> assertEquals ( 0 , $this -> _em -> getConnection () -> fetchColumn ( " select count(*) from cms_addresses " ));
// check orphan removal through replacement
$user -> address = $address ;
$address -> user = $user ;
$this -> _em -> flush ();
$this -> assertEquals ( 1 , $this -> _em -> getConnection () -> fetchColumn ( " select count(*) from cms_addresses " ));
2010-08-11 00:07:43 +04:00
// remove $address to free up unique key id
$this -> _em -> remove ( $address );
$this -> _em -> flush ();
2010-07-08 02:20:54 +04:00
$newAddress = new CmsAddress ();
$newAddress -> city = " NewBonn " ;
$newAddress -> zip = " 12354 " ;
$newAddress -> country = " NewGermany " ;
$newAddress -> street = " somenewstreet " ;
$newAddress -> user = $user ;
$user -> address = $newAddress ;
$this -> _em -> flush ();
$this -> assertEquals ( 1 , $this -> _em -> getConnection () -> fetchColumn ( " select count(*) from cms_addresses " ));
$this -> assertEquals ( 0 , $this -> _em -> getConnection () -> fetchColumn ( " select count(*) from cms_addresses where id= " . $addressId . " " ));
}
2010-07-08 19:30:39 +04:00
2010-07-20 16:20:13 +04:00
public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt ()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$userId = $user -> id ;
$this -> _em -> clear ();
$user = $this -> _em -> getPartialReference ( 'Doctrine\Tests\Models\CMS\CmsUser' , $userId );
$this -> assertTrue ( $this -> _em -> contains ( $user ));
$this -> assertNull ( $user -> getName ());
$this -> assertEquals ( $userId , $user -> id );
$user -> name = 'Stephan' ;
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertEquals ( 'Stephan' , $this -> _em -> find ( get_class ( $user ), $userId ) -> name );
}
2010-07-29 16:08:36 +04:00
public function testMergePersistsNewEntities ()
2009-11-21 21:52:02 +03:00
{
2010-07-29 16:08:36 +04:00
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$managedUser = $this -> _em -> merge ( $user );
$this -> assertEquals ( 'beberlei' , $managedUser -> username );
$this -> assertEquals ( 'Benjamin E.' , $managedUser -> name );
$this -> assertEquals ( 'active' , $managedUser -> status );
$this -> assertTrue ( $user !== $managedUser );
$this -> assertTrue ( $this -> _em -> contains ( $managedUser ));
2009-11-21 21:52:02 +03:00
$this -> _em -> flush ();
2010-07-29 16:08:36 +04:00
$userId = $managedUser -> id ;
2009-11-21 21:52:02 +03:00
$this -> _em -> clear ();
2010-07-29 16:08:36 +04:00
2010-07-30 00:27:00 +04:00
$user2 = $this -> _em -> find ( get_class ( $managedUser ), $userId );
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user2 );
2010-07-29 16:08:36 +04:00
}
public function testMergeThrowsExceptionIfEntityWithGeneratedIdentifierDoesNotExist ()
{
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$user -> id = 42 ;
try {
$this -> _em -> merge ( $user );
$this -> fail ();
} catch ( \Doctrine\ORM\EntityNotFoundException $enfe ) {}
2009-11-21 21:52:02 +03:00
}
2010-07-30 00:27:00 +04:00
/**
* @ group DDC - 634
*/
public function testOneToOneMergeSetNull ()
{
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$ph = new CmsPhonenumber ();
$ph -> phonenumber = " 12345 " ;
$user -> addPhonenumber ( $ph );
$this -> _em -> persist ( $user );
$this -> _em -> persist ( $ph );
$this -> _em -> flush ();
$this -> _em -> clear ();
$ph -> user = null ;
$managedPh = $this -> _em -> merge ( $ph );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> assertNull ( $this -> _em -> find ( get_class ( $ph ), $ph -> phonenumber ) -> getUser ());
}
2011-03-17 00:51:32 +03:00
/**
* @ group DDC - 952
*/
public function testManyToOneFetchModeQuery ()
{
$user = new CmsUser ();
$user -> username = " beberlei " ;
$user -> name = " Benjamin E. " ;
$user -> status = 'active' ;
$article = new CmsArticle ();
$article -> topic = " foo " ;
$article -> text = " bar " ;
$article -> user = $user ;
$this -> _em -> persist ( $article );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$qc = $this -> getCurrentQueryCount ();
$dql = " SELECT a FROM Doctrine \T ests \ Models \ CMS \ CmsArticle a WHERE a.id = ?1 " ;
$article = $this -> _em -> createQuery ( $dql )
-> setParameter ( 1 , $article -> id )
-> setFetchMode ( 'Doctrine\Tests\Models\CMS\CmsArticle' , 'user' , \Doctrine\ORM\Mapping\ClassMetadata :: FETCH_EAGER )
-> getSingleResult ();
$this -> assertInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $article -> user , " It IS a proxy, ... " );
$this -> assertTrue ( $article -> user -> __isInitialized__ , " ...but its initialized! " );
$this -> assertEquals ( $qc + 2 , $this -> getCurrentQueryCount ());
}
2009-07-28 15:43:42 +04:00
}