15
15
use ApiPlatform \Core \Bridge \Doctrine \Orm \Util \QueryNameGeneratorInterface ;
16
16
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
17
17
use ApiPlatform \Core \Metadata \Resource \ResourceMetadata ;
18
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \CompositeRelation ;
18
19
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \DummyCar ;
19
20
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Foo ;
20
21
use Doctrine \ORM \EntityManager ;
22
+ use Doctrine \ORM \Mapping \ClassMetadataInfo ;
21
23
use Doctrine \ORM \Query \Expr ;
22
24
use Doctrine \ORM \QueryBuilder ;
23
25
@@ -112,6 +114,7 @@ public function testApplyCollection()
112
114
113
115
$ em = $ this ->prophesize (EntityManager::class);
114
116
$ em ->getExpressionBuilder ()->shouldBeCalled ()->willReturn (new Expr ());
117
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
115
118
116
119
$ qb = new QueryBuilder ($ em ->reveal ());
117
120
@@ -123,6 +126,7 @@ public function testApplyCollection()
123
126
124
127
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
125
128
$ queryNameGenerator ->generateJoinAlias ('colors ' )->shouldBeCalled ()->willReturn ('colors_2 ' );
129
+ $ queryNameGenerator ->generateJoinAlias ('o ' )->shouldBeCalled ()->willReturn ('o_2 ' );
126
130
127
131
$ filterEagerLoadingExtension = new FilterEagerLoadingExtension ($ resourceMetadataFactoryProphecy ->reveal (), true );
128
132
$ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), DummyCar::class, 'get ' );
@@ -140,6 +144,7 @@ public function testHiddenOrderBy()
140
144
141
145
$ em = $ this ->prophesize (EntityManager::class);
142
146
$ em ->getExpressionBuilder ()->shouldBeCalled ()->willReturn (new Expr ());
147
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
143
148
144
149
$ qb = new QueryBuilder ($ em ->reveal ());
145
150
@@ -152,6 +157,7 @@ public function testHiddenOrderBy()
152
157
153
158
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
154
159
$ queryNameGenerator ->generateJoinAlias ('colors ' )->shouldBeCalled ()->willReturn ('colors_2 ' );
160
+ $ queryNameGenerator ->generateJoinAlias ('o ' )->shouldBeCalled ()->willReturn ('o_2 ' );
155
161
$ filterEagerLoadingExtension = new FilterEagerLoadingExtension ($ resourceMetadataFactoryProphecy ->reveal (), true );
156
162
$ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), DummyCar::class, 'get ' );
157
163
@@ -176,6 +182,7 @@ public function testGroupBy()
176
182
177
183
$ em = $ this ->prophesize (EntityManager::class);
178
184
$ em ->getExpressionBuilder ()->shouldBeCalled ()->willReturn (new Expr ());
185
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
179
186
180
187
$ qb = new QueryBuilder ($ em ->reveal ());
181
188
@@ -190,6 +197,7 @@ public function testGroupBy()
190
197
191
198
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
192
199
$ queryNameGenerator ->generateJoinAlias ('colors ' )->shouldBeCalled ()->willReturn ('colors_2 ' );
200
+ $ queryNameGenerator ->generateJoinAlias ('o ' )->shouldBeCalled ()->willReturn ('o_2 ' );
193
201
$ filterEagerLoadingExtension = new FilterEagerLoadingExtension ($ resourceMetadataFactoryProphecy ->reveal (), true );
194
202
$ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), DummyCar::class, 'get ' );
195
203
@@ -209,6 +217,57 @@ public function testGroupBy()
209
217
$ this ->assertEquals ($ this ->toDQLString ($ expected ), $ qb ->getDQL ());
210
218
}
211
219
220
+ public function testCompositeIdentifiers ()
221
+ {
222
+ $ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
223
+ $ resourceMetadataFactoryProphecy ->create (CompositeRelation::class)->willReturn (new ResourceMetadata (CompositeRelation::class));
224
+
225
+ $ classMetadata = new ClassMetadataInfo (CompositeRelation::class);
226
+ $ classMetadata ->isIdentifierComposite = true ;
227
+ $ classMetadata ->identifier = ['item ' , 'label ' ];
228
+
229
+ $ em = $ this ->prophesize (EntityManager::class);
230
+ $ em ->getExpressionBuilder ()->shouldBeCalled ()->willReturn (new Expr ());
231
+ $ em ->getClassMetadata (CompositeRelation::class)->shouldBeCalled ()->willReturn ($ classMetadata );
232
+
233
+ $ qb = new QueryBuilder ($ em ->reveal ());
234
+
235
+ $ qb ->select ('o ' )
236
+ ->from (CompositeRelation::class, 'o ' )
237
+ ->innerJoin ('o.compositeItem ' , 'item ' )
238
+ ->innerJoin ('o.compositeLabel ' , 'label ' )
239
+ ->where ('item.field1 = :foo ' )
240
+ ->setParameter ('foo ' , 1 );
241
+
242
+ $ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
243
+ $ queryNameGenerator ->generateJoinAlias ('item ' )->shouldBeCalled ()->willReturn ('item_2 ' );
244
+ $ queryNameGenerator ->generateJoinAlias ('label ' )->shouldBeCalled ()->willReturn ('label_2 ' );
245
+ $ queryNameGenerator ->generateJoinAlias ('o ' )->shouldBeCalled ()->willReturn ('o_2 ' );
246
+
247
+ $ filterEagerLoadingExtension = new FilterEagerLoadingExtension ($ resourceMetadataFactoryProphecy ->reveal (), true );
248
+ $ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), CompositeRelation::class, 'get ' );
249
+
250
+ $ expected = <<<SQL
251
+ SELECT o
252
+ FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
253
+ INNER JOIN o.compositeItem item
254
+ INNER JOIN o.compositeLabel label
255
+ WHERE o.item IN(
256
+ SELECT IDENTITY(o_2.item) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
257
+ INNER JOIN o_2.compositeItem item_2
258
+ INNER JOIN o_2.compositeLabel label_2
259
+ WHERE item_2.field1 = :foo
260
+ ) AND o.label IN(
261
+ SELECT IDENTITY(o_2.label) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
262
+ INNER JOIN o_2.compositeItem item_2
263
+ INNER JOIN o_2.compositeLabel label_2
264
+ WHERE item_2.field1 = :foo
265
+ )
266
+ SQL ;
267
+
268
+ $ this ->assertEquals ($ this ->toDQLString ($ expected ), $ qb ->getDQL ());
269
+ }
270
+
212
271
private function toDQLString (string $ dql ): string
213
272
{
214
273
return preg_replace ('/ \\r \\n| \\n/ ' , '' , str_replace (' ' , '' , $ dql ));
0 commit comments