Completed
Push — 3.x ( fd994c...df5a7a )
by Jordi Sala
01:59
created

ProxyQueryTest::testGetFixedQueryBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 84
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 84
rs 8.7169
cc 1
eloc 67
nc 1
nop 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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