Completed
Pull Request — 3.x (#547)
by Jeroen
04:58 queued 02:31
created

ProxyQueryTest::testGetFixedQueryBuilder()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 78
rs 8.9019
cc 1
eloc 66
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\Query\Expr\From;
16
use Rhumsaa\Uuid\Doctrine\UuidType;
17
18
class ProxyQueryTest extends \PHPUnit_Framework_TestCase
19
{
20
    public static function setUpBeforeClass()
21
    {
22
        if (!Type::hasType('uuid')) {
23
            Type::addType('uuid', 'Rhumsaa\Uuid\Doctrine\UuidType');
24
        }
25
    }
26
27
    public function dataGetFixedQueryBuilder()
28
    {
29
        return array(
30
            array('aaa', 'bbb', 'id', 'id_idx', 33, Type::INTEGER),
31
            array('aaa', 'bbb', 'id.value', 'id_value_idx', 33, Type::INTEGER),
32
            array('aaa', 'bbb', 'id.uuid', 'id_uuid_idx', '80fb6f91-bba1-4d35-b3d4-e06b24494e85', UuidType::NAME),
33
        );
34
    }
35
36
    /**
37
     * @dataProvider dataGetFixedQueryBuilder
38
     *
39
     * @param $class
40
     * @param $alias
41
     * @param $id
42
     */
43
    public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType)
44
    {
45
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')
46
            ->disableOriginalConstructor()
47
            ->getMock();
48
        $meta->expects($this->any())
49
            ->method('getIdentifierFieldNames')
50
            ->willReturn(array($id));
51
        $meta->expects($this->any())
52
            ->method('getTypeOfField')
53
            ->willReturn($identifierType);
54
55
        $mf = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataFactory')
56
            ->disableOriginalConstructor()
57
            ->getMock();
58
        $mf->expects($this->any())
59
            ->method('getMetadataFor')
60
            ->with($this->equalTo($class))
61
            ->willReturn($meta);
62
63
        $platform = $this->getMockBuilder('Doctrine\DBAL\Platforms\PostgreSqlPlatform')
64
            ->disableOriginalConstructor()
65
            ->getMock();
66
67
        $conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
68
            ->disableOriginalConstructor()
69
            ->getMock();
70
        $conn->expects($this->any())
71
            ->method('getDatabasePlatform')
72
            ->willReturn($platform);
73
74
        $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
75
            ->disableOriginalConstructor()
76
            ->getMock();
77
        $em->expects($this->any())
78
            ->method('getMetadataFactory')
79
            ->willReturn($mf);
80
        $em->expects($this->any())
81
            ->method('getConnection')
82
            ->willReturn($conn);
83
84
        $q = $this->getMock('PDOStatement');
85
        $q->expects($this->any())
86
            ->method('execute')
87
            ->willReturn(array(array($id => $value)));
88
89
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
90
            ->setConstructorArgs(array($em))
91
            ->getMock();
92
        $qb->expects($this->any())
93
            ->method('getEntityManager')
94
            ->willReturn($em);
95
        $qb->expects($this->any())
96
            ->method('getQuery')
97
            ->willReturn($q);
98
        $qb->expects($this->once())
99
            ->method('setParameter')
100
            ->with($this->equalTo($expectedId), $this->equalTo(array($value)));
101
        $qb->expects($this->once())
102
            ->method('getDQLPart')
103
            ->with($this->equalTo('from'))
104
            ->willReturn(array(new From($class, $alias)));
105
        $qb->expects($this->once())
106
            ->method('getRootEntities')
107
            ->willReturn(array($class));
108
        $qb->expects($this->exactly(2))
109
            ->method('getRootAliases')
110
            ->willReturn(array($alias));
111
112
        $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery')
113
            ->setConstructorArgs(array($qb))
114
            ->setMethods(array('a'))
115
            ->getMock();
116
117
        /* Work */
118
119
        $pq->execute();
120
    }
121
}
122