Completed
Pull Request — master (#503)
by Mihai
04:17 queued 43s
created

ProxyQueryTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 74
rs 10

2 Methods

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