1
0
mirror of synced 2024-12-15 23:56:02 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Hydration/ResultSetMappingTest.php

273 lines
10 KiB
PHP
Raw Normal View History

<?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
*/
private $_rsm;
2011-12-20 01:56:19 +04:00
2011-12-07 17:23:15 +04:00
/**
* @var \Doctrine\ORM\EntityManager
*/
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(
'Doctrine\Tests\Models\CMS\CmsUser',
'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'));
$this->assertTrue($this->_rsm->getClassName('u') == 'Doctrine\Tests\Models\CMS\CmsUser');
2009-12-19 16:38:54 +03:00
$class = $this->_rsm->getDeclaringClass('id');
$this->assertTrue($class == 'Doctrine\Tests\Models\CMS\CmsUser');
$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
*/
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',
)
)
));
$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',
'column'=> 'name'
2012-02-26 23:51:39 +04:00
)
)
),
array(
'entityClass' => 'CmsEmail',
'fields' => array(
array(
'name' => 'id',
'column'=> 'email_id'
),
array(
'name' => 'email',
'column'=> 'email'
2012-02-26 23:51:39 +04:00
)
)
)
),
'columns' => array(
array(
'name' => 'scalarColumn'
)
)
));
2012-03-11 19:10:15 +04:00
$queryMapping = $cm->getNamedNativeQuery('find-all');
2012-02-26 23:51:39 +04:00
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->_em);
$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'));
$this->assertEquals('c0', $rsm->getEntityAlias('name'));
2012-02-26 23:51:39 +04:00
$this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $rsm->getClassName('c0'));
$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'));
$this->assertEquals('c1', $rsm->getEntityAlias('email'));
2012-02-26 23:51:39 +04:00
$this->assertEquals('Doctrine\Tests\Models\CMS\CmsEmail', $rsm->getClassName('c1'));
$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'));
}
/**
* @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-29 12:29:58 +04:00
/**
* @group DDC-117
*/
2013-04-02 12:04:15 +04:00
public function testIndexByMetadataColumn()
{
2013-04-29 12:29:58 +04:00
$this->_rsm->addEntityResult('Doctrine\Tests\Models\Legacy\LegacyUser', 'u');
$this->_rsm->addJoinedEntityResult('Doctrine\Tests\Models\Legacy', 'lu', 'u', '_references');
$this->_rsm->addMetaResult('lu', '_source', '_source', true);
$this->_rsm->addMetaResult('lu', '_target', '_target', true);
$this->_rsm->addIndexBy('lu', '_source');
2013-04-02 12:04:15 +04:00
2013-04-29 12:29:58 +04:00
$this->assertTrue($this->_rsm->hasIndexBy('lu'));
2013-04-02 12:04:15 +04:00
}
}