2009-08-14 15:04:31 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\Tests\Models\CMS\CmsUser ;
2013-01-14 04:39:59 +04:00
use Doctrine\Tests\Models\CMS\CmsEmail ;
2010-09-28 00:13:14 +04:00
use Doctrine\Tests\Models\CMS\CmsAddress ;
2011-08-14 04:28:54 +04:00
use Doctrine\Tests\Models\CMS\CmsPhonenumber ;
2012-06-19 02:01:03 +04:00
use Doctrine\Common\Collections\Criteria ;
2009-08-14 15:04:31 +04:00
require_once __DIR__ . '/../../TestInit.php' ;
/**
* @ author robo
*/
class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp () {
$this -> useModelSet ( 'cms' );
parent :: setUp ();
}
2011-08-14 04:28:54 +04:00
public function tearDown ()
{
2011-10-30 11:25:19 +04:00
if ( $this -> _em ) {
$this -> _em -> getConfiguration () -> setEntityNamespaces ( array ());
}
2011-08-14 04:28:54 +04:00
parent :: tearDown ();
}
2010-09-27 23:03:12 +04:00
public function loadFixture ()
{
2009-08-14 15:04:31 +04:00
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'freak' ;
$this -> _em -> persist ( $user );
2010-02-24 05:25:09 +03:00
2009-08-14 15:04:31 +04:00
$user2 = new CmsUser ;
$user2 -> name = 'Guilherme' ;
$user2 -> username = 'gblanco' ;
$user2 -> status = 'dev' ;
$this -> _em -> persist ( $user2 );
2010-02-24 05:25:09 +03:00
2011-10-17 20:54:20 +04:00
$user3 = new CmsUser ;
$user3 -> name = 'Benjamin' ;
$user3 -> username = 'beberlei' ;
$user3 -> status = null ;
$this -> _em -> persist ( $user3 );
2012-03-12 01:30:24 +04:00
$user4 = new CmsUser ;
$user4 -> name = 'Alexander' ;
$user4 -> username = 'asm89' ;
$user4 -> status = 'dev' ;
$this -> _em -> persist ( $user4 );
2009-08-14 15:04:31 +04:00
$this -> _em -> flush ();
2011-10-17 20:54:20 +04:00
2009-08-14 15:04:31 +04:00
$user1Id = $user -> getId ();
2011-10-17 20:54:20 +04:00
2009-08-14 15:04:31 +04:00
unset ( $user );
unset ( $user2 );
2011-10-17 20:54:20 +04:00
unset ( $user3 );
2012-03-12 01:30:24 +04:00
unset ( $user4 );
2011-10-17 20:54:20 +04:00
2009-08-14 15:04:31 +04:00
$this -> _em -> clear ();
2010-09-27 23:03:12 +04:00
return $user1Id ;
}
2011-10-17 20:54:20 +04:00
2011-08-14 04:28:54 +04:00
public function loadAssociatedFixture ()
{
$address = new CmsAddress ();
$address -> city = " Berlin " ;
$address -> country = " Germany " ;
$address -> street = " Foostreet " ;
$address -> zip = " 12345 " ;
$user = new CmsUser ();
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'freak' ;
$user -> setAddress ( $address );
$this -> _em -> persist ( $user );
$this -> _em -> persist ( $address );
$this -> _em -> flush ();
$this -> _em -> clear ();
return array ( $user -> id , $address -> id );
}
2011-10-30 11:25:19 +04:00
2013-01-14 04:39:59 +04:00
public function loadFixtureUserEmail ()
{
$user1 = new CmsUser ();
$user2 = new CmsUser ();
$user3 = new CmsUser ();
$email1 = new CmsEmail ();
$email2 = new CmsEmail ();
$email3 = new CmsEmail ();
$user1 -> name = 'Test 1' ;
$user1 -> username = 'test1' ;
$user1 -> status = 'active' ;
$user2 -> name = 'Test 2' ;
$user2 -> username = 'test2' ;
$user2 -> status = 'active' ;
$user3 -> name = 'Test 3' ;
$user3 -> username = 'test3' ;
$user3 -> status = 'active' ;
$email1 -> email = 'test1@test.com' ;
$email2 -> email = 'test2@test.com' ;
$email3 -> email = 'test3@test.com' ;
$user1 -> setEmail ( $email1 );
$user2 -> setEmail ( $email2 );
$user3 -> setEmail ( $email3 );
$this -> _em -> persist ( $user1 );
$this -> _em -> persist ( $user2 );
$this -> _em -> persist ( $user3 );
$this -> _em -> persist ( $email1 );
$this -> _em -> persist ( $email2 );
$this -> _em -> persist ( $email3 );
$this -> _em -> flush ();
$this -> _em -> clear ();
return array ( $user1 , $user2 , $user3 );
}
2011-08-14 04:28:54 +04:00
public function buildUser ( $name , $username , $status , $address )
{
$user = new CmsUser ();
$user -> name = $name ;
$user -> username = $username ;
$user -> status = $status ;
$user -> setAddress ( $address );
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
return $user ;
}
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
public function buildAddress ( $country , $city , $street , $zip )
{
$address = new CmsAddress ();
$address -> country = $country ;
$address -> city = $city ;
$address -> street = $street ;
$address -> zip = $zip ;
$this -> _em -> persist ( $address );
$this -> _em -> flush ();
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
return $address ;
}
2010-09-27 23:03:12 +04:00
public function testBasicFind ()
{
$user1Id = $this -> loadFixture ();
2009-08-14 15:04:31 +04:00
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2010-02-24 05:25:09 +03:00
2009-08-14 15:04:31 +04:00
$user = $repos -> find ( $user1Id );
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $user );
2009-08-14 15:04:31 +04:00
$this -> assertEquals ( 'Roman' , $user -> name );
$this -> assertEquals ( 'freak' , $user -> status );
2010-09-27 23:03:12 +04:00
}
2010-02-24 05:25:09 +03:00
2010-09-27 23:03:12 +04:00
public function testFindByField ()
{
$user1Id = $this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2010-02-24 05:25:09 +03:00
2009-08-14 15:04:31 +04:00
$users = $repos -> findBy ( array ( 'status' => 'dev' ));
2012-03-12 01:30:24 +04:00
$this -> assertEquals ( 2 , count ( $users ));
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $users [ 0 ]);
2009-08-14 15:04:31 +04:00
$this -> assertEquals ( 'Guilherme' , $users [ 0 ] -> name );
$this -> assertEquals ( 'dev' , $users [ 0 ] -> status );
2010-09-27 23:03:12 +04:00
}
2010-02-24 05:25:09 +03:00
2011-08-14 04:28:54 +04:00
public function testFindByAssociationWithIntegerAsParameter ()
{
$address1 = $this -> buildAddress ( 'Germany' , 'Berlim' , 'Foo st.' , '123456' );
$user1 = $this -> buildUser ( 'Benjamin' , 'beberlei' , 'dev' , $address1 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$address2 = $this -> buildAddress ( 'Brazil' , 'São Paulo' , 'Bar st.' , '654321' );
$user2 = $this -> buildUser ( 'Guilherme' , 'guilhermeblanco' , 'freak' , $address2 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$address3 = $this -> buildAddress ( 'USA' , 'Nashville' , 'Woo st.' , '321654' );
$user3 = $this -> buildUser ( 'Jonathan' , 'jwage' , 'dev' , $address3 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
unset ( $address1 );
unset ( $address2 );
unset ( $address3 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$this -> _em -> clear ();
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
$addresses = $repository -> findBy ( array ( 'user' => array ( $user1 -> getId (), $user2 -> getId ())));
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$this -> assertEquals ( 2 , count ( $addresses ));
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $addresses [ 0 ]);
}
public function testFindByAssociationWithObjectAsParameter ()
{
$address1 = $this -> buildAddress ( 'Germany' , 'Berlim' , 'Foo st.' , '123456' );
$user1 = $this -> buildUser ( 'Benjamin' , 'beberlei' , 'dev' , $address1 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$address2 = $this -> buildAddress ( 'Brazil' , 'São Paulo' , 'Bar st.' , '654321' );
$user2 = $this -> buildUser ( 'Guilherme' , 'guilhermeblanco' , 'freak' , $address2 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$address3 = $this -> buildAddress ( 'USA' , 'Nashville' , 'Woo st.' , '321654' );
$user3 = $this -> buildUser ( 'Jonathan' , 'jwage' , 'dev' , $address3 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
unset ( $address1 );
unset ( $address2 );
unset ( $address3 );
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$this -> _em -> clear ();
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
$addresses = $repository -> findBy ( array ( 'user' => array ( $user1 , $user2 )));
2011-10-30 11:25:19 +04:00
2011-08-14 04:28:54 +04:00
$this -> assertEquals ( 2 , count ( $addresses ));
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $addresses [ 0 ]);
}
2010-09-27 23:03:12 +04:00
public function testFindFieldByMagicCall ()
{
$user1Id = $this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2010-02-24 05:25:09 +03:00
2009-08-14 15:04:31 +04:00
$users = $repos -> findByStatus ( 'dev' );
2012-03-12 01:30:24 +04:00
$this -> assertEquals ( 2 , count ( $users ));
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $users [ 0 ]);
2009-08-14 15:04:31 +04:00
$this -> assertEquals ( 'Guilherme' , $users [ 0 ] -> name );
$this -> assertEquals ( 'dev' , $users [ 0 ] -> status );
2010-09-27 23:03:12 +04:00
}
2011-10-30 11:25:19 +04:00
2010-09-27 23:03:12 +04:00
public function testFindAll ()
{
$user1Id = $this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2010-02-24 05:25:09 +03:00
2009-08-14 15:04:31 +04:00
$users = $repos -> findAll ();
2012-03-12 01:30:24 +04:00
$this -> assertEquals ( 4 , count ( $users ));
2010-09-27 23:03:12 +04:00
}
2010-03-03 04:30:00 +03:00
2010-09-27 23:03:12 +04:00
public function testFindByAlias ()
{
$user1Id = $this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2010-03-03 04:30:00 +03:00
$this -> _em -> getConfiguration () -> addEntityNamespace ( 'CMS' , 'Doctrine\Tests\Models\CMS' );
$repos = $this -> _em -> getRepository ( 'CMS:CmsUser' );
$users = $repos -> findAll ();
2012-03-12 01:30:24 +04:00
$this -> assertEquals ( 4 , count ( $users ));
2010-09-27 23:03:12 +04:00
}
2010-03-03 04:30:00 +03:00
2010-02-24 05:25:09 +03:00
/**
* @ expectedException \Doctrine\ORM\ORMException
*/
public function testExceptionIsThrownWhenCallingFindByWithoutParameter () {
$this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' )
-> findByStatus ();
}
/**
* @ expectedException \Doctrine\ORM\ORMException
*/
public function testExceptionIsThrownWhenUsingInvalidFieldName () {
$this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' )
-> findByThisFieldDoesNotExist ( 'testvalue' );
2009-08-14 15:04:31 +04:00
}
2010-04-09 00:50:06 +04:00
/**
* @ group locking
* @ group DDC - 178
*/
public function testPessimisticReadLockWithoutTransaction_ThrowsException ()
{
$this -> setExpectedException ( 'Doctrine\ORM\TransactionRequiredException' );
$this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' )
2010-05-15 13:48:20 +04:00
-> find ( 1 , \Doctrine\DBAL\LockMode :: PESSIMISTIC_READ );
2010-04-09 00:50:06 +04:00
}
/**
* @ group locking
* @ group DDC - 178
*/
public function testPessimisticWriteLockWithoutTransaction_ThrowsException ()
{
$this -> setExpectedException ( 'Doctrine\ORM\TransactionRequiredException' );
$this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' )
2010-05-15 13:48:20 +04:00
-> find ( 1 , \Doctrine\DBAL\LockMode :: PESSIMISTIC_WRITE );
2010-04-09 00:50:06 +04:00
}
/**
* @ group locking
* @ group DDC - 178
*/
public function testOptimisticLockUnversionedEntity_ThrowsException ()
{
$this -> setExpectedException ( 'Doctrine\ORM\OptimisticLockException' );
$this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' )
2010-05-15 13:48:20 +04:00
-> find ( 1 , \Doctrine\DBAL\LockMode :: OPTIMISTIC );
2010-04-09 00:50:06 +04:00
}
/**
* @ group locking
* @ group DDC - 178
*/
public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException ()
{
$user = new CmsUser ;
$user -> name = 'Roman' ;
$user -> username = 'romanb' ;
$user -> status = 'freak' ;
$this -> _em -> persist ( $user );
$this -> _em -> flush ();
$userId = $user -> id ;
2011-03-13 02:15:50 +03:00
$this -> _em -> find ( 'Doctrine\Tests\Models\CMS\CmsUser' , $userId );
2011-10-30 11:25:19 +04:00
2010-04-11 18:43:33 +04:00
$this -> setExpectedException ( 'Doctrine\ORM\OptimisticLockException' );
2011-03-13 02:15:50 +03:00
$this -> _em -> find ( 'Doctrine\Tests\Models\CMS\CmsUser' , $userId , \Doctrine\DBAL\LockMode :: OPTIMISTIC );
2010-04-09 00:50:06 +04:00
}
2010-09-27 23:03:12 +04:00
/**
* @ group DDC - 819
*/
public function testFindMagicCallByNullValue ()
{
$this -> loadFixture ();
2011-10-17 20:54:20 +04:00
2010-09-27 23:03:12 +04:00
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repos -> findByStatus ( null );
2011-10-17 20:54:20 +04:00
$this -> assertEquals ( 1 , count ( $users ));
2010-09-27 23:03:12 +04:00
}
/**
* @ group DDC - 819
*/
public function testInvalidMagicCall ()
{
$this -> setExpectedException ( 'BadMethodCallException' );
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$repos -> foo ();
}
2010-09-28 00:13:14 +04:00
/**
* @ group DDC - 817
*/
public function testFindByAssociationKey_ExceptionOnInverseSide ()
{
list ( $userId , $addressId ) = $this -> loadAssociatedFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$this -> setExpectedException ( 'Doctrine\ORM\ORMException' , " You cannot search for the association field 'Doctrine \T ests \ Models \ CMS \ CmsUser#address', because it is the inverse side of an association. Find methods only work on owning side associations. " );
$user = $repos -> findBy ( array ( 'address' => $addressId ));
}
/**
* @ group DDC - 817
*/
public function testFindOneByAssociationKey ()
{
list ( $userId , $addressId ) = $this -> loadAssociatedFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
$address = $repos -> findOneBy ( array ( 'user' => $userId ));
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $address );
2010-09-28 00:13:14 +04:00
$this -> assertEquals ( $addressId , $address -> id );
}
2012-11-05 22:55:54 +04:00
/**
* @ group DDC - 1241
*/
public function testFindOneByOrderBy ()
{
$this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$userAsc = $repos -> findOneBy ( array (), array ( " username " => " ASC " ));
$userDesc = $repos -> findOneBy ( array (), array ( " username " => " DESC " ));
$this -> assertNotSame ( $userAsc , $userDesc );
}
2010-09-28 00:13:14 +04:00
/**
* @ group DDC - 817
*/
public function testFindByAssociationKey ()
{
list ( $userId , $addressId ) = $this -> loadAssociatedFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
$addresses = $repos -> findBy ( array ( 'user' => $userId ));
$this -> assertContainsOnly ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $addresses );
$this -> assertEquals ( 1 , count ( $addresses ));
$this -> assertEquals ( $addressId , $addresses [ 0 ] -> id );
}
/**
* @ group DDC - 817
*/
public function testFindAssociationByMagicCall ()
{
list ( $userId , $addressId ) = $this -> loadAssociatedFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
$addresses = $repos -> findByUser ( $userId );
$this -> assertContainsOnly ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $addresses );
$this -> assertEquals ( 1 , count ( $addresses ));
$this -> assertEquals ( $addressId , $addresses [ 0 ] -> id );
}
/**
* @ group DDC - 817
*/
public function testFindOneAssociationByMagicCall ()
{
list ( $userId , $addressId ) = $this -> loadAssociatedFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsAddress' );
$address = $repos -> findOneByUser ( $userId );
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $address );
2010-09-28 00:13:14 +04:00
$this -> assertEquals ( $addressId , $address -> id );
}
2011-03-07 00:45:09 +03:00
public function testValidNamedQueryRetrieval ()
{
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$query = $repos -> createNamedQuery ( 'all' );
2011-07-26 13:38:09 +04:00
$this -> assertInstanceOf ( 'Doctrine\ORM\Query' , $query );
2011-03-07 00:45:09 +03:00
$this -> assertEquals ( 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u' , $query -> getDQL ());
}
public function testInvalidNamedQueryRetrieval ()
{
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$this -> setExpectedException ( 'Doctrine\ORM\Mapping\MappingException' );
$repos -> createNamedQuery ( 'invalidNamedQuery' );
}
2011-04-03 11:03:43 +04:00
/**
* @ group DDC - 1087
*/
2011-10-17 22:53:04 +04:00
public function testIsNullCriteriaDoesNotGenerateAParameter ()
2011-04-03 11:03:43 +04:00
{
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repos -> findBy ( array ( 'status' => null , 'username' => 'romanb' ));
$params = $this -> _sqlLoggerStack -> queries [ $this -> _sqlLoggerStack -> currentQuery ][ 'params' ];
$this -> assertEquals ( 1 , count ( $params ), " Should only execute with one parameter. " );
$this -> assertEquals ( array ( 'romanb' ), $params );
}
2011-05-01 01:18:24 +04:00
2011-10-17 22:53:04 +04:00
public function testIsNullCriteria ()
{
$this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repos -> findBy ( array ( 'status' => null ));
$this -> assertEquals ( 1 , count ( $users ));
}
2011-05-01 01:18:24 +04:00
/**
* @ group DDC - 1094
*/
public function testFindByLimitOffset ()
{
$this -> loadFixture ();
2011-10-30 11:25:19 +04:00
2011-05-01 01:18:24 +04:00
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users1 = $repos -> findBy ( array (), null , 1 , 0 );
$users2 = $repos -> findBy ( array (), null , 1 , 1 );
2012-03-12 01:30:24 +04:00
$this -> assertEquals ( 4 , count ( $repos -> findBy ( array ())));
2011-05-01 01:18:24 +04:00
$this -> assertEquals ( 1 , count ( $users1 ));
$this -> assertEquals ( 1 , count ( $users2 ));
$this -> assertNotSame ( $users1 [ 0 ], $users2 [ 0 ]);
}
/**
* @ group DDC - 1094
*/
public function testFindByOrderBy ()
{
$this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$usersAsc = $repos -> findBy ( array (), array ( " username " => " ASC " ));
$usersDesc = $repos -> findBy ( array (), array ( " username " => " DESC " ));
2012-03-12 01:30:24 +04:00
$this -> assertEquals ( 4 , count ( $usersAsc ), " Pre-condition: only four users in fixture " );
$this -> assertEquals ( 4 , count ( $usersDesc ), " Pre-condition: only four users in fixture " );
$this -> assertSame ( $usersAsc [ 0 ], $usersDesc [ 3 ]);
$this -> assertSame ( $usersAsc [ 3 ], $usersDesc [ 0 ]);
2011-05-01 01:18:24 +04:00
}
2011-10-30 11:25:19 +04:00
2013-01-14 04:39:59 +04:00
/**
* @ group DDC - 1376
*/
public function testFindByOrderByAssociation ()
{
$this -> loadFixtureUserEmail ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$resultAsc = $repository -> findBy ( array (), array ( 'email' => 'ASC' ));
$resultDesc = $repository -> findBy ( array (), array ( 'email' => 'DESC' ));
$this -> assertCount ( 3 , $resultAsc );
$this -> assertCount ( 3 , $resultDesc );
$this -> assertEquals ( $resultAsc [ 0 ] -> getEmail () -> getId (), $resultDesc [ 2 ] -> getEmail () -> getId ());
$this -> assertEquals ( $resultAsc [ 2 ] -> getEmail () -> getId (), $resultDesc [ 0 ] -> getEmail () -> getId ());
}
2012-03-12 01:30:24 +04:00
/**
* @ group DDC - 1426
*/
public function testFindFieldByMagicCallOrderBy ()
{
$this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$usersAsc = $repos -> findByStatus ( 'dev' , array ( 'username' => " ASC " ));
$usersDesc = $repos -> findByStatus ( 'dev' , array ( 'username' => " DESC " ));
$this -> assertEquals ( 2 , count ( $usersAsc ));
$this -> assertEquals ( 2 , count ( $usersDesc ));
$this -> assertInstanceOf ( 'Doctrine\Tests\Models\CMS\CmsUser' , $usersAsc [ 0 ]);
$this -> assertEquals ( 'Alexander' , $usersAsc [ 0 ] -> name );
$this -> assertEquals ( 'dev' , $usersAsc [ 0 ] -> status );
$this -> assertSame ( $usersAsc [ 0 ], $usersDesc [ 1 ]);
$this -> assertSame ( $usersAsc [ 1 ], $usersDesc [ 0 ]);
}
/**
* @ group DDC - 1426
*/
public function testFindFieldByMagicCallLimitOffset ()
{
$this -> loadFixture ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users1 = $repos -> findByStatus ( 'dev' , array (), 1 , 0 );
$users2 = $repos -> findByStatus ( 'dev' , array (), 1 , 1 );
$this -> assertEquals ( 1 , count ( $users1 ));
$this -> assertEquals ( 1 , count ( $users2 ));
$this -> assertNotSame ( $users1 [ 0 ], $users2 [ 0 ]);
}
2011-10-30 11:25:19 +04:00
2011-09-08 22:54:49 +04:00
/**
* @ group DDC - 753
*/
public function testDefaultRepositoryClassName ()
{
$this -> assertEquals ( $this -> _em -> getConfiguration () -> getDefaultRepositoryClassName (), " Doctrine \ ORM \ EntityRepository " );
$this -> _em -> getConfiguration () -> setDefaultRepositoryClassName ( " Doctrine \T ests \ Models \ DDC753 \ DDC753DefaultRepository " );
$this -> assertEquals ( $this -> _em -> getConfiguration () -> getDefaultRepositoryClassName (), " Doctrine \T ests \ Models \ DDC753 \ DDC753DefaultRepository " );
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository' );
$this -> assertInstanceOf ( " Doctrine \T ests \ Models \ DDC753 \ DDC753DefaultRepository " , $repos );
$this -> assertTrue ( $repos -> isDefaultRepository ());
2011-10-30 11:25:19 +04:00
2011-09-08 22:54:49 +04:00
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository' );
$this -> assertInstanceOf ( " Doctrine \T ests \ Models \ DDC753 \ DDC753CustomRepository " , $repos );
$this -> assertTrue ( $repos -> isCustomRepository ());
2011-10-30 11:25:19 +04:00
2011-09-08 22:54:49 +04:00
$this -> assertEquals ( $this -> _em -> getConfiguration () -> getDefaultRepositoryClassName (), " Doctrine \T ests \ Models \ DDC753 \ DDC753DefaultRepository " );
$this -> _em -> getConfiguration () -> setDefaultRepositoryClassName ( " Doctrine \ ORM \ EntityRepository " );
$this -> assertEquals ( $this -> _em -> getConfiguration () -> getDefaultRepositoryClassName (), " Doctrine \ ORM \ EntityRepository " );
}
2011-10-30 11:25:19 +04:00
2011-09-08 22:54:49 +04:00
/**
* @ group DDC - 753
* @ expectedException Doctrine\ORM\ORMException
2012-07-26 23:50:51 +04:00
* @ expectedExceptionMessage Invalid repository class ' Doctrine\Tests\Models\DDC753\DDC753InvalidRepository ' . It must be a Doctrine\Common\Persistence\ObjectRepository .
2011-09-08 22:54:49 +04:00
*/
public function testSetDefaultRepositoryInvalidClassError ()
{
$this -> assertEquals ( $this -> _em -> getConfiguration () -> getDefaultRepositoryClassName (), " Doctrine \ ORM \ EntityRepository " );
$this -> _em -> getConfiguration () -> setDefaultRepositoryClassName ( " Doctrine \T ests \ Models \ DDC753 \ DDC753InvalidRepository " );
}
2011-10-30 11:25:19 +04:00
2013-01-14 04:39:59 +04:00
/**
* @ group DDC - 1376
*
* @ expectedException Doctrine\ORM\ORMException
* @ expectedExceptionMessage You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address' , because it is the inverse side of an association .
*/
public function testInvalidOrderByAsssociation ()
{
$this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' )
-> findBy ( array ( 'status' => 'test' ), array ( 'address' => 'ASC' ));
}
2011-11-21 18:04:14 +04:00
/**
* @ group DDC - 1500
*/
public function testInvalidOrientation ()
{
$this -> setExpectedException ( 'Doctrine\ORM\ORMException' , 'Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username' );
$repo = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$repo -> findBy ( array ( 'status' => 'test' ), array ( 'username' => 'INVALID' ));
}
2012-05-27 19:22:16 +04:00
/**
* @ group DDC - 1713
*/
public function testFindByAssocationArray ()
{
$repo = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsArticle' );
$data = $repo -> findBy ( array ( 'user' => array ( 1 , 2 , 3 )));
$query = array_pop ( $this -> _sqlLoggerStack -> queries );
$this -> assertEquals ( array ( 1 , 2 , 3 ), $query [ 'params' ][ 0 ]);
$this -> assertEquals ( \Doctrine\DBAL\Connection :: PARAM_INT_ARRAY , $query [ 'types' ][ 0 ]);
}
2012-06-19 02:01:03 +04:00
/**
* @ group DDC - 1637
*/
public function testMatchingEmptyCriteria ()
{
$this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria ());
$this -> assertEquals ( 4 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaEqComparison ()
{
$this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> eq ( 'username' , 'beberlei' )
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 1 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaNeqComparison ()
{
$this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> neq ( 'username' , 'beberlei' )
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 3 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaInComparison ()
{
$this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> in ( 'username' , array ( 'beberlei' , 'gblanco' ))
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 2 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaNotInComparison ()
{
$this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> notIn ( 'username' , array ( 'beberlei' , 'gblanco' , 'asm89' ))
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 1 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaLtComparison ()
{
$firstUserId = $this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> lt ( 'id' , $firstUserId + 1 )
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 1 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaLeComparison ()
{
$firstUserId = $this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> lte ( 'id' , $firstUserId + 1 )
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 2 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaGtComparison ()
{
$firstUserId = $this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> gt ( 'id' , $firstUserId )
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 3 , count ( $users ));
}
/**
* @ group DDC - 1637
*/
public function testMatchingCriteriaGteComparison ()
{
$firstUserId = $this -> loadFixture ();
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$users = $repository -> matching ( new Criteria (
2012-08-01 23:37:22 +04:00
Criteria :: expr () -> gte ( 'id' , $firstUserId )
2012-06-19 02:01:03 +04:00
));
$this -> assertEquals ( 4 , count ( $users ));
}
2012-11-19 20:45:14 +04:00
/**
* @ group DDC - 2055
*/
public function testCreateResultSetMappingBuilder ()
{
$repository = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$rsm = $repository -> createResultSetMappingBuilder ( 'u' );
$this -> assertInstanceOf ( 'Doctrine\ORM\Query\ResultSetMappingBuilder' , $rsm );
$this -> assertEquals ( array ( 'u' => 'Doctrine\Tests\Models\CMS\CmsUser' ), $rsm -> aliasMap );
}
2009-08-14 15:04:31 +04:00
}