|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Tests\ORM\Hydration; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Types\Type; |
|
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
9
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataBuildingContext; |
|
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
|
11
|
|
|
use Doctrine\ORM\Mapping\JoinColumnMetadata; |
|
12
|
|
|
use Doctrine\ORM\Mapping\OneToOneAssociationMetadata; |
|
13
|
|
|
use Doctrine\ORM\Mapping\TableMetadata; |
|
14
|
|
|
use Doctrine\ORM\Query\ResultSetMapping; |
|
15
|
|
|
use Doctrine\ORM\Reflection\RuntimeReflectionService; |
|
16
|
|
|
use Doctrine\Tests\Models\CMS\CmsEmail; |
|
17
|
|
|
use Doctrine\Tests\Models\CMS\CmsPhonenumber; |
|
18
|
|
|
use Doctrine\Tests\Models\CMS\CmsUser; |
|
19
|
|
|
use Doctrine\Tests\Models\Legacy\LegacyUser; |
|
20
|
|
|
use Doctrine\Tests\Models\Legacy\LegacyUserReference; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Description of ResultSetMappingTest |
|
24
|
|
|
* |
|
25
|
|
|
* @author robo |
|
26
|
|
|
*/ |
|
27
|
|
|
class ResultSetMappingTest extends \Doctrine\Tests\OrmTestCase |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Doctrine\ORM\EntityManagerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $em; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ResultSetMapping |
|
36
|
|
|
*/ |
|
37
|
|
|
private $rsm; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var ClassMetadataBuildingContext |
|
41
|
|
|
*/ |
|
42
|
|
|
private $metadataBuildingContext; |
|
43
|
|
|
|
|
44
|
|
|
protected function setUp() |
|
45
|
|
|
{ |
|
46
|
|
|
parent::setUp(); |
|
47
|
|
|
|
|
48
|
|
|
$this->metadataBuildingContext = new ClassMetadataBuildingContext( |
|
49
|
|
|
$this->createMock(ClassMetadataFactory::class), |
|
50
|
|
|
new RuntimeReflectionService() |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$this->em = $this->getTestEntityManager(); |
|
54
|
|
|
$this->rsm = new ResultSetMapping; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* For SQL: SELECT id, status, username, name FROM cms_users |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testBasicResultSetMapping() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->rsm->addEntityResult( |
|
63
|
|
|
CmsUser::class, |
|
64
|
|
|
'u' |
|
65
|
|
|
); |
|
66
|
|
|
$this->rsm->addFieldResult('u', 'id', 'id'); |
|
67
|
|
|
$this->rsm->addFieldResult('u', 'status', 'status'); |
|
68
|
|
|
$this->rsm->addFieldResult('u', 'username', 'username'); |
|
69
|
|
|
$this->rsm->addFieldResult('u', 'name', 'name'); |
|
70
|
|
|
|
|
71
|
|
|
self::assertFalse($this->rsm->isScalarResult('id')); |
|
72
|
|
|
self::assertFalse($this->rsm->isScalarResult('status')); |
|
73
|
|
|
self::assertFalse($this->rsm->isScalarResult('username')); |
|
74
|
|
|
self::assertFalse($this->rsm->isScalarResult('name')); |
|
75
|
|
|
|
|
76
|
|
|
self::assertEquals($this->rsm->getClassName('u'), CmsUser::class); |
|
77
|
|
|
$class = $this->rsm->getDeclaringClass('id'); |
|
78
|
|
|
self::assertEquals($class, CmsUser::class); |
|
79
|
|
|
|
|
80
|
|
|
self::assertEquals('u', $this->rsm->getEntityAlias('id')); |
|
81
|
|
|
self::assertEquals('u', $this->rsm->getEntityAlias('status')); |
|
82
|
|
|
self::assertEquals('u', $this->rsm->getEntityAlias('username')); |
|
83
|
|
|
self::assertEquals('u', $this->rsm->getEntityAlias('name')); |
|
84
|
|
|
|
|
85
|
|
|
self::assertEquals('id', $this->rsm->getFieldName('id')); |
|
86
|
|
|
self::assertEquals('status', $this->rsm->getFieldName('status')); |
|
87
|
|
|
self::assertEquals('username', $this->rsm->getFieldName('username')); |
|
88
|
|
|
self::assertEquals('name', $this->rsm->getFieldName('name')); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @group DDC-1057 |
|
93
|
|
|
* |
|
94
|
|
|
* Fluent interface test, not a real result set mapping |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testFluentInterface() |
|
97
|
|
|
{ |
|
98
|
|
|
$rms = $this->rsm; |
|
99
|
|
|
|
|
100
|
|
|
$this->rsm->addEntityResult(CmsUser::class, 'u'); |
|
101
|
|
|
$this->rsm->addJoinedEntityResult(CmsPhonenumber::class, 'p', 'u', 'phonenumbers'); |
|
102
|
|
|
$this->rsm->addFieldResult('u', 'id', 'id'); |
|
103
|
|
|
$this->rsm->addFieldResult('u', 'name', 'name'); |
|
104
|
|
|
$this->rsm->setDiscriminatorColumn('name', 'name'); |
|
105
|
|
|
$this->rsm->addIndexByColumn('id', 'id'); |
|
106
|
|
|
$this->rsm->addIndexBy('username', 'username'); |
|
107
|
|
|
$this->rsm->addIndexByScalar('sclr0'); |
|
108
|
|
|
$this->rsm->addScalarResult('sclr0', 'numPhones', Type::getType('integer')); |
|
109
|
|
|
$this->rsm->addMetaResult('a', 'user_id', 'user_id', false, Type::getType('integer')); |
|
110
|
|
|
|
|
111
|
|
|
self::assertTrue($rms->hasIndexBy('id')); |
|
112
|
|
|
self::assertTrue($rms->isFieldResult('id')); |
|
113
|
|
|
self::assertTrue($rms->isFieldResult('name')); |
|
114
|
|
|
self::assertTrue($rms->isScalarResult('sclr0')); |
|
115
|
|
|
self::assertTrue($rms->isRelation('p')); |
|
116
|
|
|
self::assertTrue($rms->hasParentAlias('p')); |
|
117
|
|
|
self::assertTrue($rms->isMixedResult()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @group DDC-117 |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testIndexByMetadataColumn() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->rsm->addEntityResult(LegacyUser::class, 'u'); |
|
126
|
|
|
$this->rsm->addJoinedEntityResult(LegacyUserReference::class, 'lu', 'u', '_references'); |
|
127
|
|
|
$this->rsm->addMetaResult('lu', '_source', '_source', true, Type::getType('integer')); |
|
128
|
|
|
$this->rsm->addMetaResult('lu', '_target', '_target', true, Type::getType('integer')); |
|
129
|
|
|
$this->rsm->addIndexBy('lu', '_source'); |
|
130
|
|
|
|
|
131
|
|
|
self::assertTrue($this->rsm->hasIndexBy('lu')); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|