Completed
Pull Request — 3.x (#882)
by
unknown
02:27
created

PagerTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 7
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testComputeNbResultFoCompositeId() 0 20 1
A dataGetComputeNbResult() 0 7 1
A testComputeNbResult() 0 33 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrineORMAdminBundle\Tests\Datagrid;
15
16
use Doctrine\ORM\AbstractQuery;
17
use Doctrine\ORM\Tools\SchemaTool;
18
use PHPUnit\Framework\TestCase;
19
use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
20
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
21
use Sonata\DoctrineORMAdminBundle\Tests\Filter\QueryBuilder;
22
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ORM\Product;
23
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ORM\Store;
24
use Sonata\DoctrineORMAdminBundle\Tests\Fixtures\Entity\ORM\StoreProduct;
25
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
26
27
class PagerTest extends TestCase
28
{
29
    public function testComputeNbResultFoCompositeId()
30
    {
31
        $em = DoctrineTestHelper::createTestEntityManager();
32
        $classes = [
33
            $em->getClassMetadata(Product::class),
34
            $em->getClassMetadata(StoreProduct::class),
35
            $em->getClassMetadata(Store::class),
36
        ];
37
        $schemaTool = new SchemaTool($em);
38
        $schemaTool->createSchema($classes);
39
40
        $qb = $em->createQueryBuilder()
41
            ->select('o')
42
            ->from(StoreProduct::class, 'o');
43
        $pq = new ProxyQuery($qb);
44
        $pager = new Pager();
45
        $pager->setCountColumn($em->getClassMetadata(StoreProduct::class)->getIdentifierFieldNames());
46
        $pager->setQuery($pq);
47
        $this->assertEquals(0, $pager->computeNbResult());
48
    }
49
50
    public function dataGetComputeNbResult()
51
    {
52
        return [
53
            [true],
54
            [false],
55
        ];
56
    }
57
58
    /**
59
     * @dataProvider dataGetComputeNbResult
60
     *
61
     * @param bool $distinct
62
     */
63
    public function testComputeNbResult($distinct)
64
    {
65
        $query = $this->getMockBuilder(AbstractQuery::class)
66
            ->disableOriginalConstructor()
67
            ->setMethods(['getSingleScalarResult'])
68
            ->getMockForAbstractClass();
69
70
        $query->expects($this->once())
71
            ->method('getSingleScalarResult');
72
73
        $queryBuilder = $this->getMockBuilder(QueryBuilder::class)
74
            ->disableOriginalConstructor()
75
            ->setMethods(['getQuery', 'select', 'resetDQLPart'])
76
            ->getMock();
77
78
        $queryBuilder->expects($this->once())
79
            ->method('getQuery')
80
            ->willReturn($query);
81
82
        $queryBuilder->expects($this->once())
83
            ->method('select');
84
85
        $proxyQuery = new ProxyQuery($queryBuilder);
86
        $proxyQuery->setDistinct($distinct);
87
88
        $queryBuilder->expects($this->once())
89
            ->method('resetDQLPart')
90
            ->willReturn($proxyQuery);
91
92
        $pager = new Pager();
93
        $pager->setQuery($proxyQuery);
94
        $pager->computeNbResult();
95
    }
96
}
97