Completed
Push — 3.x-dev-kit ( 4fe8f1 )
by
unknown
11:30 queued 08:33
created

ProxyQueryTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 4
c 5
b 0
f 2
lcom 0
cbo 4
dl 0
loc 114
rs 10

3 Methods

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