17
17
use ApiPlatform \Core \Bridge \Doctrine \Orm \Util \QueryNameGeneratorInterface ;
18
18
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
19
19
use ApiPlatform \Core \Metadata \Resource \ResourceMetadata ;
20
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \CompositeItem ;
21
+ use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \CompositeLabel ;
20
22
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \CompositeRelation ;
21
23
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \DummyCar ;
22
24
use ApiPlatform \Core \Tests \Fixtures \TestBundle \Entity \Foo ;
@@ -39,8 +41,12 @@ public function testIsNoForceEagerCollectionAttributes()
39
41
],
40
42
], null ));
41
43
44
+ $ em = $ this ->prophesize (EntityManager::class);
45
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
46
+
42
47
$ qb = $ this ->prophesize (QueryBuilder::class);
43
48
$ qb ->getDQLPart ('where ' )->shouldNotBeCalled ();
49
+ $ qb ->getEntityManager ()->willReturn ($ em );
44
50
45
51
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
46
52
@@ -50,13 +56,17 @@ public function testIsNoForceEagerCollectionAttributes()
50
56
51
57
public function testIsNoForceEagerResource ()
52
58
{
59
+ $ em = $ this ->prophesize (EntityManager::class);
60
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
61
+
53
62
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
54
63
$ resourceMetadataFactoryProphecy ->create (DummyCar::class)->willReturn (new ResourceMetadata (DummyCar::class, null , null , null , [
55
64
'get ' => [],
56
65
], ['force_eager ' => false ]));
57
66
58
67
$ qb = $ this ->prophesize (QueryBuilder::class);
59
68
$ qb ->getDQLPart ('where ' )->shouldNotBeCalled ();
69
+ $ qb ->getEntityManager ()->willReturn ($ em );
60
70
61
71
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
62
72
@@ -66,13 +76,17 @@ public function testIsNoForceEagerResource()
66
76
67
77
public function testIsForceEagerConfig ()
68
78
{
79
+ $ em = $ this ->prophesize (EntityManager::class);
80
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
81
+
69
82
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
70
83
$ resourceMetadataFactoryProphecy ->create (DummyCar::class)->willReturn (new ResourceMetadata (DummyCar::class, null , null , null , [
71
84
'get ' => [],
72
85
]));
73
86
74
87
$ qb = $ this ->prophesize (QueryBuilder::class);
75
88
$ qb ->getDQLPart ('where ' )->shouldNotBeCalled ();
89
+ $ qb ->getEntityManager ()->willReturn ($ em );
76
90
77
91
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
78
92
@@ -82,11 +96,15 @@ public function testIsForceEagerConfig()
82
96
83
97
public function testHasNoWherePart ()
84
98
{
99
+ $ em = $ this ->prophesize (EntityManager::class);
100
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
101
+
85
102
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
86
103
$ resourceMetadataFactoryProphecy ->create (DummyCar::class)->willReturn (new ResourceMetadata (DummyCar::class));
87
104
88
105
$ qb = $ this ->prophesize (QueryBuilder::class);
89
106
$ qb ->getDQLPart ('where ' )->shouldBeCalled ()->willReturn (null );
107
+ $ qb ->getEntityManager ()->willReturn ($ em );
90
108
91
109
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
92
110
@@ -96,12 +114,16 @@ public function testHasNoWherePart()
96
114
97
115
public function testHasNoJoinPart ()
98
116
{
117
+ $ em = $ this ->prophesize (EntityManager::class);
118
+ $ em ->getClassMetadata (DummyCar::class)->shouldBeCalled ()->willReturn (new ClassMetadataInfo (DummyCar::class));
119
+
99
120
$ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
100
121
$ resourceMetadataFactoryProphecy ->create (DummyCar::class)->willReturn (new ResourceMetadata (DummyCar::class));
101
122
102
123
$ qb = $ this ->prophesize (QueryBuilder::class);
103
124
$ qb ->getDQLPart ('where ' )->shouldBeCalled ()->willReturn (new Expr \Andx ());
104
125
$ qb ->getDQLPart ('join ' )->shouldBeCalled ()->willReturn (null );
126
+ $ qb ->getEntityManager ()->willReturn ($ em );
105
127
106
128
$ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
107
129
@@ -164,12 +186,12 @@ public function testHiddenOrderBy()
164
186
$ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), DummyCar::class, 'get ' );
165
187
166
188
$ expected = <<<SQL
167
- SELECT o, CASE WHEN o.dateCreated IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dateCreated_null_rank
168
- FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
169
- LEFT JOIN o.colors colors
189
+ SELECT o, CASE WHEN o.dateCreated IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dateCreated_null_rank
190
+ FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
191
+ LEFT JOIN o.colors colors
170
192
WHERE o IN(
171
- SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2
172
- LEFT JOIN o_2.colors colors_2
193
+ SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2
194
+ LEFT JOIN o_2.colors colors_2
173
195
WHERE o_2.colors = :foo
174
196
) ORDER BY _o_dateCreated_null_rank DESC ASC
175
197
SQL ;
@@ -204,15 +226,15 @@ public function testGroupBy()
204
226
$ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), DummyCar::class, 'get ' );
205
227
206
228
$ expected = <<<SQL
207
- SELECT o, count(o.id) as counter
208
- FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
209
- LEFT JOIN o.colors colors WHERE o
229
+ SELECT o, count(o.id) as counter
230
+ FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
231
+ LEFT JOIN o.colors colors WHERE o
210
232
IN(
211
- SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2
212
- LEFT JOIN o_2.colors colors_2
233
+ SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2
234
+ LEFT JOIN o_2.colors colors_2
213
235
WHERE o_2.colors = :foo
214
- )
215
- GROUP BY o.colors HAVING counter > 3
236
+ )
237
+ GROUP BY o.colors HAVING counter > 3
216
238
ORDER BY o.colors ASC
217
239
SQL ;
218
240
@@ -250,19 +272,74 @@ public function testCompositeIdentifiers()
250
272
$ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), CompositeRelation::class, 'get ' );
251
273
252
274
$ expected = <<<SQL
253
- SELECT o
254
- FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
255
- INNER JOIN o.compositeItem item
256
- INNER JOIN o.compositeLabel label
275
+ SELECT o
276
+ FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
277
+ INNER JOIN o.compositeItem item
278
+ INNER JOIN o.compositeLabel label
279
+ WHERE o.item IN(
280
+ SELECT IDENTITY(o_2.item) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
281
+ INNER JOIN o_2.compositeItem item_2
282
+ INNER JOIN o_2.compositeLabel label_2
283
+ WHERE item_2.field1 = :foo
284
+ ) AND o.label IN(
285
+ SELECT IDENTITY(o_2.label) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
286
+ INNER JOIN o_2.compositeItem item_2
287
+ INNER JOIN o_2.compositeLabel label_2
288
+ WHERE item_2.field1 = :foo
289
+ )
290
+ SQL ;
291
+
292
+ $ this ->assertEquals ($ this ->toDQLString ($ expected ), $ qb ->getDQL ());
293
+ }
294
+
295
+ public function testFetchEagerWithNoForceEager ()
296
+ {
297
+ $ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
298
+ $ resourceMetadataFactoryProphecy ->create (CompositeRelation::class)->willReturn (new ResourceMetadata (CompositeRelation::class));
299
+
300
+ $ classMetadata = new ClassMetadataInfo (CompositeRelation::class);
301
+ $ classMetadata ->isIdentifierComposite = true ;
302
+ $ classMetadata ->identifier = ['item ' , 'label ' ];
303
+ $ classMetadata ->associationMappings = [
304
+ 'item ' => ['fetch ' => 3 , 'joinColumns ' => [['nullable ' => false ]], 'targetEntity ' => CompositeItem::class],
305
+ 'label ' => ['fetch ' => 3 , 'joinColumns ' => [['nullable ' => false ]], 'targetEntity ' => CompositeLabel::class],
306
+ ];
307
+
308
+ $ em = $ this ->prophesize (EntityManager::class);
309
+ $ em ->getExpressionBuilder ()->shouldBeCalled ()->willReturn (new Expr ());
310
+ $ em ->getClassMetadata (CompositeRelation::class)->shouldBeCalled ()->willReturn ($ classMetadata );
311
+
312
+ $ qb = new QueryBuilder ($ em ->reveal ());
313
+
314
+ $ qb ->select ('o ' )
315
+ ->from (CompositeRelation::class, 'o ' )
316
+ ->innerJoin ('o.compositeItem ' , 'item ' )
317
+ ->innerJoin ('o.compositeLabel ' , 'label ' )
318
+ ->where ('item.field1 = :foo ' )
319
+ ->setParameter ('foo ' , 1 );
320
+
321
+ $ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
322
+ $ queryNameGenerator ->generateJoinAlias ('item ' )->shouldBeCalled ()->willReturn ('item_2 ' );
323
+ $ queryNameGenerator ->generateJoinAlias ('label ' )->shouldBeCalled ()->willReturn ('label_2 ' );
324
+ $ queryNameGenerator ->generateJoinAlias ('o ' )->shouldBeCalled ()->willReturn ('o_2 ' );
325
+
326
+ $ filterEagerLoadingExtension = new FilterEagerLoadingExtension ($ resourceMetadataFactoryProphecy ->reveal (), false );
327
+ $ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), CompositeRelation::class, 'get ' );
328
+
329
+ $ expected = <<<SQL
330
+ SELECT o
331
+ FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o
332
+ INNER JOIN o.compositeItem item
333
+ INNER JOIN o.compositeLabel label
257
334
WHERE o.item IN(
258
- SELECT IDENTITY(o_2.item) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
259
- INNER JOIN o_2.compositeItem item_2
260
- INNER JOIN o_2.compositeLabel label_2
335
+ SELECT IDENTITY(o_2.item) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
336
+ INNER JOIN o_2.compositeItem item_2
337
+ INNER JOIN o_2.compositeLabel label_2
261
338
WHERE item_2.field1 = :foo
262
339
) AND o.label IN(
263
- SELECT IDENTITY(o_2.label) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
264
- INNER JOIN o_2.compositeItem item_2
265
- INNER JOIN o_2.compositeLabel label_2
340
+ SELECT IDENTITY(o_2.label) FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation o_2
341
+ INNER JOIN o_2.compositeItem item_2
342
+ INNER JOIN o_2.compositeLabel label_2
266
343
WHERE item_2.field1 = :foo
267
344
)
268
345
SQL ;
@@ -272,6 +349,6 @@ public function testCompositeIdentifiers()
272
349
273
350
private function toDQLString (string $ dql ): string
274
351
{
275
- return preg_replace ('/ \\ r \\ n| \\ n / ' , '' , str_replace ( ' ' , '' , $ dql) );
352
+ return preg_replace ([ '/\s+/ ' , ' /\(\s / ' , '/\s\)/ ' ], [ ' ' , ' ( ' , ') ' ] , $ dql );
276
353
}
277
354
}
0 commit comments