2009-04-03 15:06:58 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
2011-05-13 06:05:45 +04:00
use Doctrine\DBAL\Connection ;
2009-12-18 16:20:22 +03:00
use Doctrine\Tests\Models\CMS\CmsUser ,
Doctrine\Tests\Models\CMS\CmsArticle ;
2011-03-17 00:51:32 +03:00
use Doctrine\ORM\Mapping\ClassMetadata ;
2011-03-20 14:19:01 +03:00
use Doctrine\ORM\Query ;
2009-04-03 15:06:58 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
/**
* Functional Query tests .
*
* @ author robo
*/
class QueryTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp ()
{
$this -> useModelSet ( 'cms' );
parent :: setUp ();
}
2010-01-06 17:21:17 +03:00
2009-04-03 15:06:58 +04:00
public function testSimpleQueries ()
{
$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-04-03 15:06:58 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select u, upper(u.name) from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.username = 'gblanco' " );
2011-11-18 20:29:31 +04:00
2009-08-03 21:18:37 +04:00
$result = $query -> getResult ();
2011-11-18 20:29:31 +04:00
2009-04-03 15:06:58 +04:00
$this -> assertEquals ( 1 , count ( $result ));
2011-07-26 17:22:57 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $result [ 0 ][ 0 ]);
2009-04-03 15:06:58 +04:00
$this -> assertEquals ( 'Guilherme' , $result [ 0 ][ 0 ] -> name );
$this -> assertEquals ( 'gblanco' , $result [ 0 ][ 0 ] -> username );
$this -> assertEquals ( 'developer' , $result [ 0 ][ 0 ] -> status );
$this -> assertEquals ( 'GUILHERME' , $result [ 0 ][ 1 ]);
2009-08-03 21:18:37 +04:00
$resultArray = $query -> getArrayResult ();
2009-04-03 15:06:58 +04:00
$this -> assertEquals ( 1 , count ( $resultArray ));
$this -> assertTrue ( is_array ( $resultArray [ 0 ][ 0 ]));
$this -> assertEquals ( 'Guilherme' , $resultArray [ 0 ][ 0 ][ 'name' ]);
$this -> assertEquals ( 'gblanco' , $resultArray [ 0 ][ 0 ][ 'username' ]);
$this -> assertEquals ( 'developer' , $resultArray [ 0 ][ 0 ][ 'status' ]);
$this -> assertEquals ( 'GUILHERME' , $resultArray [ 0 ][ 1 ]);
$scalarResult = $query -> getScalarResult ();
$this -> assertEquals ( 1 , count ( $scalarResult ));
$this -> assertEquals ( 'Guilherme' , $scalarResult [ 0 ][ 'u_name' ]);
$this -> assertEquals ( 'gblanco' , $scalarResult [ 0 ][ 'u_username' ]);
$this -> assertEquals ( 'developer' , $scalarResult [ 0 ][ 'u_status' ]);
2009-06-15 22:25:47 +04:00
$this -> assertEquals ( 'GUILHERME' , $scalarResult [ 0 ][ 1 ]);
2009-04-03 15:06:58 +04:00
$query = $this -> _em -> createQuery ( " select upper(u.name) from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.username = 'gblanco' " );
$this -> assertEquals ( 'GUILHERME' , $query -> getSingleScalarResult ());
}
2009-04-09 22:12:48 +04:00
public function testJoinQueries ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$article1 = new CmsArticle ;
$article1 -> topic = " Doctrine 2 " ;
$article1 -> text = " This is an introduction to Doctrine 2. " ;
$user -> addArticle ( $article1 );
$article2 = new CmsArticle ;
$article2 -> topic = " Symfony 2 " ;
$article2 -> text = " This is an introduction to Symfony 2. " ;
$user -> addArticle ( $article2 );
2009-07-19 20:54:53 +04:00
$this -> _em -> persist ( $user );
$this -> _em -> persist ( $article1 );
$this -> _em -> persist ( $article2 );
2009-04-09 22:12:48 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
2011-12-20 23:22:14 +04:00
$query = $this -> _em -> createQuery ( " select u, a from Doctrine \T ests \ Models \ CMS \ CmsUser u join u.articles a ORDER BY a.topic " );
2009-08-03 21:18:37 +04:00
$users = $query -> getResult ();
2009-04-09 22:12:48 +04:00
$this -> assertEquals ( 1 , count ( $users ));
2011-07-26 17:22:57 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $users [ 0 ]);
2009-04-09 22:12:48 +04:00
$this -> assertEquals ( 2 , count ( $users [ 0 ] -> articles ));
$this -> assertEquals ( 'Doctrine 2' , $users [ 0 ] -> articles [ 0 ] -> topic );
$this -> assertEquals ( 'Symfony 2' , $users [ 0 ] -> articles [ 1 ] -> topic );
}
2011-05-12 00:30:42 +04:00
public function testUsingZeroBasedQueryParameterShouldWork ()
{
$user = new CmsUser ;
$user -> name = 'Jonathan' ;
$user -> username = 'jwage' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$q = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?0' );
$q -> setParameter ( 0 , 'jwage' );
$user = $q -> getSingleResult ();
2011-11-18 20:29:31 +04:00
2011-05-12 00:30:42 +04:00
$this -> assertNotNull ( $user );
}
2009-11-20 18:10:45 +03:00
public function testUsingUnknownQueryParameterShouldThrowException ()
{
$this -> setExpectedException (
" Doctrine \ ORM \ Query \ QueryException " ,
" Invalid parameter: token 2 is not defined in the query. "
);
$q = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?1' );
$q -> setParameter ( 2 , 'jwage' );
$user = $q -> getSingleResult ();
}
public function testMismatchingParamExpectedParamCount ()
{
$this -> setExpectedException (
" Doctrine \ ORM \ Query \ QueryException " ,
" Invalid parameter number: number of bound variables does not match number of tokens "
);
$q = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?1' );
$q -> setParameter ( 1 , 'jwage' );
$q -> setParameter ( 2 , 'jwage' );
$user = $q -> getSingleResult ();
}
public function testInvalidInputParameterThrowsException ()
{
2009-11-21 20:04:17 +03:00
$this -> setExpectedException ( " Doctrine \ ORM \ Query \ QueryException " );
2009-11-20 18:10:45 +03:00
$q = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?' );
$q -> setParameter ( 1 , 'jwage' );
$user = $q -> getSingleResult ();
}
2010-09-13 00:44:02 +04:00
public function testSetParameters ()
{
$q = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.name = ?1 AND u.status = ?2' );
$q -> setParameters ( array ( 1 => 'jwage' , 2 => 'active' ));
$users = $q -> getResult ();
}
2011-03-20 14:19:01 +03:00
/**
* @ group DDC - 1070
*/
public function testIterateResultAsArrayAndParams ()
{
$article1 = new CmsArticle ;
$article1 -> topic = " Doctrine 2 " ;
$article1 -> text = " This is an introduction to Doctrine 2. " ;
$article2 = new CmsArticle ;
$article2 -> topic = " Symfony 2 " ;
$article2 -> text = " This is an introduction to Symfony 2. " ;
$this -> _em -> persist ( $article1 );
$this -> _em -> persist ( $article2 );
$this -> _em -> flush ();
$this -> _em -> clear ();
2011-03-20 14:25:27 +03:00
$articleId = $article1 -> id ;
2011-03-20 14:19:01 +03:00
$query = $this -> _em -> createQuery ( " select a from Doctrine \T ests \ Models \ CMS \ CmsArticle a WHERE a.topic = ?1 " );
$articles = $query -> iterate ( array ( 1 => 'Doctrine 2' ), Query :: HYDRATE_ARRAY );
$found = array ();
foreach ( $articles AS $article ) {
$found [] = $article ;
}
$this -> assertEquals ( 1 , count ( $found ));
$this -> assertEquals ( array (
2011-03-20 14:25:27 +03:00
array ( array ( 'id' => $articleId , 'topic' => 'Doctrine 2' , 'text' => 'This is an introduction to Doctrine 2.' , 'version' => 1 ))
2011-03-20 14:19:01 +03:00
), $found );
}
2009-11-20 18:10:45 +03:00
public function testIterateResult_IterativelyBuildUpUnitOfWork ()
{
$article1 = new CmsArticle ;
$article1 -> topic = " Doctrine 2 " ;
$article1 -> text = " This is an introduction to Doctrine 2. " ;
$article2 = new CmsArticle ;
$article2 -> topic = " Symfony 2 " ;
$article2 -> text = " This is an introduction to Symfony 2. " ;
$this -> _em -> persist ( $article1 );
$this -> _em -> persist ( $article2 );
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select a from Doctrine \T ests \ Models \ CMS \ CmsArticle a " );
$articles = $query -> iterate ();
$iteratedCount = 0 ;
$topics = array ();
foreach ( $articles AS $row ) {
$article = $row [ 0 ];
$topics [] = $article -> topic ;
$identityMap = $this -> _em -> getUnitOfWork () -> getIdentityMap ();
$identityMapCount = count ( $identityMap [ 'Doctrine\Tests\Models\CMS\CmsArticle' ]);
$this -> assertTrue ( $identityMapCount > $iteratedCount );
2011-11-18 20:29:31 +04:00
2009-11-20 18:10:45 +03:00
$iteratedCount ++ ;
}
$this -> assertEquals ( array ( " Doctrine 2 " , " Symfony 2 " ), $topics );
$this -> assertEquals ( 2 , $iteratedCount );
$this -> _em -> flush ();
$this -> _em -> clear ();
}
2009-11-21 16:13:19 +03:00
2012-05-27 14:00:43 +04:00
public function testIterateResultClearEveryCycle ()
{
$article1 = new CmsArticle ;
$article1 -> topic = " Doctrine 2 " ;
$article1 -> text = " This is an introduction to Doctrine 2. " ;
$article2 = new CmsArticle ;
$article2 -> topic = " Symfony 2 " ;
$article2 -> text = " This is an introduction to Symfony 2. " ;
$this -> _em -> persist ( $article1 );
$this -> _em -> persist ( $article2 );
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select a from Doctrine \T ests \ Models \ CMS \ CmsArticle a " );
$articles = $query -> iterate ();
$iteratedCount = 0 ;
$topics = array ();
foreach ( $articles AS $row ) {
$article = $row [ 0 ];
$topics [] = $article -> topic ;
$this -> _em -> clear ();
$iteratedCount ++ ;
}
$this -> assertEquals ( array ( " Doctrine 2 " , " Symfony 2 " ), $topics );
$this -> assertEquals ( 2 , $iteratedCount );
$this -> _em -> flush ();
}
2009-12-08 23:53:01 +03:00
/**
* @ expectedException \Doctrine\ORM\Query\QueryException
*/
public function testIterateResult_FetchJoinedCollection_ThrowsException ()
{
$query = $this -> _em -> createQuery ( " SELECT u, a FROM Doctrine \T ests \ Models \ CMS \ CmsUser u JOIN u.articles a " );
$articles = $query -> iterate ();
}
2011-11-18 20:29:31 +04:00
2009-12-18 16:20:22 +03:00
/**
* @ expectedException Doctrine\ORM\NoResultException
*/
public function testGetSingleResultThrowsExceptionOnNoResult ()
{
$this -> _em -> createQuery ( " select a from Doctrine \T ests \ Models \ CMS \ CmsArticle a " )
2010-04-14 05:06:01 +04:00
-> getSingleResult ();
}
/**
* @ expectedException Doctrine\ORM\NoResultException
*/
public function testGetSingleScalarResultThrowsExceptionOnNoResult ()
{
$this -> _em -> createQuery ( " select a from Doctrine \T ests \ Models \ CMS \ CmsArticle a " )
-> getSingleScalarResult ();
}
/**
* @ expectedException Doctrine\ORM\NonUniqueResultException
*/
public function testGetSingleScalarResultThrowsExceptionOnNonUniqueResult ()
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$article1 = new CmsArticle ;
$article1 -> topic = " Doctrine 2 " ;
$article1 -> text = " This is an introduction to Doctrine 2. " ;
$user -> addArticle ( $article1 );
$article2 = new CmsArticle ;
$article2 -> topic = " Symfony 2 " ;
$article2 -> text = " This is an introduction to Symfony 2. " ;
$user -> addArticle ( $article2 );
$this -> _em -> persist ( $user );
$this -> _em -> persist ( $article1 );
$this -> _em -> persist ( $article2 );
$this -> _em -> flush ();
$this -> _em -> clear ();
$this -> _em -> createQuery ( " select a from Doctrine \T ests \ Models \ CMS \ CmsArticle a " )
-> getSingleScalarResult ();
2009-12-18 16:20:22 +03:00
}
2009-04-03 15:06:58 +04:00
2010-07-28 23:50:25 +04:00
public function testModifiedLimitQuery ()
{
for ( $i = 0 ; $i < 5 ; $i ++ ) {
$user = new CmsUser ;
$user -> name = 'Guilherme' . $i ;
$user -> username = 'gblanco' . $i ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
}
$this -> _em -> flush ();
$this -> _em -> clear ();
$data = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' )
-> setFirstResult ( 1 )
-> setMaxResults ( 2 )
-> getResult ();
$this -> assertEquals ( 2 , count ( $data ));
$this -> assertEquals ( 'gblanco1' , $data [ 0 ] -> username );
$this -> assertEquals ( 'gblanco2' , $data [ 1 ] -> username );
$data = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' )
-> setFirstResult ( 3 )
-> setMaxResults ( 2 )
-> getResult ();
$this -> assertEquals ( 2 , count ( $data ));
$this -> assertEquals ( 'gblanco3' , $data [ 0 ] -> username );
$this -> assertEquals ( 'gblanco4' , $data [ 1 ] -> username );
2010-07-28 23:57:05 +04:00
$data = $this -> _em -> createQuery ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' )
-> setFirstResult ( 3 )
-> setMaxResults ( 2 )
-> getScalarResult ();
2010-07-28 23:50:25 +04:00
}
2010-03-03 04:30:00 +03:00
public function testSupportsQueriesWithEntityNamespaces ()
2010-02-26 21:11:53 +03:00
{
2010-03-03 04:30:00 +03:00
$this -> _em -> getConfiguration () -> addEntityNamespace ( 'CMS' , 'Doctrine\Tests\Models\CMS' );
2010-02-26 21:11:53 +03:00
try {
2010-03-03 04:30:00 +03:00
$query = $this -> _em -> createQuery ( 'UPDATE CMS:CmsUser u SET u.name = ?1' );
2010-02-26 21:11:53 +03:00
$this -> assertEquals ( 'UPDATE cms_users SET name = ?' , $query -> getSql ());
$query -> free ();
} catch ( \Exception $e ) {
$this -> fail ( $e -> getMessage ());
}
2010-03-03 04:30:00 +03:00
$this -> _em -> getConfiguration () -> setEntityNamespaces ( array ());
2010-02-26 21:11:53 +03:00
}
2010-06-07 15:57:43 +04:00
/**
* @ group DDC - 604
*/
public function testEntityParameters ()
{
$article = new CmsArticle ;
$article -> topic = " dr. dolittle " ;
$article -> text = " Once upon a time ... " ;
$author = new CmsUser ;
$author -> name = " anonymous " ;
$author -> username = " anon " ;
$author -> status = " here " ;
$article -> user = $author ;
$this -> _em -> persist ( $author );
$this -> _em -> persist ( $article );
$this -> _em -> flush ();
$this -> _em -> clear ();
//$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
$q = $this -> _em -> createQuery ( " select a from Doctrine \T ests \ Models \ CMS \ CmsArticle a where a.topic = :topic and a.user = :user " )
-> setParameter ( " user " , $this -> _em -> getReference ( 'Doctrine\Tests\Models\CMS\CmsUser' , $author -> id ))
-> setParameter ( " topic " , " dr. dolittle " );
$result = $q -> getResult ();
$this -> assertEquals ( 1 , count ( $result ));
2011-07-26 17:22:57 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsArticle' , $result [ 0 ]);
2010-06-07 15:57:43 +04:00
$this -> assertEquals ( " dr. dolittle " , $result [ 0 ] -> topic );
2011-07-26 17:22:57 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $result [ 0 ] -> user );
2010-06-07 15:57:43 +04:00
$this -> assertFalse ( $result [ 0 ] -> user -> __isInitialized__ );
}
2011-11-18 20:29:31 +04:00
2011-03-09 00:22:54 +03:00
/**
* @ group DDC - 952
*/
public function testEnableFetchEagerMode ()
{
for ( $i = 0 ; $i < 10 ; $i ++ ) {
$article = new CmsArticle ;
$article -> topic = " dr. dolittle " ;
$article -> text = " Once upon a time ... " ;
$author = new CmsUser ;
$author -> name = " anonymous " ;
$author -> username = " anon " . $i ;
$author -> status = " here " ;
$article -> user = $author ;
$this -> _em -> persist ( $author );
$this -> _em -> persist ( $article );
}
$this -> _em -> flush ();
$this -> _em -> clear ();
2011-11-18 20:29:31 +04:00
2011-03-09 00:22:54 +03:00
$articles = $this -> _em -> createQuery ( 'select a from Doctrine\Tests\Models\CMS\CmsArticle a' )
2011-03-17 00:51:32 +03:00
-> setFetchMode ( 'Doctrine\Tests\Models\CMS\CmsArticle' , 'user' , ClassMetadata :: FETCH_EAGER )
2011-03-09 00:22:54 +03:00
-> getResult ();
2011-11-18 20:29:31 +04:00
2011-03-09 00:22:54 +03:00
$this -> assertEquals ( 10 , count ( $articles ));
foreach ( $articles AS $article ) {
$this -> assertNotInstanceOf ( 'Doctrine\ORM\Proxy\Proxy' , $article );
}
}
2011-04-01 01:31:58 +04:00
/**
* @ group DDC - 991
*/
2011-04-01 01:35:01 +04:00
public function testgetOneOrNullResult ()
2011-04-01 01:31:58 +04:00
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$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' " );
2011-04-01 01:35:01 +04:00
$fetchedUser = $query -> getOneOrNullResult ();
2011-04-01 01:31:58 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $fetchedUser );
$this -> assertEquals ( 'gblanco' , $fetchedUser -> username );
$query = $this -> _em -> createQuery ( " select u.username from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.username = 'gblanco' " );
2011-04-01 01:35:01 +04:00
$fetchedUsername = $query -> getOneOrNullResult ( Query :: HYDRATE_SINGLE_SCALAR );
2011-04-01 01:31:58 +04:00
$this -> assertEquals ( 'gblanco' , $fetchedUsername );
}
/**
* @ group DDC - 991
*/
2011-04-01 01:35:01 +04:00
public function testgetOneOrNullResultSeveralRows ()
2011-04-01 01:31:58 +04:00
{
$user = new CmsUser ;
$user -> name = 'Guilherme' ;
$user -> username = 'gblanco' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " select u from Doctrine \T ests \ Models \ CMS \ CmsUser u " );
$this -> setExpectedException ( 'Doctrine\ORM\NonUniqueResultException' );
2011-04-01 01:35:01 +04:00
$fetchedUser = $query -> getOneOrNullResult ();
2011-04-01 01:31:58 +04:00
}
/**
* @ group DDC - 991
*/
2011-04-01 01:35:01 +04:00
public function testgetOneOrNullResultNoRows ()
2011-04-01 01:31:58 +04:00
{
$query = $this -> _em -> createQuery ( " select u from Doctrine \T ests \ Models \ CMS \ CmsUser u " );
2011-04-01 01:35:01 +04:00
$this -> assertNull ( $query -> getOneOrNullResult ());
2011-04-01 01:31:58 +04:00
$query = $this -> _em -> createQuery ( " select u.username from Doctrine \T ests \ Models \ CMS \ CmsUser u where u.username = 'gblanco' " );
2011-04-01 01:35:01 +04:00
$this -> assertNull ( $query -> getOneOrNullResult ( Query :: HYDRATE_SCALAR ));
2011-04-01 01:31:58 +04:00
}
2011-11-18 20:29:31 +04:00
/**
* @ group DBAL - 171
*/
public function testParameterOrder ()
{
2011-11-18 20:42:32 +04:00
$user1 = new CmsUser ;
$user1 -> name = 'Benjamin' ;
$user1 -> username = 'beberlei' ;
$user1 -> status = 'developer' ;
$this -> _em -> persist ( $user1 );
$user2 = new CmsUser ;
$user2 -> name = 'Roman' ;
$user2 -> username = 'romanb' ;
$user2 -> status = 'developer' ;
$this -> _em -> persist ( $user2 );
$user3 = new CmsUser ;
$user3 -> name = 'Jonathan' ;
$user3 -> username = 'jwage' ;
$user3 -> status = 'developer' ;
$this -> _em -> persist ( $user3 );
2011-11-18 20:29:31 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
$query = $this -> _em -> createQuery ( " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.status = :a AND u.id IN (:b) " );
$query -> setParameters ( array (
2011-11-18 20:42:32 +04:00
'b' => array ( $user1 -> id , $user2 -> id , $user3 -> id ),
2011-11-18 20:29:31 +04:00
'a' => 'developer' ,
));
$result = $query -> getResult ();
$this -> assertEquals ( 3 , count ( $result ));
}
2011-05-13 06:05:45 +04:00
public function testDqlWithAutoInferOfParameters ()
{
$user = new CmsUser ;
$user -> name = 'Benjamin' ;
$user -> username = 'beberlei' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
2011-11-18 20:29:31 +04:00
2011-05-13 06:05:45 +04:00
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
2011-11-18 20:29:31 +04:00
2011-05-13 06:05:45 +04:00
$user = new CmsUser ;
$user -> name = 'Jonathan' ;
$user -> username = 'jwage' ;
$user -> status = 'developer' ;
$this -> _em -> persist ( $user );
2011-11-18 20:29:31 +04:00
2011-05-13 06:05:45 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
2011-11-18 20:29:31 +04:00
2011-05-13 06:05:45 +04:00
$query = $this -> _em -> createQuery ( " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.username IN (?0) " );
$query -> setParameter ( 0 , array ( 'beberlei' , 'jwage' ));
2011-11-18 20:29:31 +04:00
2011-05-13 06:05:45 +04:00
$users = $query -> execute ();
2011-11-18 20:29:31 +04:00
2011-05-13 06:05:45 +04:00
$this -> assertEquals ( 2 , count ( $users ));
}
2011-11-18 20:29:31 +04:00
2011-05-13 07:12:05 +04:00
public function testQueryBuilderWithStringWhereClauseContainingOrAndConditionalPrimary ()
{
$qb = $this -> _em -> createQueryBuilder ();
$qb -> select ( 'u' )
-> from ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> innerJoin ( 'u.articles' , 'a' )
-> where ( '(u.id = 0) OR (u.id IS NULL)' );
2011-11-18 20:29:31 +04:00
2011-05-13 07:12:05 +04:00
$query = $qb -> getQuery ();
$users = $query -> execute ();
2011-11-18 20:29:31 +04:00
2011-05-13 07:12:05 +04:00
$this -> assertEquals ( 0 , count ( $users ));
}
2011-11-18 20:29:31 +04:00
2011-09-02 02:11:57 +04:00
public function testQueryWithArrayOfEntitiesAsParameter ()
{
$userA = new CmsUser ;
$userA -> name = 'Benjamin' ;
$userA -> username = 'beberlei' ;
$userA -> status = 'developer' ;
$this -> _em -> persist ( $userA );
2011-11-18 20:29:31 +04:00
2011-09-02 02:11:57 +04:00
$userB = new CmsUser ;
$userB -> name = 'Roman' ;
$userB -> username = 'romanb' ;
$userB -> status = 'developer' ;
$this -> _em -> persist ( $userB );
2011-11-18 20:29:31 +04:00
2011-09-02 02:11:57 +04:00
$userC = new CmsUser ;
$userC -> name = 'Jonathan' ;
$userC -> username = 'jwage' ;
$userC -> status = 'developer' ;
$this -> _em -> persist ( $userC );
2011-11-18 20:29:31 +04:00
2011-09-02 02:11:57 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
2011-11-18 20:29:31 +04:00
2011-09-02 02:11:57 +04:00
$query = $this -> _em -> createQuery ( " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u IN (?0) OR u.username = ?1 " );
$query -> setParameter ( 0 , array ( $userA , $userC ));
$query -> setParameter ( 1 , 'beberlei' );
2011-11-18 20:29:31 +04:00
2011-09-02 02:11:57 +04:00
$users = $query -> execute ();
2011-11-18 20:29:31 +04:00
2011-09-02 02:11:57 +04:00
$this -> assertEquals ( 2 , count ( $users ));
}
2011-11-18 20:29:31 +04:00
2011-10-03 09:07:07 +04:00
public function testQueryWithHiddenAsSelectExpression ()
{
$userA = new CmsUser ;
$userA -> name = 'Benjamin' ;
$userA -> username = 'beberlei' ;
$userA -> status = 'developer' ;
$this -> _em -> persist ( $userA );
2011-11-18 20:29:31 +04:00
2011-10-03 09:07:07 +04:00
$userB = new CmsUser ;
$userB -> name = 'Roman' ;
$userB -> username = 'romanb' ;
$userB -> status = 'developer' ;
$this -> _em -> persist ( $userB );
2011-11-18 20:29:31 +04:00
2011-10-03 09:07:07 +04:00
$userC = new CmsUser ;
$userC -> name = 'Jonathan' ;
$userC -> username = 'jwage' ;
$userC -> status = 'developer' ;
$this -> _em -> persist ( $userC );
2011-11-18 20:29:31 +04:00
2011-10-03 09:07:07 +04:00
$this -> _em -> flush ();
$this -> _em -> clear ();
2011-11-18 20:29:31 +04:00
2011-10-03 09:07:07 +04:00
$query = $this -> _em -> createQuery ( " SELECT u, (SELECT COUNT(u2.id) FROM Doctrine \T ests \ Models \ CMS \ CmsUser u2) AS HIDDEN total FROM Doctrine \T ests \ Models \ CMS \ CmsUser u " );
$users = $query -> execute ();
2011-11-18 20:29:31 +04:00
2011-10-03 09:07:07 +04:00
$this -> assertEquals ( 3 , count ( $users ));
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $users [ 0 ]);
}
2012-02-18 19:07:55 +04:00
/**
* @ group DDC - 1651
*/
public function testSetParameterBindingSingleIdentifierObjectConverted ()
{
$userC = new CmsUser ;
$userC -> name = 'Jonathan' ;
$userC -> username = 'jwage' ;
$userC -> status = 'developer' ;
$this -> _em -> persist ( $userC );
$this -> _em -> flush ();
$this -> _em -> clear ();
$q = $this -> _em -> createQuery ( " SELECT DISTINCT u from Doctrine \T ests \ Models \ CMS \ CmsUser u WHERE u.id = ?1 " );
$q -> setParameter ( 1 , $userC );
$this -> assertEquals ( $userC -> id , $q -> getParameter ( 1 ));
}
2012-05-19 20:40:01 +04:00
/**
* @ group DDC - 1822
*/
public function testUnexpectedResultException ()
{
$dql = " SELECT u FROM Doctrine \T ests \ Models \ CMS \ CmsUser u " ;
$u1 = new CmsUser ;
$u2 = new CmsUser ;
$u1 -> name = 'Fabio B. Silva' ;
$u1 -> username = 'FabioBatSilva' ;
$u1 -> status = 'developer' ;
$u2 -> name = 'Test' ;
$u2 -> username = 'test' ;
$u2 -> status = 'tester' ;
try {
$this -> _em -> createQuery ( $dql ) -> getSingleResult ();
$this -> fail ( 'Expected exception "\Doctrine\ORM\NoResultException".' );
} catch ( \Doctrine\ORM\UnexpectedResultException $exc ) {
$this -> assertInstanceOf ( '\Doctrine\ORM\NoResultException' , $exc );
}
$this -> _em -> persist ( $u1 );
$this -> _em -> persist ( $u2 );
$this -> _em -> flush ();
$this -> _em -> clear ();
try {
$this -> _em -> createQuery ( $dql ) -> getSingleResult ();
$this -> fail ( 'Expected exception "\Doctrine\ORM\NonUniqueResultException".' );
} catch ( \Doctrine\ORM\UnexpectedResultException $exc ) {
$this -> assertInstanceOf ( '\Doctrine\ORM\NonUniqueResultException' , $exc );
}
}
2012-02-18 19:07:55 +04:00
}