Completed
Push — 3.x ( fc4ac9...0816de )
by Marko
02:48 queued 25s
created

PagerTest::dataGetComputeNbResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
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\Query;
16
use Doctrine\ORM\Tools\Pagination\CountWalker;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\DoctrineORMAdminBundle\Datagrid\Pager;
19
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
20
use Sonata\DoctrineORMAdminBundle\Tests\Filter\QueryBuilder;
21
22
class PagerTest extends TestCase
23
{
24
    public function dataGetComputeNbResult()
25
    {
26
        return [
27
            [true],
28
            [false],
29
        ];
30
    }
31
32
    /**
33
     * @dataProvider dataGetComputeNbResult
34
     *
35
     * @param bool $distinct
36
     */
37
    public function testComputeNbResult($distinct)
38
    {
39
        $q = $this->getMockBuilder(AbstractQuery::class)
40
            ->disableOriginalConstructor()
41
            ->setMethods(['getSingleScalarResult', 'getHint', 'setHint'])
42
            ->getMockForAbstractClass();
43
44
        $q->expects($this->once())
45
            ->method('getSingleScalarResult');
46
47
        $q->expects($this->once())
48
            ->method('getHint')
49
            ->willReturn(null);
50
51
        $q->expects($this->exactly(2))
52
            ->method('setHint')
53
            ->withConsecutive(
54
                [$this->equalTo(CountWalker::HINT_DISTINCT), $this->equalTo($distinct)],
55
                [$this->equalTo(Query::HINT_CUSTOM_TREE_WALKERS), $this->equalTo([CountWalker::class])]
56
            );
57
58
        $qb = $this->getMockBuilder(QueryBuilder::class)
59
            ->disableOriginalConstructor()
60
            ->setMethods(['getQuery'])
61
            ->getMock();
62
63
        $qb->expects($this->once())
64
            ->method('getQuery')
65
            ->willReturn($q);
66
67
        $pq = new ProxyQuery($qb);
68
        $pq->setDistinct($distinct);
69
70
        $pager = new Pager();
71
        $pager->setQuery($pq);
72
        $pager->computeNbResult();
73
    }
74
}
75