@@ -129,4 +129,88 @@ public function testApplyCollection()
129
129
130
130
$ this ->assertEquals ('SELECT o FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o LEFT JOIN o.colors colors WHERE o IN(SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2 LEFT JOIN o_2.colors colors_2 WHERE o_2.colors = :foo) ' , $ qb ->getDQL ());
131
131
}
132
+
133
+ /**
134
+ * https://github.com/api-platform/core/issues/1021.
135
+ */
136
+ public function testHiddenOrderBy ()
137
+ {
138
+ $ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
139
+ $ resourceMetadataFactoryProphecy ->create (DummyCar::class)->willReturn (new ResourceMetadata (DummyCar::class));
140
+
141
+ $ em = $ this ->prophesize (EntityManager::class);
142
+ $ em ->getExpressionBuilder ()->shouldBeCalled ()->willReturn (new Expr ());
143
+
144
+ $ qb = new QueryBuilder ($ em ->reveal ());
145
+
146
+ $ qb ->select ('o ' , 'CASE WHEN o.dateCreated IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dateCreated_null_rank ' )
147
+ ->from (DummyCar::class, 'o ' )
148
+ ->leftJoin ('o.colors ' , 'colors ' )
149
+ ->where ('o.colors = :foo ' )
150
+ ->orderBy ('_o_dateCreated_null_rank DESC ' )
151
+ ->setParameter ('foo ' , 1 );
152
+
153
+ $ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
154
+ $ queryNameGenerator ->generateJoinAlias ('colors ' )->shouldBeCalled ()->willReturn ('colors_2 ' );
155
+ $ filterEagerLoadingExtension = new FilterEagerLoadingExtension ($ resourceMetadataFactoryProphecy ->reveal (), true );
156
+ $ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), DummyCar::class, 'get ' );
157
+
158
+ $ expected = <<<SQL
159
+ SELECT o, CASE WHEN o.dateCreated IS NULL THEN 0 ELSE 1 END AS HIDDEN _o_dateCreated_null_rank
160
+ FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
161
+ LEFT JOIN o.colors colors
162
+ WHERE o IN(
163
+ SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2
164
+ LEFT JOIN o_2.colors colors_2
165
+ WHERE o_2.colors = :foo
166
+ ) ORDER BY _o_dateCreated_null_rank DESC ASC
167
+ SQL ;
168
+
169
+ $ this ->assertEquals ($ this ->toDQLString ($ expected ), $ qb ->getDQL ());
170
+ }
171
+
172
+ public function testGroupBy ()
173
+ {
174
+ $ resourceMetadataFactoryProphecy = $ this ->prophesize (ResourceMetadataFactoryInterface::class);
175
+ $ resourceMetadataFactoryProphecy ->create (DummyCar::class)->willReturn (new ResourceMetadata (DummyCar::class));
176
+
177
+ $ em = $ this ->prophesize (EntityManager::class);
178
+ $ em ->getExpressionBuilder ()->shouldBeCalled ()->willReturn (new Expr ());
179
+
180
+ $ qb = new QueryBuilder ($ em ->reveal ());
181
+
182
+ $ qb ->select ('o ' , 'count(o.id) as counter ' )
183
+ ->from (DummyCar::class, 'o ' )
184
+ ->leftJoin ('o.colors ' , 'colors ' )
185
+ ->where ('o.colors = :foo ' )
186
+ ->orderBy ('o.colors ' )
187
+ ->groupBy ('o.colors ' )
188
+ ->having ('counter > 3 ' )
189
+ ->setParameter ('foo ' , 1 );
190
+
191
+ $ queryNameGenerator = $ this ->prophesize (QueryNameGeneratorInterface::class);
192
+ $ queryNameGenerator ->generateJoinAlias ('colors ' )->shouldBeCalled ()->willReturn ('colors_2 ' );
193
+ $ filterEagerLoadingExtension = new FilterEagerLoadingExtension ($ resourceMetadataFactoryProphecy ->reveal (), true );
194
+ $ filterEagerLoadingExtension ->applyToCollection ($ qb , $ queryNameGenerator ->reveal (), DummyCar::class, 'get ' );
195
+
196
+ $ expected = <<<SQL
197
+ SELECT o, count(o.id) as counter
198
+ FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o
199
+ LEFT JOIN o.colors colors WHERE o
200
+ IN(
201
+ SELECT o_2 FROM ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar o_2
202
+ LEFT JOIN o_2.colors colors_2
203
+ WHERE o_2.colors = :foo
204
+ )
205
+ GROUP BY o.colors HAVING counter > 3
206
+ ORDER BY o.colors ASC
207
+ SQL ;
208
+
209
+ $ this ->assertEquals ($ this ->toDQLString ($ expected ), $ qb ->getDQL ());
210
+ }
211
+
212
+ private function toDQLString (string $ dql ): string
213
+ {
214
+ return str_replace (PHP_EOL , '' , str_replace (' ' , '' , $ dql ));
215
+ }
132
216
}
0 commit comments