1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Tools; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory; |
6
|
|
|
use Doctrine\ORM\Tools\ResolveTargetEntityListener; |
7
|
|
|
use Doctrine\ORM\Events; |
8
|
|
|
use Doctrine\Tests\OrmTestCase; |
9
|
|
|
|
10
|
|
|
class ResolveTargetEntityListenerTest extends OrmTestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Doctrine\ORM\EntityManager |
14
|
|
|
*/ |
15
|
|
|
private $em; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ResolveTargetEntityListener |
19
|
|
|
*/ |
20
|
|
|
private $listener; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ClassMetadataFactory |
24
|
|
|
*/ |
25
|
|
|
private $factory; |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
$annotationDriver = $this->createAnnotationDriver(); |
30
|
|
|
|
31
|
|
|
$this->em = $this->_getTestEntityManager(); |
32
|
|
|
$this->em->getConfiguration()->setMetadataDriverImpl($annotationDriver); |
33
|
|
|
$this->factory = $this->em->getMetadataFactory(); |
34
|
|
|
$this->listener = new ResolveTargetEntityListener(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @group DDC-1544 |
39
|
|
|
*/ |
40
|
|
|
public function testResolveTargetEntityListenerCanResolveTargetEntity() |
41
|
|
|
{ |
42
|
|
|
$evm = $this->em->getEventManager(); |
43
|
|
|
$this->listener->addResolveTargetEntity(ResolveTargetInterface::class, ResolveTargetEntity::class, []); |
44
|
|
|
$this->listener->addResolveTargetEntity(TargetInterface::class, TargetEntity::class, []); |
45
|
|
|
$evm->addEventSubscriber($this->listener); |
46
|
|
|
|
47
|
|
|
$cm = $this->factory->getMetadataFor(ResolveTargetEntity::class); |
48
|
|
|
$meta = $cm->associationMappings; |
|
|
|
|
49
|
|
|
|
50
|
|
|
$this->assertSame(TargetEntity::class, $meta['manyToMany']['targetEntity']); |
51
|
|
|
$this->assertSame(ResolveTargetEntity::class, $meta['manyToOne']['targetEntity']); |
52
|
|
|
$this->assertSame(ResolveTargetEntity::class, $meta['oneToMany']['targetEntity']); |
53
|
|
|
$this->assertSame(TargetEntity::class, $meta['oneToOne']['targetEntity']); |
54
|
|
|
|
55
|
|
|
$this->assertSame($cm, $this->factory->getMetadataFor(ResolveTargetInterface::class)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @group DDC-3385 |
60
|
|
|
* @group 1181 |
61
|
|
|
* @group 385 |
62
|
|
|
*/ |
63
|
|
|
public function testResolveTargetEntityListenerCanRetrieveTargetEntityByInterfaceName() |
64
|
|
|
{ |
65
|
|
|
$this->listener->addResolveTargetEntity(ResolveTargetInterface::class, ResolveTargetEntity::class, []); |
66
|
|
|
|
67
|
|
|
$this->em->getEventManager()->addEventSubscriber($this->listener); |
68
|
|
|
|
69
|
|
|
$cm = $this->factory->getMetadataFor(ResolveTargetInterface::class); |
70
|
|
|
|
71
|
|
|
$this->assertSame($this->factory->getMetadataFor(ResolveTargetEntity::class), $cm); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @group DDC-2109 |
76
|
|
|
*/ |
77
|
|
|
public function testAssertTableColumnsAreNotAddedInManyToMany() |
78
|
|
|
{ |
79
|
|
|
$evm = $this->em->getEventManager(); |
80
|
|
|
$this->listener->addResolveTargetEntity(ResolveTargetInterface::class, ResolveTargetEntity::class, []); |
81
|
|
|
$this->listener->addResolveTargetEntity(TargetInterface::class, TargetEntity::class, []); |
82
|
|
|
|
83
|
|
|
$evm->addEventListener(Events::loadClassMetadata, $this->listener); |
84
|
|
|
$cm = $this->factory->getMetadataFor(ResolveTargetEntity::class); |
85
|
|
|
$meta = $cm->associationMappings['manyToMany']; |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->assertSame(TargetEntity::class, $meta['targetEntity']); |
88
|
|
|
$this->assertEquals(['resolvetargetentity_id', 'targetinterface_id'], $meta['joinTableColumns']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @group 1572 |
93
|
|
|
* @group functional |
94
|
|
|
* |
95
|
|
|
* @coversNothing |
96
|
|
|
*/ |
97
|
|
|
public function testDoesResolveTargetEntitiesInDQLAlsoWithInterfaces() |
98
|
|
|
{ |
99
|
|
|
$evm = $this->em->getEventManager(); |
100
|
|
|
$this->listener->addResolveTargetEntity(ResolveTargetInterface::class, ResolveTargetEntity::class, []); |
101
|
|
|
|
102
|
|
|
$evm->addEventSubscriber($this->listener); |
103
|
|
|
|
104
|
|
|
$this->assertStringMatchesFormat( |
105
|
|
|
'SELECT%AFROM ResolveTargetEntity%A', |
106
|
|
|
$this |
|
|
|
|
107
|
|
|
->em |
108
|
|
|
->createQuery('SELECT f FROM Doctrine\Tests\ORM\Tools\ResolveTargetInterface f') |
109
|
|
|
->getSQL() |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
interface ResolveTargetInterface |
115
|
|
|
{ |
116
|
|
|
public function getId(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
interface TargetInterface extends ResolveTargetInterface |
120
|
|
|
{ |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @Entity |
125
|
|
|
*/ |
126
|
|
|
class ResolveTargetEntity implements ResolveTargetInterface |
127
|
|
|
{ |
128
|
|
|
/** |
129
|
|
|
* @Id |
130
|
|
|
* @Column(type="integer") |
131
|
|
|
* @GeneratedValue(strategy="AUTO") |
132
|
|
|
*/ |
133
|
|
|
private $id; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @ManyToMany(targetEntity="Doctrine\Tests\ORM\Tools\TargetInterface") |
137
|
|
|
*/ |
138
|
|
|
private $manyToMany; |
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @ManyToOne(targetEntity="Doctrine\Tests\ORM\Tools\ResolveTargetInterface", inversedBy="oneToMany") |
142
|
|
|
*/ |
143
|
|
|
private $manyToOne; |
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @OneToMany(targetEntity="Doctrine\Tests\ORM\Tools\ResolveTargetInterface", mappedBy="manyToOne") |
147
|
|
|
*/ |
148
|
|
|
private $oneToMany; |
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @OneToOne(targetEntity="Doctrine\Tests\ORM\Tools\TargetInterface") |
152
|
|
|
* @JoinColumn(name="target_entity_id", referencedColumnName="id") |
153
|
|
|
*/ |
154
|
|
|
private $oneToOne; |
|
|
|
|
155
|
|
|
|
156
|
|
|
public function getId() |
157
|
|
|
{ |
158
|
|
|
return $this->id; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @Entity |
164
|
|
|
*/ |
165
|
|
|
class TargetEntity implements TargetInterface |
166
|
|
|
{ |
167
|
|
|
/** |
168
|
|
|
* @Id |
169
|
|
|
* @Column(type="integer") |
170
|
|
|
* @GeneratedValue(strategy="AUTO") |
171
|
|
|
*/ |
172
|
|
|
private $id; |
173
|
|
|
|
174
|
|
|
public function getId() |
175
|
|
|
{ |
176
|
|
|
return $this->id; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|