File: GH11112Test.php

package info (click to toggle)
doctrine 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,556 kB
  • sloc: php: 108,620; xml: 1,340; makefile: 35; sh: 14
file content (79 lines) | stat: -rw-r--r-- 3,010 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

class GH11112Test extends OrmFunctionalTestCase
{
    protected function setUp(): void
    {
        $this->useModelSet('cms');
        self::$queryCache = new ArrayAdapter();

        parent::setUp();
    }

    public function testSimpleQueryHasLimitAndOffsetApplied(): void
    {
        $platform    = $this->_em->getConnection()->getDatabasePlatform();
        $query       = $this->_em->createQuery('SELECT u FROM ' . CmsUser::class . ' u');
        $originalSql = $query->getSQL();

        $query->setMaxResults(10);
        $query->setFirstResult(20);
        $sqlMax10First20 = $query->getSQL();

        $query->setMaxResults(30);
        $query->setFirstResult(40);
        $sqlMax30First40 = $query->getSQL();

        // The SQL is platform specific and may even be something with outer SELECTS being added. So,
        // derive the expected value at runtime through the platform.
        self::assertSame($platform->modifyLimitQuery($originalSql, 10, 20), $sqlMax10First20);
        self::assertSame($platform->modifyLimitQuery($originalSql, 30, 40), $sqlMax30First40);

        $cacheEntries = self::$queryCache->getValues();
        self::assertCount(1, $cacheEntries);
    }

    public function testSubqueryLimitAndOffsetAreIgnored(): void
    {
        // Not sure what to do about this test. Basically, I want to make sure that
        // firstResult/maxResult for subqueries are not relevant, they do not make it
        // into the final query at all. That would give us the guarantee that the
        // "sql finalizer" step is sufficient for the final, "outer" query and we
        // do not need to run finalizers for the subqueries.

        // This DQL/query makes no sense, it's just about creating a subquery in the first place
        $queryBuilder = $this->_em->createQueryBuilder();
        $queryBuilder
            ->select('o')
            ->from(CmsUser::class, 'o')
            ->where($queryBuilder->expr()->exists(
                $this->_em->createQueryBuilder()
                            ->select('u')
                            ->from(CmsUser::class, 'u')
                            ->setFirstResult(10)
                            ->setMaxResults(20),
            ));

        $query       = $queryBuilder->getQuery();
        $originalSql = $query->getSQL();

        $clone = clone $query;
        $clone->setFirstResult(24);
        $clone->setMaxResults(42);
        $limitedSql = $clone->getSQL();

        $platform = $this->_em->getConnection()->getDatabasePlatform();

        // The SQL is platform specific and may even be something with outer SELECTS being added. So,
        // derive the expected value at runtime through the platform.
        self::assertSame($platform->modifyLimitQuery($originalSql, 42, 24), $limitedSql);
    }
}