Completed
Pull Request — master (#503)
by Mihai
03:06
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\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