Completed
Pull Request — 3.x (#882)
by
unknown
01:50
created

PagerTest::testComputeNbResultFoCompositeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
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\ORM\AbstractQuery;
15
use Doctrine\ORM\Tools\SchemaTool;
16
use PHPUnit\Framework\TestCase;
17
use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
18
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
19
use Sonata\DoctrineORMAdminBundle\Tests\Filter\QueryBuilder;
20
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ORM\Product;
21
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ORM\Store;
22
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ORM\StoreProduct;
23
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
24
25
class PagerTest extends TestCase
26
{
27
    public function testComputeNbResultFoCompositeId()
28
    {
29
        $em = DoctrineTestHelper::createTestEntityManager();
30
        $classes = [
31
            $em->getClassMetadata(Product::class),
32
            $em->getClassMetadata(StoreProduct::class),
33
            $em->getClassMetadata(Store::class),
34
        ];
35
        $schemaTool = new SchemaTool($em);
36
        $schemaTool->createSchema($classes);
37
38
        $qb = $em->createQueryBuilder()
39
            ->select('o')
40
            ->from(StoreProduct::class, 'o');
41
        $pq = new ProxyQuery($qb);
42
        $pager = new Pager();
43
        $pager->setCountColumn($em->getClassMetadata(StoreProduct::class)->getIdentifierFieldNames());
44
        $pager->setQuery($pq);
45
        $this->assertEquals(0, $pager->computeNbResult());
46
    }
47
48
    public function dataGetComputeNbResult()
49
    {
50
        return [
51
            [true],
52
            [false],
53
        ];
54
    }
55
56
    /**
57
     * @dataProvider dataGetComputeNbResult
58
     *
59
     * @param bool $distinct
60
     */
61
    public function testComputeNbResult($distinct)
62
    {
63
        $query = $this->getMockBuilder(AbstractQuery::class)
64
            ->disableOriginalConstructor()
65
            ->setMethods(['getSingleScalarResult'])
66
            ->getMockForAbstractClass();
67
68
        $query->expects($this->once())
69
            ->method('getSingleScalarResult');
70
71
        $queryBuilder = $this->getMockBuilder(QueryBuilder::class)
72
            ->disableOriginalConstructor()
73
            ->setMethods(['getQuery', 'select', 'resetDQLPart'])
74
            ->getMock();
75
76
        $queryBuilder->expects($this->once())
77
            ->method('getQuery')
78
            ->willReturn($query);
79
80
        $queryBuilder->expects($this->once())
81
            ->method('select');
82
83
        $proxyQuery = new ProxyQuery($queryBuilder);
84
        $proxyQuery->setDistinct($distinct);
85
86
        $queryBuilder->expects($this->once())
87
            ->method('resetDQLPart')
88
            ->willReturn($proxyQuery);
89
90
        $pager = new Pager();
91
        $pager->setQuery($proxyQuery);
92
        $pager->computeNbResult();
93
    }
94
}
95