Completed
Pull Request — master (#503)
by Mihai
03:06
created

ProxyQueryTest::testGetFixedQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 56
rs 9.7252
cc 1
eloc 45
nc 1
nop 5

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 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\ORM\Query\Expr\From;
15
16
class ProxyQueryTest extends \PHPUnit_Framework_TestCase
17
{
18
    public function dataGetFixedQueryBuilder()
19
    {
20
        return array(
21
            array('aaa', 'bbb', 'id', 'id_idx', 33),
22
            array('aaa', 'bbb', 'id.value', 'id_value_idx', 33),
23
        );
24
    }
25
26
    /**
27
     * @dataProvider dataGetFixedQueryBuilder
28
     *
29
     * @param $class
30
     * @param $alias
31
     * @param $id
32
     */
33
    public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value)
34
    {
35
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')
36
            ->disableOriginalConstructor()
37
            ->getMock();
38
        $meta->expects($this->any())
39
            ->method('getIdentifierFieldNames')
40
            ->willReturn(array($id));
41
42
        $mf = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataFactory')
43
            ->disableOriginalConstructor()
44
            ->getMock();
45
        $mf->expects($this->any())
46
            ->method('getMetadataFor')
47
            ->with($this->equalTo($class))
48
            ->willReturn($meta);
49
50
        $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
51
            ->disableOriginalConstructor()
52
            ->getMock();
53
        $em->expects($this->any())
54
            ->method('getMetadataFactory')
55
            ->willReturn($mf);
56
57
        $q = $this->getMock('PDOStatement');
58
        $q->expects($this->any())
59
            ->method('execute')
60
            ->willReturn(array(array($id => $value)));
61
62
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
63
            ->setConstructorArgs(array($em))
64
            ->getMock();
65
        $qb->expects($this->any())
66
            ->method('getEntityManager')
67
            ->willReturn($em);
68
        $qb->expects($this->any())
69
            ->method('getQuery')
70
            ->willReturn($q);
71
        $qb->expects($this->once())
72
            ->method('setParameter')
73
            ->with($this->equalTo($expectedId), $this->equalTo(array($value)));
74
        $qb->expects($this->once())
75
            ->method('getDQLPart')
76
            ->with($this->equalTo('from'))
77
            ->willReturn(array(new From($class, $alias)));
78
79
        $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery')
80
            ->setConstructorArgs(array($qb))
81
            ->setMethods(array('a'))
82
            ->getMock();
83
84
85
        /* Work */
86
87
        $pq->execute();
88
    }
89
}
90