2009-08-14 15:04:31 +04:00
< ? php
namespace Doctrine\Tests\ORM\Functional ;
use Doctrine\Tests\Models\CMS\CmsUser ;
use Doctrine\Tests\Models\CMS\CmsPhonenumber ;
2010-09-28 00:13:14 +04:00
use Doctrine\Tests\Models\CMS\CmsAddress ;
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 ();
}
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
2009-08-14 15:04:31 +04:00
$this -> _em -> flush ();
$user1Id = $user -> getId ();
unset ( $user );
unset ( $user2 );
$this -> _em -> clear ();
2010-09-27 23:03:12 +04:00
return $user1Id ;
}
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 );
$this -> assertTrue ( $user instanceof CmsUser );
$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' ));
$this -> assertEquals ( 1 , count ( $users ));
$this -> assertTrue ( $users [ 0 ] instanceof CmsUser );
$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
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' );
$this -> assertEquals ( 1 , count ( $users ));
$this -> assertTrue ( $users [ 0 ] instanceof CmsUser );
$this -> assertEquals ( 'Guilherme' , $users [ 0 ] -> name );
$this -> assertEquals ( 'dev' , $users [ 0 ] -> status );
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 ();
2010-02-24 05:25:09 +03:00
$this -> assertEquals ( 2 , 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 ();
$this -> assertEquals ( 2 , 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 tearDown ()
{
2010-03-03 04:30:00 +03:00
$this -> _em -> getConfiguration () -> setEntityNamespaces ( array ());
2010-09-27 23:03:12 +04:00
parent :: tearDown ();
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 );
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 ();
$repos = $this -> _em -> getRepository ( 'Doctrine\Tests\Models\CMS\CmsUser' );
2010-09-28 00:13:14 +04:00
$this -> setExpectedException ( 'Doctrine\ORM\ORMException' );
2010-09-27 23:03:12 +04:00
$users = $repos -> findByStatus ( null );
}
/**
* @ 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
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 );
}
/**
* @ 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 ));
$this -> assertType ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $address );
$this -> assertEquals ( $addressId , $address -> id );
}
/**
* @ 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 );
$this -> assertType ( 'Doctrine\Tests\Models\CMS\CmsAddress' , $address );
$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' );
$this -> assertType ( 'Doctrine\ORM\Query' , $query );
$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' );
}
2009-08-14 15:04:31 +04:00
}