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