Failed Conditions
Push — master ( 9452f0...8be1e3 )
by Marco
10:03
created

Tools/Pagination/LimitSubqueryOutputWalkerTest.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\ORM\Tools\Pagination;
6
7
use Doctrine\DBAL\Platforms\MySqlPlatform;
8
use Doctrine\DBAL\Platforms\OraclePlatform;
9
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
10
use Doctrine\ORM\Query;
11
use Doctrine\ORM\Tools\Pagination\LimitSubqueryOutputWalker;
12
13
final class LimitSubqueryOutputWalkerTest extends PaginationTestCase
14
{
15
    public function testLimitSubquery() : void
16
    {
17
        $query = $this->entityManager->createQuery(
18
            'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a');
19
        $query->expireQueryCache(true);
20
        $limitQuery = clone $query;
21
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
22
23
        self::assertSame(
24
            'SELECT DISTINCT c0 FROM (SELECT t0."id" AS c0, t0."title" AS c1, t1."id" AS c2, t2."id" AS c3, t2."name" AS c4, t0."author_id" AS c5, t0."category_id" AS c6 FROM "MyBlogPost" t0 INNER JOIN "Category" t1 ON t0."category_id" = t1."id" INNER JOIN "Author" t2 ON t0."author_id" = t2."id") dctrn_result',
25
            $limitQuery->getSQL()
26
        );
27
    }
28
29
    public function testLimitSubqueryWithSortPg() : void
30
    {
31
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
32
        $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);
0 ignored issues
show
The method setDatabasePlatform() does not exist on Doctrine\DBAL\Connection. It seems like you code against a sub-type of Doctrine\DBAL\Connection such as Doctrine\Tests\Mocks\ConnectionMock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $this->entityManager->getConnection()->/** @scrutinizer ignore-call */ setDatabasePlatform(new PostgreSqlPlatform);
Loading history...
33
34
        $query = $this->entityManager->createQuery(
35
            'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title');
36
        $limitQuery = clone $query;
37
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
38
39
        self::assertSame(
40
            'SELECT DISTINCT c0, MIN(c5) AS dctrn_minrownum FROM (SELECT t0."id" AS c0, t0."title" AS c1, t1."id" AS c2, t2."id" AS c3, t2."name" AS c4, ROW_NUMBER() OVER(ORDER BY t0."title" ASC) AS c5, t0."author_id" AS c6, t0."category_id" AS c7 FROM "MyBlogPost" t0 INNER JOIN "Category" t1 ON t0."category_id" = t1."id" INNER JOIN "Author" t2 ON t0."author_id" = t2."id") dctrn_result GROUP BY c0 ORDER BY dctrn_minrownum ASC',
41
            $limitQuery->getSQL()
42
        );
43
44
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
45
    }
46
47
    public function testLimitSubqueryWithScalarSortPg() : void
48
    {
49
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
50
        $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);
51
52
        $query = $this->entityManager->createQuery(
53
            'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity'
54
        );
55
        $limitQuery = clone $query;
56
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
57
58
        self::assertSame(
59
            'SELECT DISTINCT c1, MIN(c3) AS dctrn_minrownum FROM (SELECT COUNT(t0."id") AS c0, t1."id" AS c1, t0."id" AS c2, ROW_NUMBER() OVER(ORDER BY COUNT(t0."id") ASC) AS c3 FROM "User" t1 INNER JOIN "user_group" t2 ON t1."id" = t2."user_id" INNER JOIN "groups" t0 ON t0."id" = t2."group_id") dctrn_result GROUP BY c1 ORDER BY dctrn_minrownum ASC',
60
            $limitQuery->getSQL()
61
        );
62
63
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
64
    }
65
66
    public function testLimitSubqueryWithMixedSortPg() : void
67
    {
68
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
69
        $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);
70
71
        $query = $this->entityManager->createQuery(
72
            'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
73
        );
74
        $limitQuery = clone $query;
75
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
76
77
        self::assertSame(
78
            'SELECT DISTINCT c1, MIN(c3) AS dctrn_minrownum FROM (SELECT COUNT(t0."id") AS c0, t1."id" AS c1, t0."id" AS c2, ROW_NUMBER() OVER(ORDER BY COUNT(t0."id") ASC, t1."id" DESC) AS c3 FROM "User" t1 INNER JOIN "user_group" t2 ON t1."id" = t2."user_id" INNER JOIN "groups" t0 ON t0."id" = t2."group_id") dctrn_result GROUP BY c1 ORDER BY dctrn_minrownum ASC',
79
            $limitQuery->getSQL()
80
        );
81
82
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
83
    }
84
85
    public function testLimitSubqueryWithHiddenScalarSortPg() : void
86
    {
87
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
88
        $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);
89
90
        $query = $this->entityManager->createQuery(
91
           'SELECT u, g, COUNT(g.id) AS hidden g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
92
        );
93
        $limitQuery = clone $query;
94
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
95
96
        self::assertSame(
97
            'SELECT DISTINCT c1, MIN(c3) AS dctrn_minrownum FROM (SELECT COUNT(t0."id") AS c0, t1."id" AS c1, t0."id" AS c2, ROW_NUMBER() OVER(ORDER BY COUNT(t0."id") ASC, t1."id" DESC) AS c3 FROM "User" t1 INNER JOIN "user_group" t2 ON t1."id" = t2."user_id" INNER JOIN "groups" t0 ON t0."id" = t2."group_id") dctrn_result GROUP BY c1 ORDER BY dctrn_minrownum ASC',
98
            $limitQuery->getSQL()
99
        );
100
101
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
102
    }
103
104
    public function testLimitSubqueryPg() : void
105
    {
106
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
107
        $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform);
108
109
        $this->testLimitSubquery();
110
111
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
112
    }
113
114
    public function testLimitSubqueryWithSortOracle() : void
115
    {
116
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
117
        $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);
118
119
        $query = $this->entityManager->createQuery(
120
            'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a ORDER BY p.title');
121
        $query->expireQueryCache(true);
122
        $limitQuery = clone $query;
123
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
124
125
        self::assertSame(
126
            'SELECT DISTINCT C0, MIN(C5) AS dctrn_minrownum FROM (SELECT t0."id" AS C0, t0."title" AS C1, t1."id" AS C2, t2."id" AS C3, t2."name" AS C4, ROW_NUMBER() OVER(ORDER BY t0."title" ASC) AS C5, t0."author_id" AS C6, t0."category_id" AS C7 FROM "MyBlogPost" t0 INNER JOIN "Category" t1 ON t0."category_id" = t1."id" INNER JOIN "Author" t2 ON t0."author_id" = t2."id") dctrn_result GROUP BY C0 ORDER BY dctrn_minrownum ASC',
127
            $limitQuery->getSQL()
128
        );
129
130
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
131
    }
132
133
    public function testLimitSubqueryWithScalarSortOracle() : void
134
    {
135
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
136
        $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);
137
138
        $query = $this->entityManager->createQuery(
139
            'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity'
140
        );
141
        $query->expireQueryCache(true);
142
        $limitQuery = clone $query;
143
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
144
145
        self::assertSame(
146
            'SELECT DISTINCT C1, MIN(C3) AS dctrn_minrownum FROM (SELECT COUNT(t0."id") AS C0, t1."id" AS C1, t0."id" AS C2, ROW_NUMBER() OVER(ORDER BY COUNT(t0."id") ASC) AS C3 FROM "User" t1 INNER JOIN "user_group" t2 ON t1."id" = t2."user_id" INNER JOIN "groups" t0 ON t0."id" = t2."group_id") dctrn_result GROUP BY C1 ORDER BY dctrn_minrownum ASC',
147
            $limitQuery->getSQL()
148
        );
149
150
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
151
    }
152
153
    public function testLimitSubqueryWithMixedSortOracle() : void
154
    {
155
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
156
        $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);
157
158
        $query = $this->entityManager->createQuery(
159
            'SELECT u, g, COUNT(g.id) AS g_quantity FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.groups g ORDER BY g_quantity, u.id DESC'
160
        );
161
        $query->expireQueryCache(true);
162
        $limitQuery = clone $query;
163
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
164
165
        self::assertSame(
166
            'SELECT DISTINCT C1, MIN(C3) AS dctrn_minrownum FROM (SELECT COUNT(t0."id") AS C0, t1."id" AS C1, t0."id" AS C2, ROW_NUMBER() OVER(ORDER BY COUNT(t0."id") ASC, t1."id" DESC) AS C3 FROM "User" t1 INNER JOIN "user_group" t2 ON t1."id" = t2."user_id" INNER JOIN "groups" t0 ON t0."id" = t2."group_id") dctrn_result GROUP BY C1 ORDER BY dctrn_minrownum ASC',
167
            $limitQuery->getSQL()
168
        );
169
170
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
171
    }
172
173
    public function testLimitSubqueryOracle() : void
174
    {
175
        $odp = $this->entityManager->getConnection()->getDatabasePlatform();
176
        $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform);
177
178
        $query = $this->entityManager->createQuery(
179
            'SELECT p, c, a FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost p JOIN p.category c JOIN p.author a');
180
        $query->expireQueryCache(true);
181
        $limitQuery = clone $query;
182
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
183
184
        self::assertSame(
185
            'SELECT DISTINCT C0 FROM (SELECT t0."id" AS C0, t0."title" AS C1, t1."id" AS C2, t2."id" AS C3, t2."name" AS C4, t0."author_id" AS C5, t0."category_id" AS C6 FROM "MyBlogPost" t0 INNER JOIN "Category" t1 ON t0."category_id" = t1."id" INNER JOIN "Author" t2 ON t0."author_id" = t2."id") dctrn_result',
186
            $limitQuery->getSQL()
187
        );
188
189
        $this->entityManager->getConnection()->setDatabasePlatform($odp);
190
    }
191
192
    public function testCountQueryMixedResultsWithName() : void
193
    {
194
        $query = $this->entityManager->createQuery(
195
            'SELECT a, sum(a.name) as foo FROM Doctrine\Tests\ORM\Tools\Pagination\Author a');
196
        $limitQuery = clone $query;
197
        $limitQuery->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
198
199
        self::assertSame(
200
            'SELECT DISTINCT c0 FROM (SELECT t0."id" AS c0, t0."name" AS c1, sum(t0."name") AS c2 FROM "Author" t0) dctrn_result',
201
            $limitQuery->getSQL()
202
        );
203
    }
204
205
    /**
206
     * @group DDC-3336
207
     */
208
    public function testCountQueryWithArithmeticOrderByCondition() : void
209
    {
210
        $query = $this->entityManager->createQuery(
211
            'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY (1 - 1000) * 1 DESC'
212
        );
213
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
214
215
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
216
217
        self::assertSame(
218
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, (1 - 1000) * 1 FROM (SELECT t0.`id` AS c0, t0.`name` AS c1 FROM `Author` t0) dctrn_result_inner ORDER BY (1 - 1000) * 1 DESC) dctrn_result',
219
            $query->getSQL()
220
        );
221
    }
222
223
    public function testCountQueryWithComplexScalarOrderByItem() : void
224
    {
225
        $query = $this->entityManager->createQuery(
226
            'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Avatar a ORDER BY a.image_height * a.image_width DESC'
227
        );
228
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
229
230
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
231
232
        self::assertSame(
233
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, c2 * c3 FROM (SELECT t0.`id` AS c0, t0.`image` AS c1, t0.`image_height` AS c2, t0.`image_width` AS c3, t0.`image_alt_desc` AS c4, t0.`user_id` AS c5 FROM `Avatar` t0) dctrn_result_inner ORDER BY c2 * c3 DESC) dctrn_result',
234
            $query->getSQL()
235
        );
236
    }
237
238
    public function testCountQueryWithComplexScalarOrderByItemJoined() : void
239
    {
240
        $query = $this->entityManager->createQuery(
241
            'SELECT u FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.avatar a ORDER BY a.image_height * a.image_width DESC'
242
        );
243
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
244
245
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
246
247
        self::assertSame(
248
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, c1 * c2 FROM (SELECT t0.`id` AS c0, t1.`image_height` AS c1, t1.`image_width` AS c2, t1.`user_id` AS c3 FROM `User` t0 INNER JOIN `Avatar` t1 ON t0.`id` = t1.`user_id`) dctrn_result_inner ORDER BY c1 * c2 DESC) dctrn_result',
249
            $query->getSQL()
250
        );
251
    }
252
253
    public function testCountQueryWithComplexScalarOrderByItemJoinedWithPartial() : void
254
    {
255
        $query = $this->entityManager->createQuery(
256
            'SELECT u, partial a.{id, image_alt_desc} FROM Doctrine\Tests\ORM\Tools\Pagination\User u JOIN u.avatar a ORDER BY a.image_height * a.image_width DESC'
257
        );
258
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
259
260
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
261
262
        self::assertSame(
263
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, c3 * c4 FROM (SELECT t0.`id` AS c0, t1.`id` AS c1, t1.`image_alt_desc` AS c2, t1.`image_height` AS c3, t1.`image_width` AS c4, t1.`user_id` AS c5 FROM `User` t0 INNER JOIN `Avatar` t1 ON t0.`id` = t1.`user_id`) dctrn_result_inner ORDER BY c3 * c4 DESC) dctrn_result',
264
            $query->getSQL()
265
        );
266
    }
267
268
    public function testCountQueryWithComplexScalarOrderByItemOracle() : void
269
    {
270
        $query = $this->entityManager->createQuery(
271
            'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Avatar a ORDER BY a.image_height * a.image_width DESC'
272
        );
273
        $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform());
274
275
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
276
277
        self::assertSame(
278
            'SELECT DISTINCT C0, MIN(C5) AS dctrn_minrownum FROM (SELECT t0."id" AS C0, t0."image" AS C1, t0."image_height" AS C2, t0."image_width" AS C3, t0."image_alt_desc" AS C4, ROW_NUMBER() OVER(ORDER BY t0."image_height" * t0."image_width" DESC) AS C5, t0."user_id" AS C6 FROM "Avatar" t0) dctrn_result GROUP BY C0 ORDER BY dctrn_minrownum ASC',
279
            $query->getSQL()
280
        );
281
    }
282
283
    /**
284
     * @group DDC-3434
285
     */
286
    public function testLimitSubqueryWithHiddenSelectionInOrderBy() : void
287
    {
288
        $query = $this->entityManager->createQuery(
289
            'SELECT a, a.name AS HIDDEN ord FROM Doctrine\Tests\ORM\Tools\Pagination\Author a ORDER BY ord DESC'
290
        );
291
292
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
293
294
        self::assertSame(
295
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, c2 FROM (SELECT t0."id" AS c0, t0."name" AS c1, t0."name" AS c2 FROM "Author" t0) dctrn_result_inner ORDER BY c2 DESC) dctrn_result',
296
            $query->getSQL()
297
        );
298
    }
299
300
    public function testLimitSubqueryWithColumnWithSortDirectionInNameMySql() : void
301
    {
302
        $query = $this->entityManager->createQuery(
303
            'SELECT a FROM Doctrine\Tests\ORM\Tools\Pagination\Avatar a ORDER BY a.image_alt_desc DESC'
304
        );
305
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
306
307
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
308
309
        self::assertSame(
310
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, c4 FROM (SELECT t0.`id` AS c0, t0.`image` AS c1, t0.`image_height` AS c2, t0.`image_width` AS c3, t0.`image_alt_desc` AS c4, t0.`user_id` AS c5 FROM `Avatar` t0) dctrn_result_inner ORDER BY c4 DESC) dctrn_result',
311
            $query->getSQL()
312
        );
313
    }
314
315
    public function testLimitSubqueryWithOrderByInnerJoined() : void
316
    {
317
        $query = $this->entityManager->createQuery(
318
            'SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost b JOIN b.author a ORDER BY a.name ASC'
319
        );
320
321
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
322
323
        self::assertSame(
324
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, c1 FROM (SELECT t0."id" AS c0, t1."name" AS c1, t0."author_id" AS c2, t0."category_id" AS c3 FROM "BlogPost" t0 INNER JOIN "Author" t1 ON t0."author_id" = t1."id") dctrn_result_inner ORDER BY c1 ASC) dctrn_result',
325
            $query->getSQL()
326
        );
327
    }
328
329
    public function testLimitSubqueryWithOrderByAndSubSelectInWhereClauseMySql() : void
330
    {
331
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
332
        $query = $this->entityManager->createQuery(
333
            'SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost b
334
WHERE  ((SELECT COUNT(simple.id) FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost simple) = 1)
335
ORDER BY b.id DESC'
336
        );
337
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
338
339
        self::assertSame(
340
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0 FROM (SELECT t0.`id` AS c0, t0.`author_id` AS c1, t0.`category_id` AS c2 FROM `BlogPost` t0 WHERE ((SELECT COUNT(t1.`id`) AS c3 FROM `BlogPost` t1) = 1)) dctrn_result_inner ORDER BY c0 DESC) dctrn_result',
341
            $query->getSQL()
342
        );
343
    }
344
345
    public function testLimitSubqueryWithOrderByAndSubSelectInWhereClausePgSql() : void
346
    {
347
        $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform());
348
        $query = $this->entityManager->createQuery(
349
            'SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost b
350
WHERE  ((SELECT COUNT(simple.id) FROM Doctrine\Tests\ORM\Tools\Pagination\BlogPost simple) = 1)
351
ORDER BY b.id DESC'
352
        );
353
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
354
355
        self::assertSame(
356
            'SELECT DISTINCT c0, MIN(c1) AS dctrn_minrownum FROM (SELECT t0."id" AS c0, ROW_NUMBER() OVER(ORDER BY t0."id" DESC) AS c1, t0."author_id" AS c2, t0."category_id" AS c3 FROM "BlogPost" t0 WHERE ((SELECT COUNT(t1."id") AS c4 FROM "BlogPost" t1) = 1)) dctrn_result GROUP BY c0 ORDER BY dctrn_minrownum ASC',
357
            $query->getSQL()
358
        );
359
    }
360
361
    /**
362
     * This tests ordering by property that has the 'declared' field.
363
     */
364
    public function testLimitSubqueryOrderByFieldFromMappedSuperclassMySql() : void
365
    {
366
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
367
368
        // now use the third one in query
369
        $query = $this->entityManager->createQuery(
370
            'SELECT b FROM Doctrine\Tests\ORM\Tools\Pagination\Banner b ORDER BY b.id DESC'
371
        );
372
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
373
374
        self::assertSame(
375
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0 FROM (SELECT t0.`id` AS c0, t0.`name` AS c1 FROM `Banner` t0) dctrn_result_inner ORDER BY c0 DESC) dctrn_result',
376
            $query->getSQL()
377
        );
378
    }
379
380
    /**
381
     * Tests order by on a subselect expression (mysql).
382
     */
383
    public function testLimitSubqueryOrderBySubSelectOrderByExpressionMySql() : void
384
    {
385
        $this->entityManager->getConnection()->setDatabasePlatform(new MySqlPlatform());
386
387
        $query = $this->entityManager->createQuery(
388
            'SELECT a,
389
                (
390
                    SELECT MIN(bp.title)
391
                    FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost bp
392
                    WHERE bp.author = a
393
                ) AS HIDDEN first_blog_post
394
            FROM Doctrine\Tests\ORM\Tools\Pagination\Author a
395
            ORDER BY first_blog_post DESC'
396
        );
397
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
398
399
        self::assertSame(
400
            'SELECT DISTINCT c0 FROM (SELECT DISTINCT c0, c2 FROM (SELECT t0.`id` AS c0, t0.`name` AS c1, (SELECT MIN(t1.`title`) AS c3 FROM `MyBlogPost` t1 WHERE t1.`author_id` = t0.`id`) AS c2 FROM `Author` t0) dctrn_result_inner ORDER BY c2 DESC) dctrn_result',
401
            $query->getSQL()
402
        );
403
    }
404
405
    /**
406
     * Tests order by on a subselect expression invoking RowNumberOverFunction (postgres).
407
     */
408
    public function testLimitSubqueryOrderBySubSelectOrderByExpressionPgSql() : void
409
    {
410
        $this->entityManager->getConnection()->setDatabasePlatform(new PostgreSqlPlatform());
411
412
        $query = $this->entityManager->createQuery(
413
            'SELECT a,
414
                (
415
                    SELECT MIN(bp.title)
416
                    FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost bp
417
                    WHERE bp.author = a
418
                ) AS HIDDEN first_blog_post
419
            FROM Doctrine\Tests\ORM\Tools\Pagination\Author a
420
            ORDER BY first_blog_post DESC'
421
        );
422
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
423
424
        self::assertSame(
425
            'SELECT DISTINCT c0, MIN(c4) AS dctrn_minrownum FROM (SELECT t0."id" AS c0, t0."name" AS c1, (SELECT MIN(t1."title") AS c3 FROM "MyBlogPost" t1 WHERE t1."author_id" = t0."id") AS c2, ROW_NUMBER() OVER(ORDER BY (SELECT MIN(t1."title") AS c5 FROM "MyBlogPost" t1 WHERE t1."author_id" = t0."id") DESC) AS c4 FROM "Author" t0) dctrn_result GROUP BY c0 ORDER BY dctrn_minrownum ASC',
426
            $query->getSQL()
427
        );
428
    }
429
430
    /**
431
     * Tests order by on a subselect expression invoking RowNumberOverFunction (oracle).
432
     */
433
    public function testLimitSubqueryOrderBySubSelectOrderByExpressionOracle() : void
434
    {
435
        $this->entityManager->getConnection()->setDatabasePlatform(new OraclePlatform());
436
437
        $query = $this->entityManager->createQuery(
438
            'SELECT a,
439
                (
440
                    SELECT MIN(bp.title)
441
                    FROM Doctrine\Tests\ORM\Tools\Pagination\MyBlogPost bp
442
                    WHERE bp.author = a
443
                ) AS HIDDEN first_blog_post
444
            FROM Doctrine\Tests\ORM\Tools\Pagination\Author a
445
            ORDER BY first_blog_post DESC'
446
        );
447
        $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, LimitSubqueryOutputWalker::class);
448
449
        self::assertSame(
450
            'SELECT DISTINCT C0, MIN(C4) AS dctrn_minrownum FROM (SELECT t0."id" AS C0, t0."name" AS C1, (SELECT MIN(t1."title") AS C3 FROM "MyBlogPost" t1 WHERE t1."author_id" = t0."id") AS C2, ROW_NUMBER() OVER(ORDER BY (SELECT MIN(t1."title") AS C5 FROM "MyBlogPost" t1 WHERE t1."author_id" = t0."id") DESC) AS C4 FROM "Author" t0) dctrn_result GROUP BY C0 ORDER BY dctrn_minrownum ASC',
451
            $query->getSQL()
452
        );
453
    }
454
}
455