|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pagerfanta\Tests\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
6
|
|
|
use Pagerfanta\Adapter\DoctrineDbalAdapter; |
|
7
|
|
|
|
|
8
|
|
|
class DoctrineDbalAdapterTest extends DoctrineDbalTestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function testGetNbResults() |
|
11
|
|
|
{ |
|
12
|
|
|
$adapter = $this->createAdapterToTestGetNbResults(); |
|
13
|
|
|
|
|
14
|
|
|
$this->doTestGetNbResults($adapter); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function testGetNbResultsShouldWorkAfterCallingGetSlice() |
|
18
|
|
|
{ |
|
19
|
|
|
$adapter = $this->createAdapterToTestGetNbResults(); |
|
20
|
|
|
|
|
21
|
|
|
$adapter->getSlice(1, 10); |
|
22
|
|
|
|
|
23
|
|
|
$this->doTestGetNbResults($adapter); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
private function doTestGetNbResults(DoctrineDbalAdapter $adapter) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->assertSame(50, $adapter->getNbResults()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testGetSlice() |
|
32
|
|
|
{ |
|
33
|
|
|
$adapter = $this->createAdapterToTestGetSlice(); |
|
34
|
|
|
|
|
35
|
|
|
$this->doTestGetSlice($adapter); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testGetSliceShouldWorkAfterCallingGetNbResults() |
|
39
|
|
|
{ |
|
40
|
|
|
$adapter = $this->createAdapterToTestGetSlice(); |
|
41
|
|
|
|
|
42
|
|
|
$adapter->getNbResults(); |
|
43
|
|
|
|
|
44
|
|
|
$this->doTestGetSlice($adapter); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function createAdapterToTestGetSlice() |
|
48
|
|
|
{ |
|
49
|
|
|
$countQueryBuilderModifier = function () { }; |
|
50
|
|
|
|
|
51
|
|
|
return new DoctrineDbalAdapter($this->qb, $countQueryBuilderModifier); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function doTestGetSlice(DoctrineDbalAdapter $adapter) |
|
55
|
|
|
{ |
|
56
|
|
|
$offset = 30; |
|
57
|
|
|
$length = 10; |
|
58
|
|
|
|
|
59
|
|
|
$qb = clone $this->qb; |
|
60
|
|
|
$qb->setFirstResult($offset)->setMaxResults($length); |
|
61
|
|
|
|
|
62
|
|
|
$expectedResults = $qb->execute()->fetchAll(); |
|
63
|
|
|
$results = $adapter->getSlice($offset, $length); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertSame($expectedResults, $results); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @expectedException \Pagerfanta\Exception\InvalidArgumentException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testItShouldThrowAnInvalidArgumentExceptionIfTheQueryIsNotSelect() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->qb->delete('posts'); |
|
74
|
|
|
$countQueryModifier = function () { }; |
|
75
|
|
|
|
|
76
|
|
|
new DoctrineDbalAdapter($this->qb, $countQueryModifier); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testItShouldCloneTheQuery() |
|
80
|
|
|
{ |
|
81
|
|
|
$adapter = $this->createAdapterToTestGetNbResults(); |
|
82
|
|
|
|
|
83
|
|
|
$this->qb->innerJoin('p', 'comments', 'c', 'c.post_id = p.id') |
|
84
|
|
|
->groupBy('c.post_id'); |
|
85
|
|
|
|
|
86
|
|
|
$this->assertSame(50, $adapter->getNbResults()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @expectedException \Pagerfanta\Exception\InvalidArgumentException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testItShouldThrowAnInvalidArgumentExceptionIfTheCountQueryBuilderModifierIsNotACallable() |
|
93
|
|
|
{ |
|
94
|
|
|
$countQueryBuilderModifier = 'ups'; |
|
95
|
|
|
|
|
96
|
|
|
new DoctrineDbalAdapter($this->qb, $countQueryBuilderModifier); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
private function createAdapterToTestGetNbResults() |
|
100
|
|
|
{ |
|
101
|
|
|
$countQueryBuilderModifier = function (QueryBuilder $queryBuilder) { |
|
102
|
|
|
$queryBuilder->select('COUNT(DISTINCT p.id) AS total_results') |
|
103
|
|
|
->setMaxResults(1); |
|
104
|
|
|
}; |
|
105
|
|
|
|
|
106
|
|
|
return new DoctrineDbalAdapter($this->qb, $countQueryBuilderModifier); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|