Completed
Pull Request — 3.x-dev-kit (#736)
by
unknown
12:39 queued 10:58
created

ProxyQueryTest::testGetFixedQueryBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 81
rs 8.8076
cc 1
eloc 64
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\Types\Type;
15
use Doctrine\ORM\EntityManager;
16
use Doctrine\ORM\Query\Expr\From;
17
use Doctrine\ORM\Query\Expr\OrderBy;
18
use Doctrine\ORM\Tools\SchemaTool;
19
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
20
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType;
21
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Util\NonIntegerIdentifierTestClass;
22
use Sonata\DoctrineORMAdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
23
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
24
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
25
26
class ProxyQueryTest extends PHPUnit_Framework_TestCase
27
{
28
    const DOUBLE_NAME_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity';
29
30
    /**
31
     * @var EntityManager
32
     */
33
    private $em;
34
35
    public static function setUpBeforeClass()
36
    {
37
        if (!Type::hasType('uuid')) {
38
            Type::addType('uuid', 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType');
39
        }
40
    }
41
42
    protected function setUp()
43
    {
44
        $this->em = DoctrineTestHelper::createTestEntityManager();
45
46
        $schemaTool = new SchemaTool($this->em);
47
        $classes = [
48
            $this->em->getClassMetadata(self::DOUBLE_NAME_CLASS),
49
        ];
50
51
        try {
52
            $schemaTool->dropSchema($classes);
53
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
54
        }
55
56
        try {
57
            $schemaTool->createSchema($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
62
    protected function tearDown()
63
    {
64
        $this->em = null;
65
    }
66
67
    public function dataGetFixedQueryBuilder()
68
    {
69
        return [
70
            ['aaa', 'bbb', 'id', 'id_idx', 33, Type::INTEGER],
71
            ['aaa', 'bbb', 'associatedId', 'associatedId_idx', 33, null],
72
            ['aaa', 'bbb', 'id.value', 'id_value_idx', 33, Type::INTEGER],
73
            ['aaa', 'bbb', 'id.uuid', 'id_uuid_idx', new NonIntegerIdentifierTestClass('80fb6f91-bba1-4d35-b3d4-e06b24494e85'), UuidType::NAME],
74
        ];
75
    }
76
77
    /**
78
     * @dataProvider dataGetFixedQueryBuilder
79
     *
80
     * @param $class
81
     * @param $alias
82
     * @param $id
83
     */
84
    public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType)
85
    {
86
        $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo');
87
        $meta->expects($this->any())
88
            ->method('getIdentifierFieldNames')
89
            ->willReturn([$id]);
90
        $meta->expects($this->any())
91
            ->method('getTypeOfField')
92
            ->willReturn($identifierType);
93
94
        $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory');
95
        $mf->expects($this->any())
96
            ->method('getMetadataFor')
97
            ->with($this->equalTo($class))
98
            ->willReturn($meta);
99
100
        $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform');
101
102
        $conn = $this->createMock('Doctrine\DBAL\Connection');
103
        $conn->expects($this->any())
104
            ->method('getDatabasePlatform')
105
            ->willReturn($platform);
106
107
        $em = $this->createMock('Doctrine\ORM\EntityManager');
108
        $em->expects($this->any())
109
            ->method('getMetadataFactory')
110
            ->willReturn($mf);
111
        $em->expects($this->any())
112
            ->method('getConnection')
113
            ->willReturn($conn);
114
115
        // NEXT MAJOR: Replace this when dropping PHP < 5.6
116
        // $q = $this->createMock('PDOStatement');
117
        $q = $this->getMockBuilder('stdClass')
118
            ->setMethods(['execute'])
119
            ->getMock();
120
        $q->expects($this->any())
121
            ->method('execute')
122
            ->willReturn([[$id => $value]]);
123
124
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
125
            ->setConstructorArgs([$em])
126
            ->getMock();
127
        $qb->expects($this->any())
128
            ->method('getEntityManager')
129
            ->willReturn($em);
130
        $qb->expects($this->any())
131
            ->method('getQuery')
132
            ->willReturn($q);
133
        $qb->expects($this->once())
134
            ->method('setParameter')
135
            ->with($this->equalTo($expectedId), $this->equalTo([$value]));
136
        $qb->expects($this->any())
137
            ->method('getDQLPart')
138
            ->will($this->returnCallBack(function ($part) use ($class, $alias) {
139
                $parts = [
140
                    'from' => [new From($class, $alias)],
141
                    'orderBy' => [new OrderBy('whatever', 'DESC')],
142
                ];
143
144
                return $parts[$part];
145
            }));
146
        $qb->expects($this->once())
147
            ->method('addOrderBy')
148
            ->with("$alias.$id", null);
149
        $qb->expects($this->once())
150
            ->method('getRootEntities')
151
            ->willReturn([$class]);
152
        $qb->expects($this->exactly(2))
153
            ->method('getRootAliases')
154
            ->willReturn([$alias]);
155
156
        $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery')
157
            ->setConstructorArgs([$qb])
158
            ->setMethods(['a'])
159
            ->getMock();
160
161
        /* Work */
162
163
        $pq->execute();
164
    }
165
166
    public function testAddOrderedColumns()
167
    {
168
        $qb = $this->em->createQueryBuilder()
169
                       ->select('o.id')
170
                       ->distinct()
171
                       ->from(self::DOUBLE_NAME_CLASS, 'o')
172
                       ->orderBy('o.name', 'ASC')
173
                       ->addOrderBy('o.name2', 'DESC');
174
175
        $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery')
176
                   ->disableOriginalConstructor()
177
                   ->getMock();
178
179
        $reflection = new \ReflectionClass(get_class($pq));
180
        $method = $reflection->getMethod('addOrderedColumns');
181
        $method->setAccessible(true);
182
        $method->invoke($pq, $qb);
183
184
        $dqlPart = $qb->getDqlPart('select');
185
        $this->assertCount(3, $dqlPart);
186
        $this->assertEquals('o.id', $dqlPart[0]);
187
        $this->assertEquals('o.name', $dqlPart[1]);
188
        $this->assertEquals('o.name2', $dqlPart[2]);
189
    }
190
191
    public function testSetHint()
192
    {
193
        $entity1 = new DoubleNameEntity(1, 'Foo', null);
194
        $entity2 = new DoubleNameEntity(2, 'Bar', null);
195
196
        $this->em->persist($entity1);
197
        $this->em->persist($entity2);
198
        $this->em->flush();
199
200
        $qb = $this->em->createQueryBuilder()
201
                       ->select('o.id')
202
                       ->from(self::DOUBLE_NAME_CLASS, 'o');
203
204
        $pq = new ProxyQuery($qb);
205
        $pq->setHint(
206
            \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
207
            'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Query\FooWalker'
208
        );
209
        $pq->setHint('hint', 'value');
210
211
        $result = $pq->execute();
212
213
        $this->assertEquals(2, $result[0]['id']);
214
    }
215
}
216