Completed
Pull Request — 3.x (#728)
by
unknown
01:45
created

ProxyQueryTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\EntityManager;
16
use Doctrine\ORM\Query\Expr\From;
17
use Doctrine\ORM\Query\Expr\OrderBy;
18
use Doctrine\ORM\Tools\SchemaTool;
19
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType;
20
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Util\NonIntegerIdentifierTestClass;
21
use Sonata\DoctrineORMAdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
22
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
23
24
class ProxyQueryTest extends PHPUnit_Framework_TestCase
25
{
26
    const DOUBLE_NAME_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity';
27
28
    /**
29
     * @var EntityManager
30
     */
31
    private $em;
32
33
    public static function setUpBeforeClass()
34
    {
35
        if (!Type::hasType('uuid')) {
36
            Type::addType('uuid', 'Sonata\DoctrineORMAdminBundle\Tests\Fixtures\DoctrineType\UuidType');
37
        }
38
    }
39
40
    protected function setUp()
41
    {
42
        $this->em = DoctrineTestHelper::createTestEntityManager();
43
44
        $schemaTool = new SchemaTool($this->em);
45
        $classes = array(
46
            $this->em->getClassMetadata(self::DOUBLE_NAME_CLASS),
47
        );
48
49
        try {
50
            $schemaTool->dropSchema($classes);
51
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
52
        }
53
54
        try {
55
            $schemaTool->createSchema($classes);
56
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
57
        }
58
    }
59
60
    protected function tearDown()
61
    {
62
        $this->em = null;
63
    }
64
65
    public function dataGetFixedQueryBuilder()
66
    {
67
        return array(
68
            array('aaa', 'bbb', 'id', 'id_idx', 33, Type::INTEGER),
69
            array('aaa', 'bbb', 'associatedId', 'associatedId_idx', 33, null),
70
            array('aaa', 'bbb', 'id.value', 'id_value_idx', 33, Type::INTEGER),
71
            array('aaa', 'bbb', 'id.uuid', 'id_uuid_idx', new NonIntegerIdentifierTestClass('80fb6f91-bba1-4d35-b3d4-e06b24494e85'), UuidType::NAME),
72
        );
73
    }
74
75
    /**
76
     * @dataProvider dataGetFixedQueryBuilder
77
     *
78
     * @param $class
79
     * @param $alias
80
     * @param $id
81
     */
82
    public function testGetFixedQueryBuilder($class, $alias, $id, $expectedId, $value, $identifierType)
83
    {
84
        $meta = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataInfo');
85
        $meta->expects($this->any())
86
            ->method('getIdentifierFieldNames')
87
            ->willReturn(array($id));
88
        $meta->expects($this->any())
89
            ->method('getTypeOfField')
90
            ->willReturn($identifierType);
91
92
        $mf = $this->createMock('Doctrine\ORM\Mapping\ClassMetadataFactory');
93
        $mf->expects($this->any())
94
            ->method('getMetadataFor')
95
            ->with($this->equalTo($class))
96
            ->willReturn($meta);
97
98
        $platform = $this->createMock('Doctrine\DBAL\Platforms\PostgreSqlPlatform');
99
100
        $conn = $this->createMock('Doctrine\DBAL\Connection');
101
        $conn->expects($this->any())
102
            ->method('getDatabasePlatform')
103
            ->willReturn($platform);
104
105
        $em = $this->createMock('Doctrine\ORM\EntityManager');
106
        $em->expects($this->any())
107
            ->method('getMetadataFactory')
108
            ->willReturn($mf);
109
        $em->expects($this->any())
110
            ->method('getConnection')
111
            ->willReturn($conn);
112
113
        // NEXT MAJOR: Replace this when dropping PHP < 5.6
114
        // $q = $this->createMock('PDOStatement');
115
        $q = $this->getMockBuilder('stdClass')
116
            ->setMethods(array('execute'))
117
            ->getMock();
118
        $q->expects($this->any())
119
            ->method('execute')
120
            ->willReturn(array(array($id => $value)));
121
122
        $qb = $this->getMockBuilder('Doctrine\ORM\QueryBuilder')
123
            ->setConstructorArgs(array($em))
124
            ->getMock();
125
        $qb->expects($this->any())
126
            ->method('getEntityManager')
127
            ->willReturn($em);
128
        $qb->expects($this->any())
129
            ->method('getQuery')
130
            ->willReturn($q);
131
        $qb->expects($this->once())
132
            ->method('setParameter')
133
            ->with($this->equalTo($expectedId), $this->equalTo(array($value)));
134
        $qb->expects($this->any())
135
            ->method('getDQLPart')
136
            ->will($this->returnCallBack(function ($part) use ($class, $alias) {
137
                $parts = array(
138
                    'from' => array(new From($class, $alias)),
139
                    'orderBy' => array(new OrderBy('whatever', 'DESC')),
140
                );
141
142
                return $parts[$part];
143
            }));
144
        $qb->expects($this->once())
145
            ->method('addOrderBy')
146
            ->with("$alias.$id", null);
147
        $qb->expects($this->once())
148
            ->method('getRootEntities')
149
            ->willReturn(array($class));
150
        $qb->expects($this->exactly(2))
151
            ->method('getRootAliases')
152
            ->willReturn(array($alias));
153
154
        $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery')
155
            ->setConstructorArgs(array($qb))
156
            ->setMethods(array('a'))
157
            ->getMock();
158
159
        /* Work */
160
161
        $pq->execute();
162
    }
163
164
    public function testAddOrderedColumns()
165
    {
166
        $qb = $this->em->createQueryBuilder()
167
                       ->select('o.id')
168
                       ->distinct()
169
                       ->from(self::DOUBLE_NAME_CLASS, 'o')
170
                       ->orderBy('o.name', 'ASC')
171
                       ->addOrderBy('o.name2', 'DESC');
172
173
        $pq = $this->getMockBuilder('Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery')
174
                   ->disableOriginalConstructor()
175
                   ->getMock();
176
177
        $reflection = new \ReflectionClass(get_class($pq));
178
        $method = $reflection->getMethod('addOrderedColumns');
179
        $method->setAccessible(true);
180
        $method->invoke($pq, $qb);
181
182
        $dqlPart = $qb->getDqlPart('select');
183
        $this->assertCount(3, $dqlPart);
184
        $this->assertEquals('o.id', $dqlPart[0]);
185
        $this->assertEquals('o.name', $dqlPart[1]);
186
        $this->assertEquals('o.name2', $dqlPart[2]);
187
    }
188
}
189