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