Completed
Pull Request — master (#792)
by Grégoire
01:37
created

ProxyQueryTest::testAddOrderedColumnsCompositeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineORMAdminBundle\Tests\Datagrid;
15
16
use Doctrine\DBAL\Connection;
17
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
18
use Doctrine\DBAL\Types\Type;
19
use Doctrine\ORM\EntityManager;
20
use Doctrine\ORM\Mapping\ClassMetadata;
21
use Doctrine\ORM\Mapping\ClassMetadataFactory;
22
use Doctrine\ORM\Query;
23
use Doctrine\ORM\Query\Expr\From;
24
use Doctrine\ORM\Query\Expr\OrderBy;
25
use Doctrine\ORM\QueryBuilder;
26
use Doctrine\ORM\Tools\SchemaTool;
27
use PHPUnit\Framework\TestCase;
28
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
29
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType;
30
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Query\FooWalker;
31
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Util\NonIntegerIdentifierTestClass;
32
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
33
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
34
35
class ProxyQueryTest extends TestCase
36
{
37
    /**
38
     * @var EntityManager
39
     */
40
    private $em;
41
42
    public static function setUpBeforeClass(): void
43
    {
44
        if (!Type::hasType('uuid')) {
45
            Type::addType('uuid', UuidType::class);
46
        }
47
    }
48
49
    protected function setUp(): void
50
    {
51
        $this->em = DoctrineTestHelper::createTestEntityManager();
52
53
        $schemaTool = new SchemaTool($this->em);
54
        $classes = [
55
            $this->em->getClassMetadata(DoubleNameEntity::class),
56
        ];
57
58
        try {
59
            $schemaTool->dropSchema($classes);
60
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
61
        }
62
63
        try {
64
            $schemaTool->createSchema($classes);
65
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
66
        }
67
    }
68
69
    protected function tearDown(): void
70
    {
71
        $this->em = null;
72
    }
73
74
    public function dataGetFixedQueryBuilder()
75
    {
76
        return [
77
            ['aaa', 'bbb', 'id', 'id_idx', 33, Type::INTEGER],
78
            ['aaa', 'bbb', 'associatedId', 'associatedId_idx', 33, null],
79
            ['aaa', 'bbb', 'id.value', 'id_value_idx', 33, Type::INTEGER],
80
            ['aaa', 'bbb', 'id.uuid', 'id_uuid_idx', new NonIntegerIdentifierTestClass('80fb6f91-bba1-4d35-b3d4-e06b24494e85'), UuidType::NAME],
81
        ];
82
    }
83
84
    /**
85
     * @dataProvider dataGetFixedQueryBuilder
86
     *
87
     * @param $class
88
     * @param $alias
89
     * @param $id
90
     */
91
    public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType): void
92
    {
93
        $meta = $this->createMock(ClassMetadata::class);
94
        $meta->expects($this->any())
95
            ->method('getIdentifierFieldNames')
96
            ->willReturn([$id]);
97
        $meta->expects($this->any())
98
            ->method('getTypeOfField')
99
            ->willReturn($identifierType);
100
101
        $mf = $this->createMock(ClassMetadataFactory::class);
102
        $mf->expects($this->any())
103
            ->method('getMetadataFor')
104
            ->with($this->equalTo($class))
105
            ->willReturn($meta);
106
107
        $platform = $this->createMock(PostgreSqlPlatform::class);
108
109
        $conn = $this->createMock(Connection::class);
110
        $conn->expects($this->any())
111
            ->method('getDatabasePlatform')
112
            ->willReturn($platform);
113
114
        $em = $this->createMock(EntityManager::class);
115
        $em->expects($this->any())
116
            ->method('getMetadataFactory')
117
            ->willReturn($mf);
118
        $em->expects($this->any())
119
            ->method('getConnection')
120
            ->willReturn($conn);
121
122
        // NEXT MAJOR: Replace this when dropping PHP < 5.6
123
        // $q = $this->createMock('PDOStatement');
124
        $q = $this->getMockBuilder('stdClass')
125
            ->setMethods(['execute', 'setHint'])
126
            ->getMock();
127
        $q->expects($this->once())
128
           ->method('setHint')
129
           ->willReturn($q);
130
        $q->expects($this->any())
131
            ->method('execute')
132
            ->willReturn([[$id => $value]]);
133
134
        $qb = $this->getMockBuilder(QueryBuilder::class)
135
            ->setConstructorArgs([$em])
136
            ->getMock();
137
        $qb->expects($this->any())
138
            ->method('getEntityManager')
139
            ->willReturn($em);
140
        $qb->expects($this->any())
141
            ->method('getQuery')
142
            ->willReturn($q);
143
        $qb->expects($this->once())
144
            ->method('setParameter')
145
            ->with($this->equalTo($expectedId), $this->equalTo([$value]));
146
        $qb->expects($this->any())
147
            ->method('getDQLPart')
148
            ->will($this->returnCallBack(function ($part) use ($class, $alias) {
149
                $parts = [
150
                    'from' => [new From($class, $alias)],
151
                    'orderBy' => [new OrderBy('whatever', 'DESC')],
152
                ];
153
154
                return $parts[$part];
155
            }));
156
        $qb->expects($this->once())
157
            ->method('addOrderBy')
158
            ->with("$alias.$id", null);
159
        $qb->expects($this->once())
160
            ->method('getRootEntities')
161
            ->willReturn([$class]);
162
        $qb->expects($this->exactly(2))
163
            ->method('getRootAliases')
164
            ->willReturn([$alias]);
165
166
        $pq = $this->getMockBuilder(ProxyQuery::class)
167
            ->setConstructorArgs([$qb])
168
            ->setMethods(['a'])
169
            ->getMock();
170
171
        /* Work */
172
173
        $pq->execute();
174
    }
175
}
176