2009-04-09 22:12:48 +04:00
< ? php
namespace Doctrine\Tests\ORM\Hydration ;
use Doctrine\ORM\Query\ResultSetMapping ;
use Doctrine\ORM\Mapping\ClassMetadata ;
require_once __DIR__ . '/../../TestInit.php' ;
/**
* Description of ResultSetMappingTest
*
* @ author robo
*/
class ResultSetMappingTest extends \Doctrine\Tests\OrmTestCase
{
2011-12-07 17:23:15 +04:00
/**
* @ var ResultSetMapping
*/
2009-04-09 22:12:48 +04:00
private $_rsm ;
2011-12-20 01:56:19 +04:00
2011-12-07 17:23:15 +04:00
/**
* @ var \Doctrine\ORM\EntityManager
*/
2009-04-09 22:12:48 +04:00
private $_em ;
protected function setUp () {
parent :: setUp ();
$this -> _rsm = new ResultSetMapping ;
$this -> _em = $this -> _getTestEntityManager ();
}
/**
* For SQL : SELECT id , status , username , name FROM cms_users
*/
public function testBasicResultSetMapping ()
{
$this -> _rsm -> addEntityResult (
2009-05-21 23:18:14 +04:00
'Doctrine\Tests\Models\CMS\CmsUser' ,
2009-04-09 22:12:48 +04:00
'u'
);
$this -> _rsm -> addFieldResult ( 'u' , 'id' , 'id' );
$this -> _rsm -> addFieldResult ( 'u' , 'status' , 'status' );
$this -> _rsm -> addFieldResult ( 'u' , 'username' , 'username' );
$this -> _rsm -> addFieldResult ( 'u' , 'name' , 'name' );
$this -> assertFalse ( $this -> _rsm -> isScalarResult ( 'id' ));
$this -> assertFalse ( $this -> _rsm -> isScalarResult ( 'status' ));
$this -> assertFalse ( $this -> _rsm -> isScalarResult ( 'username' ));
$this -> assertFalse ( $this -> _rsm -> isScalarResult ( 'name' ));
2009-08-03 17:25:56 +04:00
$this -> assertTrue ( $this -> _rsm -> getClassName ( 'u' ) == 'Doctrine\Tests\Models\CMS\CmsUser' );
2009-12-19 16:38:54 +03:00
$class = $this -> _rsm -> getDeclaringClass ( 'id' );
2009-05-21 23:18:14 +04:00
$this -> assertTrue ( $class == 'Doctrine\Tests\Models\CMS\CmsUser' );
2009-04-09 22:12:48 +04:00
$this -> assertEquals ( 'u' , $this -> _rsm -> getEntityAlias ( 'id' ));
$this -> assertEquals ( 'u' , $this -> _rsm -> getEntityAlias ( 'status' ));
$this -> assertEquals ( 'u' , $this -> _rsm -> getEntityAlias ( 'username' ));
$this -> assertEquals ( 'u' , $this -> _rsm -> getEntityAlias ( 'name' ));
$this -> assertEquals ( 'id' , $this -> _rsm -> getFieldName ( 'id' ));
$this -> assertEquals ( 'status' , $this -> _rsm -> getFieldName ( 'status' ));
$this -> assertEquals ( 'username' , $this -> _rsm -> getFieldName ( 'username' ));
$this -> assertEquals ( 'name' , $this -> _rsm -> getFieldName ( 'name' ));
}
2011-12-20 01:56:19 +04:00
2011-12-07 17:23:15 +04:00
/**
* @ group DDC - 1057
2011-12-20 01:56:19 +04:00
*
2011-12-07 17:23:15 +04:00
* Fluent interface test , not a real result set mapping
*/
public function testFluentInterface ()
{
$rms = $this -> _rsm ;
2011-12-20 01:56:19 +04:00
2011-12-07 17:23:15 +04:00
$rms -> addEntityResult ( 'Doctrine\Tests\Models\CMS\CmsUser' , 'u' )
-> addJoinedEntityResult ( 'Doctrine\Tests\Models\CMS\CmsPhonenumber' , 'p' , 'u' , 'phonenumbers' )
-> addFieldResult ( 'u' , 'id' , 'id' )
-> addFieldResult ( 'u' , 'name' , 'name' )
-> setDiscriminatorColumn ( 'name' , 'name' )
-> addIndexByColumn ( 'id' , 'id' )
-> addIndexBy ( 'username' , 'username' )
-> addIndexByScalar ( 'sclr0' )
-> addScalarResult ( 'sclr0' , 'numPhones' )
-> addMetaResult ( 'a' , 'user_id' , 'user_id' );
2011-12-20 01:56:19 +04:00
2011-12-07 17:23:15 +04:00
$this -> assertTrue ( $rms -> hasIndexBy ( 'id' ));
$this -> assertTrue ( $rms -> isFieldResult ( 'id' ));
$this -> assertTrue ( $rms -> isFieldResult ( 'name' ));
$this -> assertTrue ( $rms -> isScalarResult ( 'sclr0' ));
$this -> assertTrue ( $rms -> isRelation ( 'p' ));
$this -> assertTrue ( $rms -> hasParentAlias ( 'p' ));
$this -> assertTrue ( $rms -> isMixedResult ());
}
2012-02-26 23:51:39 +04:00
/**
* @ group DDC - 1663
*/
2012-02-27 02:27:42 +04:00
public function testAddNamedNativeQueryResultSetMapping ()
2012-02-26 23:51:39 +04:00
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
2012-03-11 19:10:15 +04:00
$cm -> mapOneToOne ( array (
'fieldName' => 'email' ,
'targetEntity' => 'Doctrine\Tests\Models\CMS\CmsEmail' ,
'cascade' => array ( 'persist' ),
'inversedBy' => 'user' ,
'orphanRemoval' => false ,
'joinColumns' => array ( array (
'nullable' => true ,
'referencedColumnName' => 'id' ,
)
)
));
2012-02-27 02:27:42 +04:00
$cm -> addNamedNativeQuery ( array (
'name' => 'find-all' ,
'query' => 'SELECT u.id AS user_id, e.id AS email_id, u.name, e.email, u.id + e.id AS scalarColumn FROM cms_users u INNER JOIN cms_emails e ON e.id = u.email_id' ,
'resultSetMapping' => 'find-all' ,
));
2012-02-26 23:51:39 +04:00
$cm -> addSqlResultSetMapping ( array (
'name' => 'find-all' ,
'entities' => array (
array (
'entityClass' => '__CLASS__' ,
'fields' => array (
array (
'name' => 'id' ,
'column' => 'user_id'
),
array (
'name' => 'name' ,
2012-02-27 02:27:42 +04:00
'column' => 'name'
2012-02-26 23:51:39 +04:00
)
)
),
array (
'entityClass' => 'CmsEmail' ,
'fields' => array (
array (
'name' => 'id' ,
'column' => 'email_id'
),
array (
'name' => 'email' ,
2012-02-27 02:27:42 +04:00
'column' => 'email'
2012-02-26 23:51:39 +04:00
)
)
)
),
'columns' => array (
array (
'name' => 'scalarColumn'
)
)
));
2012-03-11 19:10:15 +04:00
2012-02-27 02:27:42 +04:00
$queryMapping = $cm -> getNamedNativeQuery ( 'find-all' );
2012-02-26 23:51:39 +04:00
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder ( $this -> _em );
2012-02-27 02:27:42 +04:00
$rsm -> addNamedNativeQueryMapping ( $cm , $queryMapping );
2012-02-26 23:51:39 +04:00
$this -> assertEquals ( 'scalarColumn' , $rsm -> getScalarAlias ( 'scalarColumn' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'user_id' ));
2012-02-27 02:27:42 +04:00
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'name' ));
2012-02-26 23:51:39 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getClassName ( 'c0' ));
2012-02-27 02:27:42 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'name' ));
2012-02-26 23:51:39 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'user_id' ));
$this -> assertEquals ( 'c1' , $rsm -> getEntityAlias ( 'email_id' ));
2012-02-27 02:27:42 +04:00
$this -> assertEquals ( 'c1' , $rsm -> getEntityAlias ( 'email' ));
2012-02-26 23:51:39 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsEmail' , $rsm -> getClassName ( 'c1' ));
2012-02-27 02:27:42 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsEmail' , $rsm -> getDeclaringClass ( 'email' ));
2012-02-26 23:51:39 +04:00
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsEmail' , $rsm -> getDeclaringClass ( 'email_id' ));
2012-02-27 02:27:42 +04:00
}
/**
* @ group DDC - 1663
*/
public function testAddNamedNativeQueryResultSetMappingWithoutFields ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
$cm -> addNamedNativeQuery ( array (
'name' => 'find-all' ,
'query' => 'SELECT u.id AS user_id, e.id AS email_id, u.name, e.email, u.id + e.id AS scalarColumn FROM cms_users u INNER JOIN cms_emails e ON e.id = u.email_id' ,
'resultSetMapping' => 'find-all' ,
));
$cm -> addSqlResultSetMapping ( array (
'name' => 'find-all' ,
'entities' => array (
array (
'entityClass' => '__CLASS__' ,
)
),
'columns' => array (
array (
'name' => 'scalarColumn'
)
)
));
$queryMapping = $cm -> getNamedNativeQuery ( 'find-all' );
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder ( $this -> _em );
$rsm -> addNamedNativeQueryMapping ( $cm , $queryMapping );
$this -> assertEquals ( 'scalarColumn' , $rsm -> getScalarAlias ( 'scalarColumn' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'id' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'name' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'status' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'username' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getClassName ( 'c0' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'id' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'name' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'status' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'username' ));
}
/**
* @ group DDC - 1663
*/
public function testAddNamedNativeQueryResultClass ()
{
$cm = new ClassMetadata ( 'Doctrine\Tests\Models\CMS\CmsUser' );
$cm -> initializeReflection ( new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService );
$cm -> addNamedNativeQuery ( array (
'name' => 'find-all' ,
'resultClass' => '__CLASS__' ,
'query' => 'SELECT * FROM cms_users' ,
));
$queryMapping = $cm -> getNamedNativeQuery ( 'find-all' );
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder ( $this -> _em );
$rsm -> addNamedNativeQueryMapping ( $cm , $queryMapping );
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'id' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'name' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'status' ));
$this -> assertEquals ( 'c0' , $rsm -> getEntityAlias ( 'username' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getClassName ( 'c0' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'id' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'name' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'status' ));
$this -> assertEquals ( 'Doctrine\Tests\Models\CMS\CmsUser' , $rsm -> getDeclaringClass ( 'username' ));
2012-02-26 23:51:39 +04:00
}
2013-04-02 12:14:24 +04:00
2013-04-02 12:04:15 +04:00
public function testIndexByMetadataColumn ()
{
$rms = $this -> _rsm ;
$rms -> addEntityResult ( 'Doctrine\Tests\Models\Legacy\LegacyUser' , 'u' )
-> addJoinedEntityResult ( 'Doctrine\Tests\Models\Legacy' , 'lu' , 'u' , '_references' )
-> addMetaResult ( 'lu' , '_source' , '_source' , true )
-> addMetaResult ( 'lu' , '_target' , '_target' , true )
-> addIndexBy ( 'lu' , '_source' );
$this -> assertTrue ( $rms -> hasIndexBy ( 'lu' ));
}
2009-04-09 22:12:48 +04:00
}