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

ProxyQueryTest::dataGetFixedQueryBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Sonata\DoctrineORMAdminBundle\Tests\Datagrid;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\Query\Expr\From;
7
use Doctrine\ORM\QueryBuilder;
8
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
9
10
class ProxyQueryTest extends \PHPUnit_Framework_TestCase
11
{
12
    public function dataGetFixedQueryBuilder()
13
    {
14
        return array(
15
            array('aaa', 'bbb', 'id', 'id_idx', 33),
16
            array('aaa', 'bbb', 'id.value', 'id_value_idx', 33),
17
        );
18
    }
19
20
    /**
21
     * @dataProvider dataGetFixedQueryBuilder
22
     *
23
     * @param $class
24
     * @param $alias
25
     * @param $id
26
     */
27
    public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value)
28
    {
29
        $meta = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')
30
            ->disableOriginalConstructor()
31
            ->getMock();
32
        $meta->expects($this->any())
33
            ->method('getIdentifierFieldNames')
34
            ->willReturn(array($id));
35
36
        $mf = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataFactory')
37
            ->disableOriginalConstructor()
38
            ->getMock();
39
        $mf->expects($this->any())
40
            ->method('getMetadataFor')
41
            ->with($this->equalTo($class))
42
            ->willReturn($meta);
43
44
        $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
45
            ->disableOriginalConstructor()
46
            ->getMock();
47
        $em->expects($this->any())
48
            ->method('getMetadataFactory')
49
            ->willReturn($mf);
50
51
        $q = $this->getMock('PDOStatement');
52
        $q->expects($this->any())
53
            ->method('execute')
54
            ->willReturn(array(array($id => $value)));
55
56
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
57
            ->setConstructorArgs(array($em))
58
            ->getMock();
59
        $qb->expects($this->any())
60
            ->method('getEntityManager')
61
            ->willReturn($em);
62
        $qb->expects($this->any())
63
            ->method('getQuery')
64
            ->willReturn($q);
65
        $qb->expects($this->once())
66
            ->method('setParameter')
67
            ->with($this->equalTo($expectedId), $this->equalTo(array($value)));
68
        $qb->expects($this->once())
69
            ->method('getDQLPart')
70
            ->with($this->equalTo('from'))
71
            ->willReturn(array(new From($class, $alias)));
72
73
        $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery')
74
            ->setConstructorArgs(array($qb))
75
            ->setMethods(array('a'))
76
            ->getMock();
77
78
79
        /* Work */
80
81
        $pq->execute();
82
    }
83
}
84